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?
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!
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