• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Setting and Changing Global Variables in DS Grid

Let's say I have a global variable (global.var) that either equals true or false and I want to put that in a grid. If I use ds_grid_set, it's my understanding that the variable itself is not placed in the grid but rather its value is placed in the grid (true/false). So when I "get" that grid cell, I'm not accessing global.var, only what its value was when the grid was created.

Is there a good system for what I'm trying to do? I'm looking at the documentation for ds lists, but it seems like they'd have the same exact problem. I've seen some people mention reading and writing to files, but that seems a little counterintuitive, especially if I want save data to only be written when the player saves the game at a specific time, for example, and not when they are selecting an item they acquired since the last save.

Any help or redirects would be great, thanks!

EDIT: I can't try this till tomorrow night, but maybe creating a function that returns the proper global variable, and then I can change the value of whatever it returns?
 

Roldy

Member
You are correct most data types in GML are passed by value. Here is a list of data types: https://manual.yoyogames.com/#t=GameMaker_Language/GML_Overview/Data_Types.htm

However, all the DS structures are essentially passed by reference, as well as arrays. Additionally, the newest addition 'structs' are actually passed by reference.

In this way you can create a global struct and then put it into a ds_list, or ds_map etc.. and accessing it via the ds structure will access the global one. They are one and the same, a reference.

Example:
GML:
function myStruct() constructor {
    myValue = 1;
}

global.someStruct = new myStruct();

show_debug_message(string(global.someStruct)); // This will show global.someStruct.myValue is 1

myList = ds_list_create();

ds_list_add(myList, global.someStruct);

myList[| 0].myValue = 42;

show_debug_message(string(global.someStruct)); // This will show global.someStruct.myValue is now 42
However I would be curious why you would do this. Why not just make the ds structure global.

Example:

GML:
global.myList = ds_list_create();
 

Yal

šŸ§ *penguin noises*
GMC Elder
You're correct: basically everything is stored as values. (Array literals is an exception, because arrays are stored as an undocumented "reference" type under the hood)

You're kinda approaching this from the wrong way, though: with a grid or global array, you only need one variable anyway (plus a "index" that refers to the related grid cell, which you hardcode for each event that uses the grid). For instance, keeping track of collectibles with a "global.item_collected" grid, you'd give each collectible a my_flag variable, and in their Room Start event:
if(global.item_collected[my_flag]){instance_destroy()}

(and when collected you'd set this flag like so)
global.item_collected[my_flag] = true

The "variables" GUI element is pretty good for this, since you can change the value easily on a per-instance basis from the room editor.
 
I guess I didn't realize grids could just be global like that, but that basically solves it. I'll have to replace all my global variable references, but the end result should work better than having a bunch of random variables to keep track of. Thanks!
 
Top