so with GM2.3 structs, how would one go about instances(heavy object) of a struct?
I used structs to define "Units" say
GML:
tower() contructor
{
hp = 100
name = tower
x = 0
y = 0
}
this essentially has all the info for that unit. For it to concretely (collisions) exist in the world I would need to associate it with a game instance.
I do it by creating a struct instance [
new tower()
] , giving it to a object instance [
instance_create(x,y, obj_tower)
] and then transfer all variables values from the struct to the instance. (a script heavily lifted from FrostyCat's struct_merge() but copying to instance instead)
This works nicely: it creates game instances that automatically get all the variables from the unit structs i make.
But I'm realising now that it creates a duplication of all variables: the struct and the instance copy and they are not synchronised. This makes sense but it feels inefficient. In theory, I only need to change the instance's variables, since that is what acts and exists in the world. But it feels off to keep an out of date associated struct. Also, when/if saving comes in line, the struct it would be usefull to keep the struct updated (easier to save).
Anyone encountered this design situation? How do you handle it?
- I can see how I could not even copy the struct variables and simply operate on the struct variables. ie: if collision mystruct.hp -= 10 but stat would be a mess and all instance code would always need "mystruct" prefix
- Not use instances, do all code from struct. but that loses all the benefits from built in instances.(events)
- Let variables desynchronise, synchronise them manually when needed.
I know this is more of a design question, but I've seen a lot of discussion on good ways to use the structs and I like to think the situation I've described has been encountered here?