I use option button and dictionaries to populate them.
my_stuf = {“Stone”, [100, “Global.stone”]
For example to choose a material (stone) and also show the amount of stone currently available. In addition I tried to also transport information about the variable that safes the stone amount and use this to manipulate this amount.
Problem is it just does not work.
I have horrible time trying to access the variable (it is in a global script like Global.stone) from the dictionary. No matter what I try I end up with errors.
str to var seems to not work, tried that.
nodepath and get node does not work.
putting the variable in directly and not as string does also not work.
Am I on a totally wrong path here. Is there a fundamentally better way to handle optionbutton?
This is a problem that comes back to me in many different forms and I feel like there must be a better, smarter way to do this?
Okay, after browsing the net I think I understand the problem and can better explain it.
What I was trying to do was reference a basic variable which does not work (I now know). Every time it only generated the value and this did not allow me to change the base variable amount.
Now, how do you go about it if you have a dynamic situation (player chooses options, events interact etc) and you can not hard code a variable into a function but you still need to change a basic variable?
If I can not reference basic variables how do experienced people workaround it/have better solutions?
Do you not safe information in basic variables? var stone = 100 ?
What? I’m confused even more now
You can define a variable var stone = 100 and then reference it in the code or change its value. E.g. This will assign a 100 to the stone variable, then check if the value is greater than 50 and if so, add 10 to the stone variable.
var stone = 100
If stone > 50:
stone += 10
Share your code please, because otherwise we’re going around the tree.
Instead of a basic int I use an array with only one value. I can then reference the array and manipulate the value.
Not knowing that gdscript does not reference basic variables was the core problem. It is a bit of a work around but for most applications I have in mind I can use arrays or dictionaries.
What do you mean? Why can’t you use a basic int variable?
GDScript can handle basically all variable types that you can find in other programming languages.
I’m so confused
GDscript can not reference basic variable types like int. This was a big problem for me in my code. I now use arrays to do the same and I can reference them without a problem.
All primitive types are passed by value and can not be referenced.
I don’t make the rules I just have to follow them.
This is why I had to change my variables from int to array. That works.
Ohh, now I finally get what you mean - the difference between “by val” and “by ref”. But that’s how it works in most programming languages, it’s not only GDScript thing - most primitive variable types are passed by value, not reference.
I would suggest that maybe you could make a custom inner class and put an int variable inside. Classes are passed by reference. I would say this solution would be more elegant than using Arrays.
Making classes is something new to me, I have to first look into it and learn it.
This is my first project and I learn as I go. That explains my many noob questions. Sry.