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

GameMaker How to add 2 global variables

N

ndaoud360

Guest
Ok I have a game where I type in digits and it's supposed to add them together but it doesn't. I have, type 1 digit, go to next page, type 2nd digit, go to next page etc. till around 3 pages, then the 4th page will add all them together. It just takes my numbers and puts them next to each other and doesn't add them.

I have assigned digits to
Code:
global.score_P1_R1, global.score_P1_R2 and global.score_P1_R3;
and I have a final score variable which will be the added up answer as
Code:
global.final_score_P1_9H
Then I used this to add them together
Code:
global.final_score_P1_9H = global.score_P1_R1 + global.score_P1_R2 + global.score_P1_R3;
Then I use this to draw the added number to the screen
Code:
draw_set_color(c_black)
draw_set_font(font_player_text)
draw_text(214,312,global.final_score_P1_9H)
but it doesn't add them, it just draws all the numbers I typed right next to each other.
 

woods

Member
draw_set_color(c_black)
draw_set_font(font_player_text)
draw_text(214,312,global.final_score_P1_9H)


i think your error is here...
draw_text(x,y,string)



so your last line should read something like

Code:
draw_text(32,32,"your score total is..." +string(
global.final_score_P1_9H = global.score_P1_R1 + global.score_P1_R2 + global.score_P1_R3
)
// x and y are where you want the info drawn
// everything in the quotes is typed to screen as is..
// +string is the variables you want to add together


or something very close to this at least ;o) hope this helps
 

TheouAegis

Member
string( global.final_score_P1_9H = global.score_P1_R1 + global.score_P1_R2 + global.score_P1_R3)
Aside form the fact that you're missing a second closing parentheses, that code is wrong.

First off, in your case since you are concatenating "your score total is..." with the score, you do need to use string(). However the OP isn't concatenating anything, so string() isn't even necessary.
Second, that code will evaluate the line inside the string() parentheses first. And that line is NOT global.final_score_P1_9H=global.score_P1_R1+global.score_P1_R2+global.score_P1_R3, but is in fact evaluated as global.final_score_P1_9H==global.score_P1_R1+global.score_P1_R2+global.score_P1_R3, which will be either 0 or 1.

Back to @ndaoud360 . As BattleRifle said, your global.score_P1_R1, global.score_P1_R2, and global.score_P1_R3 contain strings. Somewhere in your project you stored their values either using the string() function or you put quote marks around the values. This causes + to concatenate the strings, which results in what you were seeing.

Also, you should learn to use arrays so your variables are easier to read and maintain. Right now it's simple enough because you only have 3 variables, but what if you decide to later add variable rounds or rooms (or whatever R stands for), or have variable number of players beyond just 2 players or whatever your current setup is? Arrays will make all of that much simpler. So then your variables in this scenario would be something like
for(p=0;p<players;p++)
{

global.final_score[p] = 0;
for(r=0;r<rounds;r++)

global.final_score[p]+=global.score[p,r];
}
 
Ok I have a game where I type in digits and it's supposed to add them together but it doesn't. I have, type 1 digit, go to next page, type 2nd digit, go to next page etc. till around 3 pages, then the 4th page will add all them together. It just takes my numbers and puts them next to each other and doesn't add them.

I have assigned digits to
Code:
global.score_P1_R1, global.score_P1_R2 and global.score_P1_R3;
and I have a final score variable which will be the added up answer as
Code:
global.final_score_P1_9H
Then I used this to add them together
Code:
global.final_score_P1_9H = global.score_P1_R1 + global.score_P1_R2 + global.score_P1_R3;
Then I use this to draw the added number to the screen
Code:
draw_set_color(c_black)
draw_set_font(font_player_text)
draw_text(214,312,global.final_score_P1_9H)
but it doesn't add them, it just draws all the numbers I typed right next to each other.
That's very simple confusion between String and Reals,
see in your code, the global.score_P~_R~ are not real they are strings,
when you add strings for example,
Code:
var a = "apple";
var b = "is";
var c = "a";
var d = "fruit";

var e = a + b + c + d;//result will be e = appleisafruit;
here

when you add strings for example,
Code:
var a = 2;
var b = 1;
var c = 12;
var d = 2;

var e = a + b + c + d; //result will be 17
what you can do is convert the string into real values using real() function
Code:
global.final_score_P1_9H = real(global.score_P1_R1) + real(global.score_P1_R2) + real(global.score_P1_R3);
and convert it to string when you have to print it using string() function;
Code:
draw_set_color(c_black)
draw_set_font(font_player_text)
draw_text(214,312,string(global.final_score_P1_9H));
make sure global.final_score_P1_9H is always real, by initializing it's initial value to 0 or some real number and not anything enclosed under " ";
Hope it helps!
 
N

ndaoud360

Guest
what you can do is convert the string into real values using real() function
Code:
global.final_score_P1_9H = real(global.score_P1_R1) + real(global.score_P1_R2) + real(global.score_P1_R3);
The real is all I needed to fix it, thanks! Also I didn't realize "" was a real verses a string.
 
Top