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

Looping Through Structs

mar_cuz

Member
Hi Guys,

Just mucking around in a test project to familiarise myself with the new additions to game makers code and I was wondering if it is possible to loop through stucts and if so how?

Thanks,
 

Nidoking

Member
Are you attempting to loop through the values contained in a single struct, or to loop through a collection of structs?
 

FrostyCat

Redemption Seeker
The basic book line for looping through a struct (GMS 2.3.1):
GML:
var keys = variable_struct_get_names(strc);
for (var i = array_length(keys)-1; i >= 0; --i) {
    var k = keys[i];
    var v = strc[$ k];
    /* Use k and v here */
}
For GMS 2.3.0, substitute strc[$ k] with variable_struct_get(strc, k).
 

Nidoking

Member
I suppose it works as an academic exercise, but I can't imagine what practical purpose there would be to this. If you've got data that you want to loop through, pretty much any other structure makes more sense. If you've got a struct, the data should represent specific things that you'd want to access by variable names to do the things they're meant to do.
 

FrostyCat

Redemption Seeker

Nidoking

Member
Okay, I can see the value in those operations. I'd still want to write my own specific functions for handling those, because it seems like there's always going to be some minor thing you'd want to do differently in any real-world situation where you'd have to write some wrapper for the general function, and it helps me control exactly what my structs are doing, but I get the point.
 
I found your post because this is what I needed to do. It works fine but the variables are not being listed in any kind of order I can see. Will I have to re-order them manually? I was expecting either the creation order or alphabetical order.

Sorry to necro bump this thread but it is directly relevant. To the OP.
 

Alice

Darts addict
Forum Staff
Moderator
@Daniel Mallett Yes, the struct keys are sorted in a seemingly random order; might have something to do with underlying hash codes (basically, string keys are converted to an integer using some very specific calculations for more efficient implementation).

As far as I can tell, you can't retrieve the creation order (you'd need to pretty much store it in a separate array), but you can easily convert these keys to alphabetical order:
GML:
var some_struct = { lorem: 123, ipsum: 456, dolor: 789, sit: 1011, amet: 1213 };
var keys = variable_struct_get_names(some_struct);
show_debug_message(keys); // will show the messy unordered keys
array_sort(keys, true /* ascending order */);
show_debug_message(keys); // now the keys are neatly ordered!
 

kburkhart84

Firehammer Games
All said, structs aren't meant for actual ordered data....arrays are however. You should use the right tool for the job for sure. If you make your own topic about specifically what you are doing we might be able to suggest what could be the best(or better, in our opinion) way to go about it @Daniel Mallett
 
Top