• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Instances

A

Amoses

Guest
So, I have a game and I have the Healthbar of the player in the player object, and whenever the player gets killed, I dont want the healthbar to disappear, is there any way I can put the healthbar anywhere else? or just make it so it doesnt disappear in the player object?
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
When the player gets killed, does it get destroyed? If so, that would be why it's not being drawn anymore.

If you want the health bar to persist either way, you'll have to draw it somewhere else, for example in a controller object that always exists in the room. Let it check if the player exists using instance_exists - if not, draw an empty health bar because the player is presumably dead. Otherwise, refer to the player's variables that store its health-related data and draw the health bar accordingly.
 
A

Amoses

Guest
When the player gets killed, does it get destroyed? If so, that would be why it's not being drawn anymore.

If you want the health bar to persist either way, you'll have to draw it somewhere else, for example in a controller object that always exists in the room. Let it check if the player exists using instance_exists - if not, draw an empty health bar because the player is presumably dead. Otherwise, refer to the player's variables that store its health-related data and draw the health bar accordingly.
I know why its being destroyed, and yea I need to know how to draw it somewhere else but still connect it to the players hp?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
Normally, you'd want to create a "controller" object for all things like this (ie: health, score, stats, pause menus, etc...), and you would then use "global" variables for the things you want to track. Global variables are variables that don't belong to any instance or object specifically, but are instead "global" to the whole game, meaning they can be accessed by any object. So, make the player health for example "global.hp" and then you can change it and draw it or whatever from any object in the game.
 
A

Amoses

Guest
Normally, you'd want to create a "controller" object for all things like this (ie: health, score, stats, pause menus, etc...), and you would then use "global" variables for the things you want to track. Global variables are variables that don't belong to any instance or object specifically, but are instead "global" to the whole game, meaning they can be accessed by any object. So, make the player health for example "global.hp" and then you can change it and draw it or whatever from any object in the game.
ohhhh, thats what a controller object is!!! ive been wondering what they were, cause i always see then when im watching videos on how to do things, thank you! could you give me a example on how to draw the players health from another object though?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Moderator
Okay, well you would first need to initialise the global variable. Commonly, this is done in a script which is called from the the FIRST object placed in the game (like a main menu or a splash screen or whatever). It would look like this:

Code:
global.hp = 100;
global.scr = 0;
global.whatever = "whatever";
// etc...
Now, in your game, you would have an object like "obj_GameControl" in your room, and in the DRAW GUI event you'd simply put something like:

Code:
draw_text(32, 32, "Life = " + string(global.hp));
draw_text(32, 48, "Score = " + string(global.scr));
// etc...
We use the draw GUI event, as it makes drawing stuff like this a lot easier as it isn't affected by the camera position.

Finally, in -for example - the player collision with an enemy you'd do something like:

Code:
global.hp -= 10;
if global.hp <= 0 instance_destroy();
To remove health from the global variable as well as check to see if the health value is less than zero.

I hope that helps!
 
A

Amoses

Guest
Okay, well you would first need to initialise the global variable. Commonly, this is done in a script which is called from the the FIRST object placed in the game (like a main menu or a splash screen or whatever). It would look like this:

Code:
global.hp = 100;
global.scr = 0;
global.whatever = "whatever";
// etc...
Now, in your game, you would have an object like "obj_GameControl" in your room, and in the DRAW GUI event you'd simply put something like:

Code:
draw_text(32, 32, "Life = " + string(global.hp));
draw_text(32, 48, "Score = " + string(global.scr));
// etc...
We use the draw GUI event, as it makes drawing stuff like this a lot easier as it isn't affected by the camera position.

Finally, in -for example - the player collision with an enemy you'd do something like:

Code:
global.hp -= 10;
if global.hp <= 0 instance_destroy();
To remove health from the global variable as well as check to see if the health value is less than zero.

I hope that helps!
i used a draw_healthbar, would that work instead of the draw_text?
 
Top