GMS 2.3+ 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

Member
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.
 

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.
 
Top