(SOLVED) Create Responsive Health (Not Using the Health Extension)

How do I…

Unscrew-up my health programming? It was all nice and in place before I programmed another mechanic and then suddenly it’s not working. I tinkered with it some more and it didn’t work. I looked up a template and it got worse. Tried to use the Health Extension, but that didn’t work. I never touched the health script when I scripted the other mechanic, so I don’t understand what went wrong.

What is the expected result

When the player runs into one of the enemies, one of the hearts in the corner is supposed to disappear. After all five hearts are gone, it’s Game Over and the Restart screen should appear.

What is the actual result

When the player collides with one of the enemies, nothing happens. No hearts disappear, so injured sound, no game over, or anything else that’s supposed to happen when the player collides with an enemy.
Incase this is important, I put all the enemy sprites in a global object group called Enemies. None of the enemies have health mechanics in their external events, it’s all in the player’s Ex. Event.

Related screenshots

Screenshot 1 is a preview of the game. Player is colliding with a rat enemy, but there’s no reaction.
Screenshot 2 is the player’s external event with the health programming. The disabled scripts are old code that I was gonna delete. It currently doesn’t work.
Screenshot 3 is supposed to be the external event for when the player dies.
Screenshot 4 is the first/respawn room. There’s not much scripting regarding the player’s health and respawn, but I thought I should add it anyway.

In the second block of your hurt event you are inverting the condition that the global variable Saving.Health is not equal to 0 (meaning it should be 0 before the actions will be triggered).


I’m sure that helped the code somewhere, but so far, nothing has changed. When the player collides with one of the enemies, there’s still no response.

EDIT: Now that I think about it, most of my current problems with the code involve the collision condition. It’s almost like the program doesn’t ‘see’ the part of the script with the collision condition. I have another mechanic that involves a collision condition with an enemy and it was a headache to get it to work. Then it stopped working again. I was only able to half-fix it, where the collision worked in one room but didn’t work in another room, despite linking the right extensions to both rooms. Idk if it’s the collision condition itself or the enemy sprite that aren’t getting acknowledged. Or maybe sprites with the Platformer Behaviour.

If you’re using the flash(blink) behavior, just activating it isn’t enough. You also need to use it’s flash(blink) action and set the length of time.

1 Like

Thanks Keith. I tried to see how to implement that.

I managed to partially fix the health. Along with yours and Drona’s suggestions, I also split the script between the Player and the Enemies. Basically the enemies have the script to activate the blinking behaviour and the player has the part of the script to change the health.

Two things are happening, but I think they’re tied. This is when the player is supposed to get a game over and when they start.

Screenshot 1: When the player starts the game (Menu Scene → Level Select Scene → First Room Scene) and enters the first room for the first time, the Restart Game text (which is only supposed to appear when the player is dead) appears. Meanwhile, when the hearts are gone and the player is supposed to be dead, the Restart Level text doesn’t appear when it should.

Screenshot 2: The script for the Level Select room. I thought that there could be a problem between the game going from Level Select to the first room, but I haven’t found anything yet.

Screenshot 3: The DeathAndRestart event is where I scripted the what happens when the player loses all 5 hearts and gets a game over. This might be where the problem is, but I haven’t found anything yet.

I tested the flash(blink) behavior. Oddly, when disabled it still sets the is flashing condition to true if you use the flash action but it doesn’t flash. I would’ve thought nothing would happen.

There’s no need to disable/enable the flash behavior. It might actually be causing a problem. There are at least 2 spots where I saw an enable behavior without the flash action. I would remove the enable and disable and make sure there’s a flash action with how many seconds that you want it to flash.

1 Like

I took out the flash behaviour, but that didn’t change anything. On the plus side, my code looks a little cleaner, now.

I’m trying to look up a specific health tutorial in the meantime that I liked, but I can’t seem to find it. :confounded:

EDIT: I’m wondering if there’s somewhere that says that when the game starts, it sets the player as Dead but it doesn’t change it. That could explain why the game starts with the program enacting what it’s supposed to do when the player is dead, but doesn’t when the player’s hearts are gone. I thought that I avoided this by setting the global variable for the player’s health to 5 in the Level Select scene when the player clicks the button, but I’m wondering if there’s somewhere the program is changing that.

You can use the debugger to check the health variable. I would check the other sections. Maybe the game load or save. section. I would definitely check the health variable in the debugger. That might narrow down the problem.

Health isn’t too complex. You start at a value like 5. When the player takes damage, you decrease the value. If the value is equal to or less than zero then the player is dead.

1 Like

Jeez, why didn’t I think of the debugger?? Sleep problems are definitely catching up to me, haha.

Anyway, I did and what I suspected was happening is happening. When the game starts, the health global variable is set to 0 for some reason. Now I gotta figure out where that’s happening and correct it without screwing up the rest of the scripts.

1 Like

Ikr??? I’ve been using GD long enough, you’d think this would be easy for me!
Though I should cut myself some slack, cuz storage, data, and programming little exceptions in the scripts is still new for me.

EDIT: So using debug, it looks like the Dead boolean IS set to True when it should be False, and the health variable is set to 0 when it should be 5. Now I just need to figure out where the code is changing that. That’s a bit weird, because I have a condition and action in the first room that, if the Dead boolean is True, then to switch it to False to avoid this very problem. I’ll need to find out why it’s not acknowledging that. I think it may be something with the Death and respawn Ex Event.

There’s a lot to learn. I’m still learning. Sometimes it’s the smallest thing that gets you. I can’t remember all the times it was a typo. A variable was either misspelled or a letter was in the wrong case. I also copy/paste a lot and sometimes I forget to change an X() to a Y(). You can read the same lines hundreds of times before you see the problem.

I get stuck with instances. I’m not always sure when to use a for next object.

1 Like

Another thing I found: when the player’s health is at 0, the boolean won’t set to True like it should. This is frustrating because I have it listed in the Death and Restart extension that the boolean switches to True when the health variable is 0, so I don’t know why that’s not being acknowledged.

Okay, now this is just weird. I managed to find a way to set the Dead boolean to False and the Health to 5 when the room starts, but it’s still glitching???

Also, when I disable all the script in the Death and Restart Ex. Event, the health doesn’t work anymore. I’ll collide with an enemy and nothing happens. This makes no sense because that stuff occurs in the Player’s and the Enemies Ex. Events. They shouldn’t be linked like that.

This might be unrelated but IDK if you noticed the error message in the bottom left corner. Something about an effect on the playButton.

Yeah, I noticed that. idk what it’s talking about. It doesn’t really tell me what I need to do.
The only effect I know that I’m doing with it is to Bold the text when the mouse is touching the text.

EDIT: I figured it out. Turns out I had some kinda effect installed. I uninstalled it now and it’s not showing up.

Good news and bad news:
I played around with the scripts and was able to find a way to start the game without it beginning with the Dead boolean set to True. The bad news is that the Health Variable has disappeared from the debugger list, so that’ll be my new goal for today.

If I can get that variable back into the script and set it right, then that will have it solved. The only kicker is that I can’t just make the variable change to 5 because the player may reenter the room. I gotta also make sure the health doesn’t reset whenever there’s a scene change. I knew how to do this, so I gotta try and remember it.

I think I might’ve got what I wanted.

Screenshot 1: script of Level Select Scene. I set an action for the text of global variable PrevRoom to “Start”.
Screenshot 2: First/Respawn room script. I have a sub condition that, when the PrevRoom global variable is “Start”, the player will have 5 lives.
Screenshot 3: Record of lives in the Death and respawn Ex. Event.

So far, this is working. When the player starts the game, they have 5 lives. When they leave the room and get injured, a heart disappears. If they return to the first room without dying, the hearts don’t change back to 5 so the script is keeping track of the number of lives between rooms. This is because whenever the player changes rooms/scenes, the text for PrevRoom changes.

I’m going to test things out tonight and tomorrow before I mark this as solved just incase I find another bug.

yeeeeah it’s not working.

For some reason, the code keeps changing the PrevRoom variable into a number when it’s supposed to be a string. It changes into a string in every other room, just not the first one. I looked at the script in the first room and any mention of the code sets it as a text, not a value, so I don’t know why that’s happening.

I tried some other things, but nothing’s been working. The health still stays at 0 when the game starts. It’s at 5 in the Menu Scene, it’s at 5 in the Level Select Scene, but the second it changes to the first scene of the actual game, it sets to 0.

I’m open to any suggestions by this point. Nothing I’ve tried is working.

EDIT: I think I might’ve found a problem. I had an extra condition-action sequence (the now disabled block under the cursor blocks) about the transition and change scene in the Level Select Scene. I deleted that and it looks like it’s working so far, but I’m gonna do more testing tomorrow just incase I’m wrong again.

EDIT 2: One bug I noticed. On the Level Select scene, I have to click on the level 1 button twice before it responds. It doesn’t work when I just click it once anymore.

I’m looking thru your various screenshots. I can’t tell which ones are current but in several places there is change the global variable Saving set to 1 Unless, I’m reading it wrong, I think that will reset your entire structure to just 1.

Edit: on the debug screen PrevRoom is showing as 0. Although on 1 screen it shows as set to CurrentScene.Name(). Variables in Gdevelop are weird. Just trying to read them will create them.

1 Like