• 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)global variable issues

A

Adjud

Guest
Hi, I haven't used game maker since GM7, so I have a lot to catch up on. my problem is my game won't execute when I set global variables, this is the objcontroller create code I am using:

Code:
instance_create_depth(mouse_x,mouse_y,0,objcursor);
global.str = 5;
global.dex=5;
global.mag=5;
global.hp=20;
global.mp=20;
global.mdef=0;
global.def=0;
global.lvl=1;
global.exp=0;
draw_text(x=120,y=318,global.str);
draw_text(x=150,y=500,"Experience: " + global.exp);
draw_text(x=20,y=500, "Lvl: " + global.lvl);
instance_create_depth(x=94,y=318,0,objbutstr);
it keeps telling variable reference only once, then when I try to play the game it gives me the error in the screen shot.
 

Attachments

It has nothing to do with your global variables. It is that you cannot put x=120, y=318 etc. inside your function calls. If you scrolled up through the Output window you would probably see that it is throwing errors on all the draw_text and instance_create_depth lines.
These:
Code:
draw_text(x=120,y=318,global.str);
draw_text(x=150,y=500,"Experience: " + global.exp);
draw_text(x=20,y=500, "Lvl: " + global.lvl);
instance_create_depth(x=94,y=318,0,objbutstr);
should be
Code:
draw_text(120,318,global.str);
draw_text(150,500,"Experience: " + global.exp);
draw_text(20,500, "Lvl: " + global.lvl);
instance_create_depth(94,318,0,objbutstr);
 
A

Adjud

Guest
its still giving me the same error, displayed in the image below:

Code:
instance_create_depth(mouse_x,mouse_y,0,objcursor);
global.str = 5;
global.dex=5;
global.mag=5;
global.hp=20;
global.mp=20;
global.mdef=0;
global.def=0;
global.lvl=1;
global.exp=0;
draw_text(120,318,global.str);
draw_text(150,500,"Experience: " + global.exp);
draw_text(20,500, "Lvl: " + global.lvl);
instance_create_depth(94,318,0,objbutstr);
 

Attachments

its still giving me the same error, displayed in the image below:

Code:
instance_create_depth(mouse_x,mouse_y,0,objcursor);
global.str = 5;
global.dex=5;
global.mag=5;
global.hp=20;
global.mp=20;
global.mdef=0;
global.def=0;
global.lvl=1;
global.exp=0;
draw_text(120,318,global.str);
draw_text(150,500,"Experience: " + global.exp);
draw_text(20,500, "Lvl: " + global.lvl);
instance_create_depth(94,318,0,objbutstr);
And have you done what it is telling you to do? Have you read the whole of the Output window and the Compile window for the errors? Just looking at the last few lines is not enough for you to find the problems, but I also suspect it is still the draw_text lines as you are trying to draw a number and appending those to the text "Experience" and "Lvl", which means you have to first convert the numbers to strings, otherwise GMS will try to do a mathematical add because you are using numbers. So you would need to wrap all of the numbers with string():
Code:
draw_text(150,500,"Experience: " + string(global.exp));
draw_text(20,500, "Lvl: " + string(global.lvl));
 
The yellow errors are not errors. All they mean is that the variables are being created but are not being used anywhere else. It's just a cautionary alert for development, nothing more, and will not stop your game from running.

It has nothing to do with your global variables. It is that you cannot put x=120, y=318 etc. inside your function calls. If you scrolled up through the Output window you would probably see that it is throwing errors on all the draw_text and instance_create_depth lines.
Well, you actually can do so, but all that will happen is that they'll be considered comparisons and will return either 0 or 1. There won't be any errors in the Output window.
 
A

Adjud

Guest
I'm really frustrated.... I used to be really good at programming but this is still not working, now the error is directly connected to line 13 which is setting the variable "global.exp"



upload_2020-1-13_0-27-30.png
 
It's easy to miss, but if you take away the "global." from global.exp, you'll see it turn orange (using default colours). exp() is a function. Choose a different name for your variable.
 
A

Adjud

Guest
well that fixed it, but theres no text displayed in the room, I tried changing the backround color and reloading, it displays nothing even if I put
Code:
draw_text(22,22,"hello")
it displays nothing..
sorry for the stupid questions....

on a side note is there a book for the new gml? I seen books on game maker programming in the old language but not sure about the new changes.[/code]
 

TsukaYuriko

☄️
Forum Staff
Moderator
You are attempting to draw text in a Create event. Use a Draw event if you want to draw to the game window, as the room background will otherwise overwrite anything you draw to the game window.
 

samspade

Member
well that fixed it, but theres no text displayed in the room, I tried changing the backround color and reloading, it displays nothing even if I put
Code:
draw_text(22,22,"hello")
it displays nothing..
sorry for the stupid questions....

on a side note is there a book for the new gml? I seen books on game maker programming in the old language but not sure about the new changes.[/code]
I'm not aware of any books for GMS 2, though the newest ones for GMS 1 aren't that far off. But with the (hopefully) soon updates in 2.3 there will be significant changes making those more out of date.

However, there's always the manual. These two sections are a pretty good overview:
There are also these YouTube tutorials:
 
A

Adjud

Guest
i'm having another issue, I created an object called "objslime" I placed it in the room with no code but when I test my game the sprite of "objslime" is my "objcursor" sprite.
why would the sprite change for no reason?

edit: the uploaded image shows the circled sword graphic which is supposed to be a slime. both sprites have different names, I didn't add a mask or change anything other than picking the sprite for both objects. Even if I change my cursor sprite to the slime sprite it still stays as the cursor sprite, like the slime sprite is masked by the cursor sprite, but I didn't set anything like that

Edit2: well I created another sprite in gamemaker, then deleted the sprite and the problem resolved itself? might be a bug in game maker?

either way its solved.... sorry
 

Attachments

Last edited by a moderator:
Top