GML Does it matter where I Create and update variables?

NotTayyy

Member
So I Made an persistent inventory manager object and Inside it created a Data structure with all my Characters stats, Like the name, gold, Def, atk, Equipment slots, exp Etc...

Then i want to make a Variable for each one so i can update them but i dont know where i should put them.

So I Was wondering, Can i make and Update all the variables In the player object and Then the DS Would just read it from the player object and put it into the DS Grid in Inventory?
Or do I have to create the variables and update them in the Inventory object?
 

TailBit

Member
Well, if you make a enum for stats in the control object
Code:
enum Stat {HP,MP,STR} // and so on

// then an array with ds_list for each player
var i;
i = ds_list_create(); // creating a list and adding the stats for the character
i[| Stat.HP] = 10;
i[| Stat.MP] = 30;
i[| Stat.STR] = 10; // could ofc use ds_list_add(i, 10,30,10 ...) to make it a bit simpler
players[0] = i; // store it in an array 

// do the same for player[1 to 3]
so when you create the player object:
Code:
( instance_create(x,y,obj_player) ).stat = obj_control.players[0]; // then give that player a connection to its stat list
so if you now change:
Code:
stat[| Stat.HP] -= 10;
since this is the same list as in obj_control then it will change it directly
 

TsukaYuriko

☄️
Forum Staff
Moderator
You technically could create any variable anywhere (provided that it is declared under the correct scope). There is also nothing that prevents you from changing any given variable from anywhere, as long as you know the ID of the instance that owns it as well as its name.

This doesn't mean that you should, though.

Primarily, ensure that no variable exists twice. If your player has an HP variable, don't have the same variable in your HUD object to draw it there. Take it from the player. Or better yet, take it from a centralized data storage.

Secondarily, the most natural place to store data is not always the best. In the scenario above, storing the player's HP in the player's object seems like the natural thing to do. Storing it as an instance variable will work until you add multiple rooms to the game, as it will reset when entering a new room. Storing such semi-persistent data in a central place, such as a persistent object or a data structure, will make handling it much easier in the long run.


When working with data structures, it is usually a good idea to create scripts which access said data structure for you and return/update values. It's much easier to memorize and use a script with named arguments than memorizing the names and structure of all of your data structures - having scripts takes care of that for you and makes your code less prone to human errors.
 

NotTayyy

Member
You technically could create any variable anywhere (provided that it is declared under the correct scope). There is also nothing that prevents you from changing any given variable from anywhere, as long as you know the ID of the instance that owns it as well as its name.

This doesn't mean that you should, though.

Primarily, ensure that no variable exists twice. If your player has an HP variable, don't have the same variable in your HUD object to draw it there. Take it from the player. Or better yet, take it from a centralized data storage.

Secondarily, the most natural place to store data is not always the best. In the scenario above, storing the player's HP in the player's object seems like the natural thing to do. Storing it as an instance variable will work until you add multiple rooms to the game, as it will reset when entering a new room. Storing such semi-persistent data in a central place, such as a persistent object or a data structure, will make handling it much easier in the long run.


When working with data structures, it is usually a good idea to create scripts which access said data structure for you and return/update values. It's much easier to memorize and use a script with named arguments than memorizing the names and structure of all of your data structures - having scripts takes care of that for you and makes your code less prone to human errors.
I Seee, If I Want it to be persistand I Need it to be in a persistant object, putting it in player would reset the var, So I Guess I Keep it in the Inventory Object since thats Consistant, And Call on it from the player object to get the health and stuff.
Thankss
 
Top