• 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 Show difference in variable gain after level?

S

Stratos.la

Guest
Say i have a global gold variable that is saved. and mid-level killing monsters gives gold, how can i show how much the player earned in that level particular? i have tried it like so but it doesn't work.

Right now is in the create event. i tried putting it in begin step and end step but still it shows 0. i also tried adding the difference, still nothing
GML:
previous_gold = global.gold;
xp = global.p_exp
gold = global.gold
previous_xp = global.p_exp
text = "gold : " + string(gold - previous_gold);
text1 = "Exp : " + string(xp - previous_xp);
 

saffeine

Member
it shows 0 because you're setting the previous variables to the same value as the current values. by subtracting one from the other, you'll only ever get 0.

GML:
previous_gold = global.gold; // if this is 100...
gold = global.gold; // ... then this is also 100.

// same applies here.
previous_xp = global.p_exp;
xp = global.p_exp;
since this is all happening in one event, the difference is only ever going to be 0, and the current variables and text will never change.
move the previous_ variables to the create event / the event that runs at the start of each new level, and move everything else to the step event.
by doing this, you set the previous_ variables to the last known value at the time of the level starting, and update the other values every step, allowing there to be a difference.
 

TheouAegis

Member
You need to set update gold and xp whenever... whenever gold and xp get updated. You need to update text and text1 in the End Step event.
 
S

Stratos.la

Guest
xp gets updated 2 ways. by an alarm every 5 seconds giving 2 and after each kill. gold is calculated from a range of 1 to 10 after each kill. so i should put the text variable in the step event and leave the create event with the declared values and the draw well as it is to draw? same code right? i should subtract?
 
S

Stratos.la

Guest
the thing is that the object that displays that is not created in the course of the game. it gets created after the level is complete. so thinking about it now yea it makes sence. should i write the values in an object available throughout the level until the end?
 

saffeine

Member
xp gets updated 2 ways. by an alarm every 5 seconds giving 2 and after each kill. gold is calculated from a range of 1 to 10 after each kill. so i should put the text variable in the step event and leave the create event with the declared values and the draw well as it is to draw? same code right? i should subtract?
yes, you just have to move the code i mentioned to the right places.

create event / level start
GML:
/*
    putting this in the starting event sets the variables
    once to whatever the value is at the time.
*/

previous_gold = global.gold;
previous_xp = global.p_exp;
step event / draw event / some event that's run regularly
GML:
/*
    these values need to be updated as often as they are changed.
    xp and gold need to be updated to be whatever their current
    values are, and the text variables need to be updated appropriately.
   
    additionally, the difference needs to be calculated every time
    the variable changes, or at least whenever you want them to
    update on the screen.
*/

xp = global.p_exp;
gold = global.gold;
text = "gold : " + string(gold - previous_gold);
text1 = "Exp : " + string(xp - previous_xp);

update based on your followup question:
you can handle it any way you like, you just need to be mindful that the previous_ variables should only be updated where you want to start comparing from.
the way you're doing it now should still be fine, it's just the fact that you were updating everything in the same event and causing the values to be the same.
if you only want the object to appear once at the end of the level and then get destroyed, you could also do something like this instead:

GML:
// get the current exp and gold values.
xp = global.p_exp;
gold = global.gold;

// calculate the difference and update the text.
text = "gold : " + string(gold - previous_gold);
text1 = "Exp : " + string(xp - previous_xp);

// once the values have already been calculated, update the previous ones.
// this has to be done afterwards so the values aren't the same BEFORE updating the text.
previous_xp = global.p_exp;
previous_gold = global.gold;
 
Last edited:

saffeine

Member
i don't know if you caught that last addition because i was editing it in the moment you were replying, but i think that might be what you're looking for.
by doing it that way, you can contain it all in a single event at the end, just make sure you set the previous values to 0 when the game starts, or you'll have undefined variable errors.

you might have to use global variables for the previous values though if the object is getting destroyed, or it won't even matter.
 
S

Stratos.la

Guest
im trying to get it to work but it doesn't draw on the screen. pfff . i tried placing the variables on an object active in the room untill the LevelEnd objects spawns. and then in thae LevelEnd object draw event but it doesnt draw it and funny enough my exp bar works fine but the global.p_exptolevel doesnt change the value. at the first room i have an object that sets up everything as it is. then at each room creation event i spawn on a different layer the object that distributes exp /gold leveling up etc . i think i lost track of what im doing!
GML:
if global.p_exp >= global.p_exptolevel
{
    draw_text(x-150,y-150,"Level up!" + "" + "level:" + string(global.p_level))
}
draw_text(x-120,y+50,"Remaining to level up : " + string(global.p_exptolevel))
draw_rectangle_color(x-150,y+90,x+155,y+100,c_black,c_black,c_black,c_black,false)
draw_rectangle_color(x-150,y+90,x-150+(global.p_exp / global.p_exptolevel*300),y+100,c_blue,c_blue,c_blue,c_blue,false)

with (oLevelEndStats)
{
draw_text(x-150,y-190,text)
draw_text(x,y,text)
draw_text(x-150,y-170,text1)
}
 

saffeine

Member
im trying to get it to work but it doesn't draw on the screen. pfff . i tried placing the variables on an object active in the room untill the LevelEnd objects spawns. and then in thae LevelEnd object draw event but it doesnt draw it and funny enough my exp bar works fine but the global.p_exptolevel doesnt change the value. at the first room i have an object that sets up everything as it is. then at each room creation event i spawn on a different layer the object that distributes exp /gold leveling up etc . i think i lost track of what im doing!
GML:
if global.p_exp >= global.p_exptolevel
{
    draw_text(x-150,y-150,"Level up!" + "" + "level:" + string(global.p_level))
}
draw_text(x-120,y+50,"Remaining to level up : " + string(global.p_exptolevel))
draw_rectangle_color(x-150,y+90,x+155,y+100,c_black,c_black,c_black,c_black,false)
draw_rectangle_color(x-150,y+90,x-150+(global.p_exp / global.p_exptolevel*300),y+100,c_blue,c_blue,c_blue,c_blue,false)

with (oLevelEndStats)
{
draw_text(x-150,y-190,text)
draw_text(x,y,text)
draw_text(x-150,y-170,text1)
}
in your with statement you're using x and y, which i assume you're doing as if they're belonging to the object that calls it.
try using other.x and other.y, see if it makes any difference.
 
S

Stratos.la

Guest
no no im silly it works fine ( the exp to next level ) since im drawing a stable variable and not the difference between the current xp and that. pfff i think ive fried my brain. but still it doesnt draw the gained exp and gold. any ideas?? it could be something very simple but i just cant see it
 
S

Stratos.la

Guest
in your with statement you're using x and y, which i assume you're doing as if they're belonging to the object that calls it.
try using other.x and other.y, see if it makes any difference.
that did it but im a mess i had forgotten to place the object in the room. pff maybe i need a break! thank you nonetheless you've been great!
 
Top