• 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!

GML Instance Level Global Change or Change by Function

G

GOD-sSs-END

Guest
Here's a fun question for you gurus out there: At the instance level, is it better to change a global variable directly or via function when possible? Or is there no difference. Here's the scenario.

I have a button that, when clicked, hides a layer by changing the value of global.ins_panel from false to true, declared in the game controller in my game-initializing room. I can do this two ways:

1) directly using global.ins_panel = true;
2) or by function using layer_set_visible(global.ins_panel, true);

Option 2 requires global.ins_panel = layer_get_id("ins_panel");

Is one option better than the other in any way(s) at all???
 
I wouldn't quite do either exactly.

I would create my own script, called for example "scr_show_panel( _showPanel)"

Then, in the instance, I would call that script whenever I wanted to show or hide the layer/panel:

Code:
scr_show_panel( true); // Show panel

scr_show_panel( false); // Hide panel
scr_show_panel:

Code:
/// @arg _showPanel (bool)

var _showPanel = argument0;

layer_set_visible("ins_panel", _showPanel);
Also, your examples are a bit fuzzy to me - option one requires your global variable to be boolean value, with no mention of how you are using that boolean to change the layer visibility. I'm guessing with option 1, you would *still* have to call layer_set_visible() function somewhere, so its double the trouble. option 2 you are setting it to a layer_id at the start of the game, and still calling layer_set_visible explicitly in your example.

So both ways take about the same amount of time and code.

So my final answer is
1) Abstract the code to a script.
2) Call the script from the instance.
3) Later, if you find a better way of doing it, you only need to change the script once to update it for all places where you used it.

Let me know if that's what you were after, I'm not 100% sure what answer you were looking for.
 
Top