I am aiming to pass individual structs to instances. The idea is that I have a mega struct stored as a macro, but I want to pass the relevant data to instances as needed.
What would be the best way to go about this? It is dawning on me that instead of having a mega struct and attempting to pass by reference, I should be rolling with a mega constructor and passing structs as needed. Is this the right approach? Thanks in advance for your help.
I am aiming to pass individual structs to instances. The idea is that I have a mega struct stored as a macro, but I want to pass the relevant data to instances as needed.
What would be the best way to go about this? It is dawning on me that instead of having a mega struct and attempting to pass by reference, I should be rolling with a mega constructor and passing structs as needed. Is this the right approach? Thanks in advance for your help.
I'm afraid this does not really tell me at all what you are trying to do precisely. All variables (global, local, instance, struct members) with struct values are containing references to structs. Anything passed around in any way whatsoever will be a reference, not a concrete struct in itself. No idea how "mega constructor" relates to structs to pass in some manner to something.
I'm afraid this does not really tell me at all what you are trying to do precisely. All variables (global, local, instance, struct members) with struct values are containing references to structs. Anything passed around in any way whatsoever will be a reference, not a concrete struct in itself. No idea how "mega constructor" relates to structs to pass in some manner to something.
I want to create individual structs for instances which I can then populate a grid with. Sorry for not being clearer.
I want to create individual structs for instances which I can then populate a grid with. Sorry for not being clearer.
Any reason not to store those structs using an instance variable in each instance?
Edit: I'm not sure if this is a chicken and egg question, actually. Does the struct come first and exist for a while before the instance appears in the game, or does the instance get created and a struct created after?
You're right, I had it mixed up. I don't know how to approach the struct situation. I will have a single object for a variety of different entities, and the struct will contain the relevant data [stats, sprite etc]. But I'm not sure how to assign a struct to a created unit [storing said struct as an instance variable as you suggest].
Any reason not to store those structs using an instance variable in each instance?
Edit: I'm not sure if this is a chicken and egg question, actually. Does the struct come first and exist for a while before the instance appears in the game, or does the instance get created and a struct created after?
You're right, I had it mixed up. I don't know how to approach the struct situation. I will have a single object for a variety of different entities, and the struct will contain the relevant data [stats, sprite etc]. But I'm not sure how to assign a struct to a created unit [storing said struct as an instance variable as you suggest].
Case1: Struct created on the fly in the instance Create eventmy_stats = new Stats(. );
Case 2: Struct created on the fly and assigned to an existing or newly created instance var inst = instance_create_layer(xpos, ypos, "Instances", obj_whatever); inst.my_stats = new Stats(. );
Case 3: Struct exists in some structure beforehand and is looked up in instance Create event global.all_stats =
my_stats = global.all_stats.stats2;
Case 4: Struct exists in some structure and is assigned to an existing or newly created instance global.all_stats =
var inst = instance_create_layer(xpos, ypos, "Instances", obj_whatever); inst.my_stats = global.all_stats.stats2;
All while keeping in mind all these assignments uses references to structs, and as such, if more than one instance refers to the same struct, modying the content referenced by one instance will impact all other rferences to the same struct. Either implement some struct copy functionality, or create new structs every time, if this is not desired behavior.
my_stats = new Stats(. );
Case 2: Struct created on the fly and assigned to an existing or newly created instance var inst = instance_create_layer(xpos, ypos, "Instances", obj_whatever); inst.my_stats = new Stats(. );
Case 3: Struct exists in some structure beforehand and is looked up in instance Create event global.all_stats =
my_stats = global.all_stats.stats2;
Case 4: Struct exists in some structure and is assigned to an existing or newly created instance global.all_stats =
var inst = instance_create_layer(xpos, ypos, "Instances", obj_whatever); inst.my_stats = global.all_stats.stats2;
All while keeping in mind all these assignments uses references to structs, and as such, if more than one instance refers to the same struct, modying the content referenced by one instance will impact all other rferences to the same struct. Either implement some struct copy functionality, or create new structs every time, if this is not desired behavior.
Thanks for the help =) I do not want to pass by reference so struct copy functionality is the order of the day. What I currently have is this:
///@construct() function construct(_speed, _melee, _ranged, _armour, _actions, _awareness, _sprite, _created_sound, _movement_sound, _attack_sound, _death_sound) constructor < spd = _speed; melee = _melee; ranged = _ranged; armour = _armour; actions = _actions; awareness = _awareness; sprite = _sprite; created_sound = _created_sound; movement_sound = _movement_sound; attack_sound = _attack_sound; death_sound = _death_sound; >///@print_struct() function print_struct(entity) < //unit stats if entity == "marine" < var thing = new construct(3,2,3,1,2,1,noone, noone, noone, noone, noone); >> //creating instances and assigning structs button = instance_create_layer(room_width/2, (room_height/2) + (-(sprite_get_height(spr_menu_button)*2.5) * i) + (sprite_get_height(spr_menu_button)) * (i * 1.25), button_layer, obj_menu_button); //we want to use a constructor to assign structs to menu buttons //that way the menu buttons will know what to do and it will be simple // var struct = print_struct("marine"); button.button_data = struct; show_message(button.button_data.spd); >
The last line of code throws an error, saying it cannot be resolved. I think I am close to the solution but it is eluding me. The button is obviously not a unit, but I'll be assigning structs to them as well so was using them to test the approach.
Edit: This may be because of a bug I have introduced. I'll post back when I've fixed the bug.
Last edited: Feb 7, 2022Well, you're calling a function, print_struct() , that does not return a value (in this case, a struct), that you can later use to extract a value from. I'm not sure where you got the name print_struct() from, since it would seemingly imply printing something somewhere, and not necessarily construct anything. In any case, if you return thing from the function, that value would be assigned to your local variable (which is not needed, since you can just assign to button.button_data directly).
Well, you're calling a function, print_struct() , that does not return a value (in this case, a struct), that you can later use to extract a value from. I'm not sure where you got the name print_struct() from, since it would seemingly imply printing something somewhere, and not necessarily construct anything. In any case, if you return thing from the function, that value would be assigned to your local variable (which is not needed, since you can just assign to button.button_data directly).
You're right about button.button_data = print_struct("marine") , thank you. I'll rename the function. Sorry to be a pain in the arse but I'm running into another problem.
var i; //iterate through button count for (i = 0; i < button_count; i++)< button = instance_create_layer(room_width/2, (room_height/2) + (-(sprite_get_height(spr_menu_button)*2.5) * i) + (sprite_get_height(spr_menu_button)) * (i * 1.25), button_layer, obj_menu_button); show_message(instance_exists(button)); _id = instance_id_get(button) show_message(string(_id));
Show_message for instance_exists gives 1, for _id gives -4. Am I missing something? Sorry for being a pain.
Edit: Sorry! After a bit of searching the right code was
for (i = 0; i < button_count; i++)< button = instance_create_layer(room_width/2, (room_height/2) + (-(sprite_get_height(spr_menu_button)*2.5) * i) + (sprite_get_height(spr_menu_button)) * (i * 1.25), button_layer, obj_menu_button); show_message(instance_exists(button)); _id = instance_find(obj_menu_button, i); button.button_data = template("marine"); show_message(variable_instance_get(_id, "button_data"));
Thanks for all your help =)
Last edited: Feb 7, 2022
//we want to use a constructor to assign structs to menu buttons
//that way the menu buttons will know what to do and it will be simple
This seems like what you want to do: Have a button that its behavior is variable or data driven. While structs can certainly help do that, it can be done in many ways.
In this case I would simply consider writing functions to define button behavior. If you only have a few buttons (less than 30) I probably wouldn't waste time making a more involved factory scheme.
// Something along the lines of function SMyButton(_someAction) constructor < myAction = _someAction; static DoAction = function () < return myAction();>> // Make a new button myFirstButton = new SMyButton( function () < // do stuff >); // Invoke the button myFirstButton.DoAction();
I do not want to pass by reference so struct copy functionality is the order of the day.
From your example 'code' it looks more like you are trying to define the base attributes, or behavior, of a unit type that is the same for all units of that type. In that case you very well may want to simply have all units of that type reference a single struct (or possible all reference a struct parented from a base struct).
But if you want to struct copy then while there is no explicit struct copy function currently available; probably the easiest way to copy a struct is with json_stringify and json_parse. A bit of a round about way of doing it, but straight forward, and would get you going quickly and you could make/use a more elegant struct copy later. Biggest drawback is I believe you would lose constructor info so instanceof would just return 'struct' instead of the named constructor.
// Copy a struct aCopyOfSomeStruct = json_parse(json_stringify(someStruct));
Last edited: Feb 7, 2022
This seems like what you want to do: Have a button that its behavior is variable or data driven. While structs can certainly help do that, it can be done in many ways.
In this case I would simply consider writing functions to define button behavior. If you only have a few buttons (less than 30) I probably wouldn't waste time making a more involved factory scheme.
// Something along the lines of function SMyButton(_someAction) constructor < myAction = _someAction; static DoAction = function () < return _myAction();>> // Make a new button myFirstButton = new SMyButton( function () < // do stuff >); // Invoke the button myFirstButton.DoAction();
From your example 'code' it looks more like you are trying to define the base attributes or behavior of a unit type, that is the same for all units of that type. In that case you very well may want to simply have all units of that type reference a single struct (or possible all reference a struct parented from a base struct).
But if you want to struct copy then while there is no explicit struct copy function currently available; probably the easiest way to copy a struct is with json_stringify and json_parse. A bit of a round about way of doing it, but straight forward, and would get you going quickly and you could make/use a more elegant struct copy later. Biggest drawback is I believe you would lose constructor info so instanceof would just return 'struct' instead of the named constructor.
This is very helpful =) Thank you! I'm not sure on the exact terminology. I'm certain you're right about individual units inheriting from a parent struct, but can't see another way to easily accomplish it without the factory method I've outlined above [or at least I think that's the factory method.] Main thing is I have it working, so from here I think things should improve. Thanks again!