If global.var increases , increase another

S

Stratos.la

Guest
i am making a small leveling up system and i have made it so far, so i said why not code the enemies every time the player levels up to level up themselves! thought it could be easy but i am having trouble making it happen. i have set a global variable for the level (of the player) and i cant detect it in the enemy create event. Something along the lines of
Code:
if global.level += 1
{
    //do enemy stuff
}
which of cource doesnt work and i get an error so i tried saving the global variable in another variable in the enemy object but still nothing. any ideas please?
 

Mert

Member
GML:
if global.level += 1
{
    //do enemy stuff
}
This will throw an error as you cannot do calculations in an IF case.
The easiest & most performance efficient solution for this is to handle this task "whenever" you level up.

Here's an example function. When you think that player should level up, use the following function

GML:
function player_level_up() {
global.player_level += 1;

/* Now also level up the enemies here */
with (enemyObjects) {
    level +=1;
}
}
Code:
player_level_up();
 
S

Stratos.la

Guest
GML:
if global.level += 1
{
    //do enemy stuff
}
This will throw an error as you cannot do calculations in an IF case.
The easiest & most performance efficient solution for this is to handle this task "whenever" you level up.

Here's an example function. When you think that player should level up, use the following function

GML:
function player_level_up() {
global.player_level += 1;

/* Now also level up the enemies here */
with (enemyObjects) {
    level +=1;
}
}
Code:
player_level_up();
Thank You! although i didn't run it as a script i added it in the player level up event! i didnt really think to call the enemy level from the player and was doing it the other way around!
 
Just so you know, if you absolutely must check a variable constantly and do something when it changes, you can do that by setting another variable to it's value on begin step and then comparing them at end step (as long as you are only manipulating the variable to be checked in the "normal" step event). If they don't equal each other in the end step, then the variable to be checked has changed and you can perform your action:

Begin Step:
Code:
prev_value = value;
End Step:
Code:
if (prev_value != value) {
   // Now we know something changed value in the Step Event and we can perform what we wanted to do when it changed here
}
 
Top