I try to avoid global variables as much as possible. Change my mind.

I like my objects to be poked and responded to from the outside. I try to avoid global variables as much as possible because it seems messy. Sometimes this is unavoidable(ish).

I don't like using globals inside an object. But if I have to use a global this is the way I do it now. (If I really, really have to). Every time I see a global in my code it merely serves as a flag telling me I have seriously miss-understood the logic of the situation and should review that again at a later time. If I can avoid it I will avoid it.

Globals should be put into room 101. Yes, No, Maybe so?
 
To answer your question about why I want my mind changed. I always want my mind changed because I am always wrong. Always, every time.

As far as my question goes, I like my objects small. (As small as possible units).

  • A square has four sides. It always has four sides, it will never have more than four sides.
  • A square has four lines that can be set to a number of colours (or a mixture of)
  • A square is either filled or not filled (with one or many colours)
  • A square always has the same length lines on all sides

etc... etc..

If I want a square ill call the function that draws a square, give it all the data required to produce a square. I get a square. At what point would I ever need to use a global.

I am absolutely atrocious at trying to explain what I'm thinking to others in a way that they understand where I am coming from. (That's entirely my fault).


I did search google to effectively confirm my own echo chamber however It occurred to me before I made this search. Not good science I know. But it kind of just occurred to me that this was the case independent of search results.

I may have overstated my concern. I'm not saying or at least did not mean to imply that globals where useless but more that the propensity to use them is way larger than should be acceptable. 'Only use them when you absolutely have to'.

If you have lots of globals you would tend to copy the data from them to the individual object variables in any case. If you could give me a scenario where a global is the best option. I don't know it's more of a deep feeling that something is not right in the world of global variables.

I hope this makes some sense. I struggle with forums, to be honest. Since I started using Gamemaker the last time I used any kind of forum or chat was in the '90s so please bare with. There is more in my mind than I can type or others have the time to read. I do apologize. I just don't know any better.

I'm not sure there is an actual answer to this question. I do remember seeing a post someone made about de-compiling and everyone lost here blob. Don't want that again but that's the best I can describe my thoughts on this. UG!

Edit: sorry for the spelling mistakes and terrible grammar. I blame google, Grammarly, my poor keyboard, and most importantly a lack of care on my part. It's not acceptable I know I'm working on it.
 
Last edited:
Well, to put in a counterpoint, I believe that globals are the fastest variables to be accessed. Them or local, lol, can't remember which. But they're faster than instance variables and especially dot accessing (my_inst.variable). So, for instance, if I have data stored in a map that each instance might want to be accessing data from, I'll use a global variable to hold that map, rather than a controller object or storing the pointer to the map in an instance variable for each instance.

I'm not entirely sure what the question really is though. Use global variables when you should, use instance variables when you should and use local variables when you should. There's not really too much in the way of "one should always be using X type of variable". Use them when it makes sense to use them.
 
I did come across too heavy with the never ever kind of philosophy. Would you agree that they are overused? I use them. But every time I do I ask myself the question 'Is there no possible another way'. (That would make sense, there is always another way).

You do live up to your name. I do feel refreshed. I'm going to grab my own refresher towel right now. Thank you. I spend way to much time worrying about absolute truth than I do actually making things happen.
 

Yal

🐧 *penguin noises*
GMC Elder
Games are different from almost all other types of applications in that they're inherently defined by their state and not the data they operate on, so it makes sense that having that state in a single, easily accessible container is a desirable thing (don't forget that games also want things to be as efficient as possible so they can have both nice effects and real-time response times without lag). Global data is the perfect answer to that.

In fact, GM's global glue goes beyond the global keyword itself - there's global access from any object to any other object whenever you need it! with loops, event_perform_object, and all built-in-functions (and script-scope function variables) being globally scoped gives you an enormous freedom in gluing things together in whatever way's practical. I've tried a whole bunch of engines, and I've always bounced back after realizing how much ****ing boilerplate you need to do for something as simple as just reading a variable. I actively hate Java for making people thing you get prettier code if you need to write everything four times (data type, variable name, getter/setter and user-side variable) and GM's inherent spaghetti capacity is perfect when you care more about getting **** done than about patting yourself on the back for living up to your own 10 code-grammar-nazi commandments.
 

kburkhart84

Firehammer Games
This is indeed an echo chamber...most of us that have ever used anything outside of GML for programming will agree. However, I DON'T fully agree. They have their purposes. GML in general is a language designed to make the specific purpose of game creation easier. Why not use the features it has where they make sense? I agree you should not just use them anywhere and everywhere, as most functionality should be self-contained in objects that handle their one task(and one task only generally). But sometimes, it makes more sense to make something global instead of store it in an object if it is going to be accessed by multiple objects all over the place.
 
Lol. Grammar-nazi was probably a little strong. But you are ultimately correct. I need to take a chill pill effectively. I hear you. I just can't let go.
 

ThraxxMedia

Member
Just ask yourself how much information your game needs to store and share. Example: most people probably have at least one single core game loop object that's persistent (if not... they should). If most of what you manage stays in there, you're perfectly fine with local variables. A global should always have a MEANING of sorts... i.e. a reason to exist, that explicitly tells you why it can't exist in any other format.

As a more specific example: difficulty. This is a good thing to keep as a global, because you're pretty much accessing it EVERYWHERE.
From calculating damage to defining how many enemies should spawn to how much damage your own weapon does - this is a thing you wanna keep track of.
 
I don't (unfortunately) think of things as a whole. For example, I imagine the most obvious things that most games would need.

  • I come up with a menu system generator (Rather than just slapping three or four objects in a row with images attached to them)
  • Instead of writing a whole game and finding that I need a high score table. (That's a project all in itself)
  • I need some eh 'windows' controls so I make a checkbox, textbox, scrollbar. (That's a project)
The idea is that when I want to make any kind of game, I effectively just go down a massive list of variables and bobs my uncle.

I strayed away from the global variables there a little. The connection is that I will use them but just not for individual bits and pieces.

Immagne I have complete all of my 'bits and pieces'. Then the globals come in. Otherwise, you get too interwoven and can't see the woods through the trees.
 

kburkhart84

Firehammer Games
As a more specific example: difficulty. This is a good thing to keep as a global, because you're pretty much accessing it EVERYWHERE.
From calculating damage to defining how many enemies should spawn to how much damage your own weapon does - this is a thing you wanna keep track of.
I agree and this is the kind of thing where I DO go ahead and use globals for the convenience. That said, it is always possible to have an object that controls the overall game state, etc... and this object could easily store the difficulty. You could also even have functions to directly access that variable. Of course, at that point you might as well have just used the damh global....but the options are there. That's the good part about all this, we can do things however we prefer.
 
I feel like Alice in wonderland sometimes and at others Willy Wonka in the chocolate factory. I'm slowly starting to twig. It's taking a while.
 
Z

zendraw

Guest
if you know your wrong, why ask? experiment, experience, and try find whats theyr usefulness. programming is not a debate, this is practice, not politics. if you are political then you need to do somthing with your life. perhaps your mentally frustrated and projecting on programming. id use only globals if i didnt have to type global.variable... ofcorse id use local variables for more local stuff.
 

chamaeleon

Member
If you use more than one instance for holding "global" data, you get a kind of namespacing of the data. If you enter the debugger to inspect values the data could be organized to belong to groups of variables that relate to one another in some way. Whether this is worthwhile enough depends on your code, I guess.
 

Rob

Member
I think some people will use a lot of globals because it's the first way they discover that's "easiest" to access from all over objects. I also think most people will have had a bad experience from using too many globals at one point or another, and so now try to avoid using "too many".

If you can design your code before you get too deep into it then you'll probably already know when you need a global or not.

All I know is RPG's should NOT just depend on global variables!
 
Maybe all global variables could be dispensed with in 2.3 because of the new keyword static. For example you may be used to setting up a global score variable to keep score, this is convenient because the score needs updating from several objects. But now you can have a score function where the score is set to 0 using a static variable at the start of the game and any object can change the score using this function.
GML:
function my_score(n){
    static the_score = 0;
    return the_score += n;
}
 
Last edited:

kburkhart84

Firehammer Games
Yes, static can be used as a replacement workaround for global. Simply using an instance variable on an object you only have one instance of along with dot syntax can also work.
 
You are correct. I'm just struggling with 'it'. The other guys have also very valid maybe more specific points. But yes I do need to work on that aspect. I am trying. Just too many variables to consider sometimes.

The 'change my mind' thing was supposed to be light-hearted.
 
Last edited:
Z

zendraw

Guest
I think some people will use a lot of globals because it's the first way they discover that's "easiest" to access from all over objects. I also think most people will have had a bad experience from using too many globals at one point or another, and so now try to avoid using "too many".

If you can design your code before you get too deep into it then you'll probably already know when you need a global or not.

All I know is RPG's should NOT just depend on global variables!
what bad experiences are we talking about here? sure they use more memory or whatever.
 
Z

zendraw

Guest
if we change his mind, and hes aways wrong, he was wrong from the very start changing his mind.
zen, you are alredy what you are.
 

Khao

Member
if we change his mind, and hes aways wrong, he was wrong from the very start changing his mind.
zen, you are alredy what you are.
Not to mention.

If you're always wrong.

And you think you're always wrong.

You're always right.
 
S

Sybok

Guest
Well if a variable is going to be used everywhere throughout an application, why not use globals? If not, then why use them?

Every tool has it's purpose. It's up to you how you use the tools. As @hippyman said, no one really cares in the end. 🤣

Do what you want to do. I'm not going to make you change your mind.
 

Rob

Member
what bad experiences are we talking about here? sure they use more memory or whatever.
Like... coding a stat-heavy game using just globals instead of arrays/enums/data structures etc. I've seen a game where there are a dozen heroes, all having their own global stats, and the game is a dungeon crawler. It's 12x the code and makes no use of any of the functions that would make the job much easier.
 
Thanks, guys. It's more of a psychological barrier than anything. I'm so anal about getting everything perfect it takes me 5x as long as it should do anything. To the point, I comment on every single line and try to justify everything.

This method does tend to pay off for large projects but for tiny ones, it probably is a waste of time to worry about these things. I also tend to find I learn more if I take time to do it right. Software Bit rot is my worst nightmare.
 
Z

zendraw

Guest
Like... coding a stat-heavy game using just globals instead of arrays/enums/data structures etc. I've seen a game where there are a dozen heroes, all having their own global stats, and the game is a dungeon crawler. It's 12x the code and makes no use of any of the functions that would make the job much easier.
thats on the programmer, not on the globals, you cant say globals are bad when the programmer is an idiot. i thou use macros for states, which are also shared betwean all the characters, like everyone has idle, move, hurt, die states. altho i also use a single parent for them with local variables for the statees.
 
I do use them. This was mearly a thought that occerured to me. I just felt like somthing was wrong and I did not need so many. I'm not going to be hard coreabout it but every time I use one I'm asking myself do I really need it. I don't feel the same way about other variables. I'm asking have I got enough variables at that point.
 

Gamebot

Member
I tend to use macros in place of globals and enums. Even though GMS supports the pointer ( var.value ) I find that using globals in certain situations make it a lot easier for me to remember those important globals.
 
Z

zendraw

Guest
macros are for constants, globals are for variables. thats the basic rule. so when i need a variable that is seen by all, i ask if this is a constant or a variable value. you can also put irandom(359); as a constant/macro.
 
I had a little of this confusion between macros and constants a few weeks back. The deal where it's constant but pre-calculated once. What do you call a true constant in game maker? Maybe the built-in ones are true constants I guess.
 
Last edited:
This should be in the Community Chat section. Also no offense but this isn't Reddit. Nobody here really cares about how we individually decide to program something.
Sorry, I don't use Reddit. It's not my intention. Stop trying to discover people's intentions. I try my best not to, but if My question insults a person's sensibilities but I learn something afterward. (I don't care).

If it's supposed to be in community chat the forum admins will move it. It became community chat. It didn't start off that way or was not intended that way.
 

kburkhart84

Firehammer Games
I had a little of this confusion between macros and constants a few weeks back. The deal where it's constant but pre-calculated once. What do you call a true constant in game maker? Maybe the built-in ones are true constants I guess.
A macro is the closest thing to a "real constant" that GML has the way I understand it. It isn't really a constant as its basically a letter for letter text code replacement, which is more useful than constants if you put something like a function call. But if you only put a number or a string(or a true/false), then it basically serves as a constant, and that's how I use them all over the place. I don't need GML to enforce that it actually be a traditional constant, but putting a constant there makes it one anyway. Then, if I DO want something that is calculated multiple times, I just put that in the code(exception being I may shorten function names using macros).
 

hippyman

Member
I may be wrong about this but I believe I read somewhere that macros/constants depend on what you put in them. I could be totally wrong. But iirc it's a macro when you put in some kind of function and it's a constant when you only put a literal value.
 
Ah, ok, that's interesting. You're saying that game maker does have 'true' constants but it just doesn't tell you what it's doing.

#macro test 0
#macro test 10 - 10

It looks like the same result but when 'compiled', one is treated well and the other not.
 

Nidoking

Member
A macro is text replacement at the compiler level. A constant is an identifier that can be used like a variable but whose value can't be changed at runtime. self, noone, and all are constants. The distinction is obvious in cases like the above, where you put a calculation into a macro. I can't think of any cases in GML where it would be important to use a variable rather than a macro, but in other languages, it's more of an issue. In cases where you pass a function as an argument to another function, for example, you couldn't pass a function-style macro because there's no identifier, and it doesn't exist anywhere in memory as a function.
 

Nidoking

Member
That's probably a good summary. There are many ways to do almost everything, and some ways are better than others, but if what you do works, then it's usually good enough. When you're skilled enough with the program to understand all of the concepts, then you'll start to work out the improvements for yourself.
 
That is exactly what happens. I just see problems a certain way, way ahead of time. Then I slow down. This process might happen before I write even one line of code.

I'll give you a really simple, silly example.

Client: I want a box that says "Hello world".;

Me: Ok Gamemaker show_debug_message("Hello world!");

Client: I can't see anything on the screen.;

Me: Sorry. Hello Gamemaker show_message("Hello world!");

Client: Cool I can see it now but can you take the '!' out so it just states "Hello world".

Me: Ok Gamemaker show_messae("Hello world");

Client: Can you make the text red;

Me (To the forum): I have a guy that wants red text in a textbox that I can't access and the only Gamemaker knows how to do and I have to program my own textbox guys because this guy wants red text.

Other forum members: Sorry dude, program your own text box. GMS knows how to talk to windows WPF and whatnot but you're on your own buddy.

Me to the client again: Sorry had a few technical issues it will be with you next week.

Client: Really it's going to take a week to make the text red.

Me: Well not exactly it's hard to explain.

Me actually programming: Well I can't access anything unless game maker lets me and it is a gaming platform after all. OK What variables do I need?

  1. The size of the messagebox (Width, height)
  2. What about scale Ill add that
  3. What if they don't like the sprie ill put that in a variable NP
  4. How am I going to catch an argument while pausing the rest of the code because it's not a textbox anymore.
  5. This is taking a long tme for what it needs shall I cheat. (I can cheat, can't I?)
  6. Screw it you can have a box with a mouse detection field and I'll hold the code with fudge because you want it now and It shouldn't be that far fetched, message boxes have been available for years why are you fighting me Gamemaker.
  7. I'm going to bed.
Client: Sorry this is taking way too long it's not good for me I only asked you to make the text read on the message box you already created (Which I didn't).

Me thoughts: Your right I've failed and it's dumb because it easy but it takes some more time to do certain types of tasks in game maker.

I don't know if you can use the show message functions and pass them more arguments than what I seem to be claiming there. It was just a simple example. If I went into details about what I'm coding it would convolute the situation and nobody would understand where I'm coming from at all.

I do understand where people are coming from if I understand the sentiment correctly. If I ask the time I don't need a lecture on atomic theory and history. But if you have a watch with a missing hand, it's a problem.

Skim over any grammatical errors. I'm too tired for that now. Sorry. :)
 
Last edited:

Rob

Member
thats on the programmer, not on the globals, you cant say globals are bad when the programmer is an idiot. i thou use macros for states, which are also shared betwean all the characters, like everyone has idle, move, hurt, die states. altho i also use a single parent for them with local variables for the statees.
I usually have a couple of global variables (in the entire game), but I would not have actors each using their own global variables.
Using csv files, data structures, Enums and state systems makes life much easier.
I don't know what point you're trying to make but if its global variables > data structures/arrays for stat-hvy games then... I have no words.
 
Last edited:
Z

zendraw

Guest
as i said, i have every actor use 1 macro as a pointer to a state or somthing, the use i make of global variables and constants is to have every instance accessing them easily without worrying if an instance exists at that point or not. there is no point, globals have theyr use .
 

Rob

Member
What about when you want to iterate through the stats or use a script for damage? I'd use a for loop and loop through the array/data_structure (to iterate through the stats) or use an enum to point to the right place in that actor's array/data_structure so that everyone can make use of the same code regardless of what's going on. I always found doing that unmanageable with global variables, especially with a decent amount of stats.

An example of when I would use a for loop is when I'm starting a battle and the game picks random enemies. Each enemy gets an array based on the base stats that are stored in a ds_grid. A for loop assigns all the entries in the array and it would work for any enemy. The same happens for the heroes (although which heroes are participating is NOT random, but chosen by the player).

An example of a damage script could be:
GML:
var damage = attacker.a_stats[e_stats.attack] - defender.a_stats[e_stats.defence];
The attacker can be a player or AI and vice versa for the defender. I don't see how I can do the same with only global variables.
 
Last edited:
What about when you want to iterate through the stats or use a script for damage? I'd use a for loop and loop through the array/data_structure (to iterate through the stats) or use an enum to point to the right place in that actor's array/data_structure so that everyone can make use of the same code regardless of what's going on. I always found doing that unmanageable with global variables, especially with a decent amount of stats.

An example of when I would use a for loop is when I'm starting a battle and the game picks random enemies. Each enemy gets an array based on the base stats that are stored in a ds_grid. A for loop assigns all the entries in the array and it would work for any enemy. The same happens for the heroes (although who is participating is NOT random, but chosen by the player).

An example of a damage script could be:
GML:
var damage = attacker.a_stats[e_stats.attack] - defender.a_stats[e_stats.defence];
The attacker can be a player or AI and vice versa for the defender.
This is exactly what I do except I store stats in a ds_list instead of an array--ds_list_copy has too many handy use cases for me to consider using an array. It's absolutely fantastic. Simple to code for and easy to add new stats. One big benefit is that all units, player or enemy are the same object: objUnit. The only difference between players and enemies is a single boolean variable. Storing variables like HP, Attack, Defense, etc. in separate global variables is a bit of a ridiculous thought to me. Why would it be global? How do you differentiate between player units in that global variable? Is it a global array? If that's the case, why not just make something like an obj_player_controller just to store these variables? GMS2.3 makes this even easier with lightweight objects, because you can store the data for individual characters in their own lightweight objects referenced by the controller in an way that's easy to read: obj_player_controller.John.stat[|Stat.hp]. If your only issue is "it takes a long time to write out obj_player_controller. compared to global." then why not make a macro? This works great:
GML:
#macro PLAYER obj_player_controller
PLAYER.John.stat[| Stat.hp] = 500;
 
Last edited:
  • Like
Reactions: Rob

Rob

Member
This is exactly what I do except I store stats in a ds_list instead of an array--ds_list_copy has too many handy use cases for me to consider using an array. It's absolutely fantastic. Simple to code for and easy to add new stats. One big benefit is that all units, player or enemy are the same object: objUnit. The only difference between players and enemies is a single boolean variable. Storing variables like HP, Attack, Defense, etc. in separate global variables is a bit of a ridiculous thought to me. Why would it be global? How do you differentiate between player units in that global variable? Is it a global array? If that's the case, why not just make something like an obj_player_controller just to store these variables? GMS2.3 makes this even easier with lightweight objects, because you can store the data for individual characters in their own lightweight objects referenced by the controller in an way that's easy to read: obj_player_controller.John.stat[|Stat.hp]. If your only issue is "it takes a long time to write out obj_player_controller. compared to global." then why not make a macro? This works great:
GML:
#macro PLAYER obj_player_controller
PLAYER.John.stat[| Stat.hp] = 500;
I love lists too and I use them whenever possible - especially if I want to externally save the information. I don't ever need to save the battle data for actors so arrays are good enough for me in that situation :)
 
Z

zendraw

Guest
actor.damage ... you will alredy be accessing the object, so obviosly the shortest way is to access the actor`s variables... you can set that actors variables from your list or wherevere is convenient to you.

you avoid global variables becouse performance? id guess? yet you make lists that you constantly refrence which is even worse for performance. you do realise objects are pretty much lists of variables right? and every actor can have the same variables, modified in a MOD script, depending on the actor.
 
I just wanted to apologize to anyone on the forum if I come over a little abrasive. Just stressed at the moment. Sorry guys and thanks for your help. It is well received.
 

Yal

🐧 *penguin noises*
GMC Elder
I think some people will use a lot of globals because it's the first way they discover that's "easiest" to access from all over objects. I also think most people will have had a bad experience from using too many globals at one point or another, and so now try to avoid using "too many".
I've never been in a situation where I've found myself having too many variables... but I've cursed myself for having too many hardcoded magic numbers way too many times. (And let's just say I learned about using arrays to make batch operations on large chunks of data easier way too late - it's easy to get seduced by long lists of say, comprehensible stat names without realizing it'd make more sense to streamline it into an array because you'll do the same few operations on them over and over anyway)
 
Top