Variable property inside Struct

So, I have the following code for a struct that is organized inside a map variable:

currency_properties := struct:
    Type : currency
    Value : int = 0
    GainPerSecond : int = 0

var Currencies<private> : [currency]currency_properties = map{}

And trying to set these values in another function:

SetValue(Currency:currency, Value:int):void=
    if(CurrencyProperty := Currencies[Currency]):
    set CurrencyProperty.Value = Value

But it’s throwing me the error:

The assignment's left hand expression type `int` cannot be assigned to(3509)

Well, fair enough, right? I don’t have the ‘var’ expression in the ‘Value’ property inside my struct.
But it also doesn’t work when I put it there, throwing me a new error:

Structs may not contain mutable members.(3607)

I can see a workaround by instancing a new struct every time I’ll change a value, but is there no better way to do this? Am I missing something about structs?

Thanks in advance for any support!

1 Like

The way you mention at the last is the correct and the only way to change a struct. As you already knew, struct cannot have mutable data inside it. Check the documentation about Struct for more info!

1 Like

In my experience you need to treat is as initializing a new structure each time, but if anyone has a better way, I’d love to hear it.

Verse is the only language I know with this issue, it seems like they are so busy putting in cool stuff, that basic stuff is neglected.

2 Likes

Thanks! I will solve it this way then.

I feel that…
The basic stuff are so basic and intuitive that if baffles me that they aren’t available yet.

I personally never use structs, using class (or class<concrete> if you want to expose data) allows you for much more.

Though, non scalar values don’t seem to expose well at the moment (see Nested concrete classes @editable properties gets reset everytime)

2 Likes

Alternatively, you can create a class and create methods that affects the variables you have if you wish so. In this way you don’t have to instance a whole new class to change one variable

2 Likes

@im_a_lama and @SeedohFN
I didn’t think of using a class to solve it, it worked perfectly and it’s exactly what I wanted.
Thank you very much!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.