Struct variables

DesArts

Member
I'm new to structs with the GM update. Couldnt find a whole lot of info on how to work with struct variables.

So, if I do

var1 = {}
var2 = var1


Does that copy or just reference the same struct twice. If it doesn't copy, can I duplicate one in some other way?

Also - does passing a struct into a as a parameter like
function function(struct) {}
and altering it in there keep referencing the original struct?

Basic stuff really, It might have been said somewhere but I couldn't find it. Main issue is the duplication.

Thanks
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Structs (iirc) are passed by reference, and not copied. To copy a struct you'd need to use the variable_struct_* functions to loop through all the variables in a struct and copy them to a new one.
 

samspade

Member
I'm new to structs with the GM update. Couldnt find a whole lot of info on how to work with struct variables.

So, if I do

var1 = {}
var2 = var1


Does that copy or just reference the same struct twice. If it doesn't copy, can I duplicate one in some other way?

Also - does passing a struct into a as a parameter like
function function(struct) {}
and altering it in there keep referencing the original struct?

Basic stuff really, It might have been said somewhere but I couldn't find it. Main issue is the duplication.

Thanks
Nocturne already answered this, but one thing I would suggest, is that if you're going to be copying a certain type of struct often, it might be worth building in a copy method to that struct. This will (probably) always be faster than using the generic method.
 

Joe Ellis

Member
I agree with samspade, with methods, they work with scope so if made by the original struct, having a method bound to that struct that creates a new one\copy of all it's variables is probably the fastest way. I haven't actually tested it but so far I'm assuming that referencing variables from a struct within a method that was created & bound to that struct, is faster than using the classic struct.variable.
Without testing I can't really be sure, maybe samspade has tested it, be he too said (probably) lol, I'm gonna run a test tomorrow haha
 

Yal

šŸ§ *penguin noises*
GMC Elder
Nocturne already answered this, but one thing I would suggest, is that if you're going to be copying a certain type of struct often, it might be worth building in a copy method to that struct. This will (probably) always be faster than using the generic method.
Yeah, especially if you define a constructor: this preallocates room for all variables defined in the structure, instead of manually appending them in runtime. It uses less memory too, since constructor variables share the same nametable for all instances of the struct, while each struct has a separate one for "added later" variables.
 

Joe Ellis

Member
I've been thinking about for a while why constructors would be useful, but I guess it's essentially to do with performance. Of course you could create a function that creates a struct locally, adds variables, then returns it. (I've been doing this so far) But the constructor element basically does the same thing, but the engine has some "turbo" power for doing that specific thing
 
Top