Tutorials and Global Variables

P

PoultryKillr

Guest
Hello. So let me start by saying I am slowly learning more and more.

One of the things I have learned is the potential disaster that is Global Variables. I have been following a tutorial on RTS Turn based games. However this tutorial, while teaching me a lot, left me in a hole I can't dig myself out of.

My end goal was to be able to create characters with their own variables. Variables like how many spaces they can move specifically. More importantly each individual character will have additional abilities unique to themselves.

The set up was basically

ObjPlayer (Parent)
And
ObjChar

I'm wondering if I will be able to create unique differences between each character given these Global Variables.

EDIT: I know that I should not use tutorials to create a game in a copy/paste fasion. And it wasn't my intention. I'm just sort of stuck, because I went through all these tutorials to learn something that seems to be mutually hated by the coding community.

EDIT 2: So after further research I have found that my worry was ill placed. I can give the "child" objects their own Variables. Works well.
 
Last edited by a moderator:

RangerX

Member
First of all, I don't know where you are coming from. Global variables are just one type of variables and they aren't "better" or "worse" than any others.
A variable is a little space in memory that you reserve and give a name. In that "box" your can put some data. (numbers). That's all a variable is. What changes from a variable to another is how you can ACCESS it.
This is where you should learn about variable scope:

https://docs.yoyogames.com/source/d...01_gml language overview/variables/index.html

The manual is often a better read than most Youtube videos!
 

TheouAegis

Member
You do not use Global variables if you want instances to have their own stats and whatnot. You have to make all of the variables local to each instance. If you want an instance to interact with another's variables, you need to retrieve the ID of the particular instance you want to act with, and then you reference it's variables the same way you would do with a global variable, but use its ID instead of the global word.

E.g.:

inst= instance_position(mouse_x, mouse_y, obj_unit);
if inst != noone {
target = inst; move_toward_point(inst.x,inst.y,movespeed);
}
 
P

PoultryKillr

Guest
Perhaps I jumped the gun when I said "mutually hated". I've simply come across many commenters on both forums and YouTube that seem to have that attitude towards global variables.

Either way. I will check out this manual. I would much rather learn by reading than copy and pasting.
 
Top