• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Way to use Variables(Strings) in Struct Pathing?

Hello All,

I am hoping this to be a very easy question that I may have overlooked in the manual, but I can't seem to find the answer. The title may be a little confusing so let me break it down.

Lets say I create this struct:

GML:
var stats =
{
    stat0 : 23
    stat1 : 24
}

Keeping it basic ,but let's say I have multiple stats, and some containing additional structs. My goal is to be able to navigate to these using custom strings (for potential looping). This can be done in JSON very easily, but I am not sure this functionality exists within structs yet.

So I would want to navigate using something like this:

GML:
var a = "stat" + string(0)

//Getting value from Struct
var statvalue = stats[a] //Should be the same as var statvalue = stats[stat0]
I should expect this to give me the value of stat0 which was 23; however, it fails when attempting this code. Again not sure if this is a feature yet, just wanted to make sure before moving on to another method.

Appreciate all the feedback, and if there are any questions or confusion regarding the post feel free to ask.

Thanks!
 

chamaeleon

Member
Try using the $ accessor, stats[$ a], or the function variable_struct_get(stats, a). Hopefully you know stats[a] treats stats as an array, syntactically (although it expects a to be a number, not a string, of course), and if you wanted direct access to the struct member without the dynamic string creation, the syntax is stats.stat0.
 
Last edited:
Try using the $ accessor, stats[$ a], or the function variable_struct_get(stats, a). Hopefully you know stats[a] treats stats as an array, syntactically (although it expects a to be a number, not a string, of course), and if you wanted direct access to the struct member without the dynamic string creation, the syntax is stats.stat0.
Both of those methods worked for what I was needing. I saw the variable_struct_get function mentioned in the manual but couldn't figure out how to get it to work until you explained it. Thank you for your help and a little more insight on how it all works!!
 
Top