GML Help with Persistent Global Variables

1. I set up a global variable (global.phealth) which I display as a health system for my player. I'm looking to have this global.variable remain persistent throughout each level so that his health carries over from room to room, but once the level transitions or restarts; instead of the bar staying at the last position it once was, it restarts. I've also checked off the persistent box for this object as well and still no luck. Does anyone have any suggestions?

- I control the increase or decrease in the levels of the bar by
global.phealth -= 10; or global.phealth += 10;

The Healthbar code is:


Create:

GML:
global.phealth = 0;
Draw:

GML:
draw_sprite_ext(sprite_Coach_Health,0,x,y,global.phealth/100,1,0,c_white,1);
draw_self();
 

Nidoking

Member
Every time you create a new Healthbar instance, it will reset global.phealth to 0. If the instance is persistent, then you only want to create one at the start of the game, or each time you want the health to be reset (and you've destroyed the previous one).
 
I did I placed it in my first room, Level 1 & I didn’t place it in another room past then because it’s persistent. But it still resets when the level changes.
 

gnysek

Member
Put a breakpoint in create event, and run in debug mode. Check if create event is performed in level 2. That should give you answer, if it's not executed, then another object must have set this value to 0. On room restart it may reset though, so it's usually good habit to have a "empty" room with one object where all global settings are applied, and then it immidiately goes to next room, and never comes back to room 0.

For GMS 2.3 you can initialize global variables in any script, outside of functions - this code will run first before "game start" event then.
 
I tried debugging it and looking for another object must have set this value to 0, and still no changes. The global variable still resets back to zero.
 

FoxyOfJungle

Kazan Games
I think there is some part of the code that is resetting the variable. To find all the parts that contain the variables, inside the IDE, click on Edit and Search, type the global variable and GMS 2 will search all files, so you will find out if there is any part of the code resetting. Put that variable in the start of the game, in the first room, make sure that the object is not persistent.
 
Top