How to programatically access a variable number or string?

Hi there,

How do you access a variable by using another variable for its name?

E.g. let’s say you have:
VariableString(name)=“Speed”

And you want to access speed.

How would you access a variable name called something plus speed, e.g. “Ship”+VariableString(name) (in order to access a variable called “ShipSpeed” in this case.

This is just a random example, there’s a different way to do object variables built in.

I’m also experimenting and trying to figure it out, but if you know, please let me know in the mean time. Thank you!

You can access structures dynamically using a string like so:

Variable(Ship["Speed"])

Where “Speed” is the child variable of Ship but you can add structures to structures, so in case it is what you want, you can make “Ship” to be a structure of something and make “Speed” the structure of “Ship” and do:

Variable(Something["Ship"]["Speed"])

You can also access inventory items dynamically using a string but inventory items are not variables so you can not do

ShipSpeed = 100

to make it store 100 as value but instead you need to

Repeat 100 times: Add "ShipSpeed" to inventory

So Inventory is might be not what you want in this case but I guess you can do what you trying with structures :slight_smile:
Each structure can store it own value, so you can go pretty complex with them.

May I ask what you actually trying to do?

Thank you, I will try that.

P.S. As for what I’m trying to do, just writing down some game stats into files (or virtual files for HTML) so that they can be reloaded once the game is started again (so we don’t have to start from scratch).

There’s definitely a few different ways to do that (e.g. use variables in hierarchy name might be possible, not sure), but I wanted to consider using variables named accordingly for the purpose. Still considering various options. :slight_smile:

I noticed I should give a closer example as to what exactly I’m doing, and here it is:

That’s just an example of “what I would want to do”, rather than what works. I’m later trying to access this and write it as text on a screen (just to verify it works).

Hm, so far this seems to work:
VariableString(BulletDamage.[VariableString(FileToRead)])

So maybe I’ll use something like that to get BulletDamage.Turret1 (instead of Turret1.BulletDamage since I can’t do that without having actual objects which I won’t have on some scenes, e.g. upgrade screen or shop screen where I may still need to access the data).

Edit: Confirmed, I can read that with:
VariableString(BulletDamage.[“Turret1”])

Obviously, this would need to be global variables once completed ,and the actions would repeat each time for every Turret type (I would put them in external actions, then call them once for each set value for FileToRead. Kind of like a function, or as close as we can get to a function in Gdevelop HTML 5.

Maybe I miss the whole picture here but In case you just want to save/load information, I fail to see how dynamic access of variables helps with anything :confused:

I mean, if you just want to save the speed of the ship for example you just simply need to write the value in to file or web storage and name the group holding the value accordingly. So, speed of ship is stored within group “ShipSpeed”.
In case there are multiple instances of the ship and you want to store the speed of each individually, normally I use and object variable as an ID to identify each instance when saving and loading their information like so:

Generate ID number for each ship:

For each Ship: Do = Variable(counter) to ID variable of ship Do + 1 to variable counter

And when you save info just do:

For each Ship: Write ship.Variable(Speed) in "ShipSpeed" + ToString(ship.Variable(ID)) of file "save"

And when you load the info, you can just simply use the ID variable to find the info of each ship:

For each Ship: If "ShipSpeed" + ship.Variable(ID) exists in file "save" :Read "ShipSpeed" + ship.Variable(ID) from file save and store value in variable Load :Do = Variable(Load) to ship.Variable(Speed)

Since you can always read/load only one info at the time and the variable you load in to must be a scene variable, you can use just a single variable to load the values in to and then simply use that variable right after load to pass the information to an other variable.

Actually this is a pretty good point and I may use the “tree” name including the object method if possible.

But I need to load some variables for every object globally, so that each scene can access it (e.g. Shop, Repair, Level1… Level2, all can access the current Turrent stats, for example). I actually used Ship just as a random term. :slight_smile:

So I would probably end up with Global variables such as Turret1.Stat1, Turret1.Stat2, Turret1.Stat3; Turret2.Stat, and so forth. Not for each turret instance, of course, but for each turret type (so for each Turret1 that is yet to be placed/doesn’t exist on map yet, or even in the entire level, such as Upgrade or Shop scenes, and so for each Turret of type 2 as well).

So, for example, when I create a Turret 1 instance, it will get those stats (e.g. upgraded speed of rotation, strength of fire, etc.), even though no Turret 1 exists yet on the map. And when I want to upgrade the Turret 1 stats in e.g. Upgrade shop, I would be upgrading those global variables rather than object variables directly (as the objects don’t actually exist in this scene). Also, each Turret of one type will have exactly the same stats (e.g. you can’t upgrade just one turret of a type), so I don’t have a big instancing issue, I just need to save, load, and share those stats properly across every scene.

Edit: I reread your point, and it may actually work completely. But I would rather load those variables from memory when sharing across scenes such as current map, or shop, or upgrade shop (as currently I need to set variable values for each created instance, once an instance gets created), so perhaps still some kind of global variables. I’ll give it some though, there might be a better way. :slight_smile:

You can just load the info from save at the beginning of each scene.

At the end of each scene just save any info you want to be saved/shared and at the beginning of each scene just load any info you need. It make it a lot easier even to move values of object variables from one scene to an other without need to use a bunch of global variables.
You can keep things simple and organised this way.

Might end up doing that. :slight_smile: In either case, thanks for offering some helpful advice.