[SOLVED] Health Point System

I’m looking for more information about implementing a health point system and still learning about variables, etc.

It’s a bit hard to follow the available examples and tutorials because they all seem to be based on the player having 100 health points, which is too many for my game. I’d like to use a text-object-based system that simple displays a specified amount via variables, which should fluctuate on collisions with various damage/heal objects - and these do not have their own health points.

I’ve got the basic damage part sort of working, but the heals are not adding points to the health correctly.

It keeps adding the value after instead of to the health amount.

For example; the base/start health on level one is 14. The damage objects remove 1 point for each collision, so one collision makes the health 13, etc. :sunglasses:
However, a collision with the heal object makes it 131 and additional collisions just keep adding 1’s after like 1311, 13111, and so on. :astonished:

These heals should make it display the sum of the current health plus 1.

I’m attaching a capture of the events. What am I doing wrong?

Also, it’s “base14” because the starting health is to be different for each level.

First the first, the health is a number and you want to handle it like a number (add, subtract) so use a number :wink:
(When you use strings + “1” is concatenating another string, 13 + 1 = 14. but “13” + “1” = “131” (concatenated string)).

Then, the big mistake here, when you use actions to modify a variable (Do = 0 to global variable MyVariable) you have to pass the variable name only (MyVariable) not Variable(MyHealth). The last is used in expressions to read the variable value.

Also, some inconsistencies:
*You do + “1” and - “1” (using strings) in two actions to modify the variable value, but then use = 0 (a number).
*There are two actions to modify a global variable, one to modify the number and other to modify the text, you are using the action to modify the number but using strings (in two of the three actions, read the previous point).

To sync the text object with the health value, you can add an event at the end, that always set the text object string = ToString(Variable(MyHealthVariable)).

With all these changes, the result should be:


(You can update the text in each variable modification as you are doing right now instead of doing it every frame).

1 Like

:blush:

That indeed did the trick, thank you so much for that and your patience!

On a related note…

When accumulative damage reaches 0, the game should be reset with the base health for the level intact.
Conversely, when the health reaches a certain number, the player should level up.

How I can achieve this without completely restarting the game?

Don’t worry, it’s hard at the beginning :slight_smile:

I see you’re using a global variable to save the health, global variables are stored between scenes, so if you want to reset the health you have to do it manually and then change scene:

Conditions: Global variable playerHealth.base14 is <= 0 Actions: Do = 3 to the global variable playerHealth.base14 Go to scene "MyFirstScene"

  • Instead 3 use your default starting health
  • To reset the scene go to the same scene, you can use the expression to get the current scene name and do: Go to scene CurrentSceneName()

The leveling-up system may be a bit harder, I’ve no idea how is the leveling in your game, modify stats?
If you want to modify stats (as base health), you’ll need a new variable for each stat, for example “maxHealth”, when you level-up increase the maxHealth, when you have to reset the current health (because you reset the scene) do playerHealth.base14 = Variable(maxHealth).

Thanks :slight_smile:

May I PM/DM you, here or on Discord to explain that? I’m not quite ready to put finer details in public yet.

I’ll say that I have global variables set up for the health in each level, but maybe there is a better approach.

And I’ll be trying your suggestion for the reset :slight_smile:

Yeah, you can PM me here :slight_smile:

Thanks for that! :slight_smile:

If you’d rather, we can continue about the code that’s needed here as I’m sure you get lots of PM’s and it may help someone else too.

As you prefer :slight_smile:

Also, if you can explain what the important variables stand for and how do you expect them to work, that would be great.

I’ll send you a link to my BETA release, although maybe it should be Alpha lol.

I thought that was already explained in my first PM where I listed all the level info :\ The global variables correspond to those.

Yeah, but you have consider that it’s a bit harder for somebody out of the project :wink:
I know what you want to do and I want to help but when I read “Variable(PlayerHealth.base14)” I get stuck, why base14?, is it the base health in the level 14, or for level 14, or other thing? :slight_smile:

I appreciate you taking the time.

I did consider that, hence the reason I sent a link to where I’ve published the test version of the game, where you can see how many levels there are and what the starting HP is on each. I think it becomes obvious (taken with the information you already have) that 14 in base14 refers to the HP on level 1. You may just be over-thinking it a little. :wink: The game only has 6 levels thus far, although there are 5 available on the selection screen.

What conditions/events are needed to:

  • reload the levels after HP has become less than or equal to 0?
  • remember deleted objects?

You don’t have to include all of the exact variables; if you tell me what they are for “base14”, I’ll know it needs to be done for all the other levels.

At first, I thought it was a joke and laughed a lot, really… why 14!?, why not -23? :smiley:

Anyway, just kidding, that doesn’t matter, let’s check this:

  • Reload the levels after HP has become less than or equal to 0?
  • Remember deleted objects?

To reload a level but remember deleted objects, the best way is to move the player to the initial position, because using the action to change scene will reset everything.
Then, after moving the player, reset the health to the base value, you need two health values, the base and the current. The current health is the “real” one, increased when picking up health items and reduced by enemies. When you level-up increase the base health (and refill the current health if you want).
So, with the variables “PlayerHealth.base14”, “PlayerHealth.current”, and with a helper object “PlayerSpawn” to mark the starting player position you can do:

Conditions: Variable PlayerHealth.current is <= 0 Actions: Do = Do = PlayerSpawn.X() ; = PlayerSpawn.Y() to the position of Player Do = Variable(PlayerHealth.base14) to the variable PlayerHealth.current

Well, to semi-seriously answer that question, there is a story behind the origins of this game that is related to the first program development project that I worked on.
But it is a long story and has little to no relevance to this game going forward, so I’m not getting into it :laughing:

Now back to the issue at hand…

It seems that I may have to completely rework the way I have the existing health system set up in order to integrate this because the base (and goal) health were done as global variables, but it does make more sense to have them as scene or object variables instead. It’s just that when I tried to do that initially, it wasn’t working, so I tried going with global.

When testing the code you just gave on my first level, the action to change the position on respawn seems to conflict with the action to change the position of the player after they’ve hit the ground but survived. When that happens, their health should be whatever it is after being damaged by contact with the ground.

So let’s say they started with 14, then collide a damaging object for -1 on their first move, but then fall off a platform and hit the ground for -5.
They are sent back to the starting position (for that level) with 8 HP, which is correct.

After I entered the code you gave me - albeit, probably incorrectly - my player object started falling through the platform and disappearing :\ Except I never changed the behavior of it (gravity, etc). I had to completely delete that object and recreate it, which meant I had to reenter all events that were connected to it in order to make it somewhat playable again.

I’m going to make my changes to a test copy instead and see how that works, so I can start fresh with it and still keep my functional copy. I should have done that to begin with - my bad!

I have removed all of my global variables, so now I’ve got variables on the player object itself for level/scene 1 as:

NAME | VALUE

PlayerHealth | (Structure)
base | 14
goal | 15
current | I don’t know what to put here?

I don’t know how the events should change for heals or damages now either; I’ve tried putting the variables in the events in a few different ways and none of them work. Once it gets working on one level, I can copy it to other levels, but as of now I’m basically back at square one where the health isn’t changing at all.

current | 14
So the player starts with the full base health.

At the beginning of the scene: current health = base health
Damaged by enemies: current health - 1
Pick health item: current health + 1
current health >= goal health: base health = goal health, increase goal health for the next level
current health <= 0: Move to the beginning and current health = base health

That’s a classic gaming mode, you’ll have to think a bit more if you want to do something new and fresh :slight_smile:

Thank you, I’ll try that.

Yeah, right now I’m more in “learn mode” than “create mode”.

Remember, I’ve been using GDevelop for a bit less than 2 weeks as of this post. I’m still exploring the conditions/events, getting to know how to use variables, and so on. I’m also not exactly super invested into the game development field to be honest, although I’ve been playing games since the original Super Mario Bros was a thing on the first Nintendo - Tetris was my favorite, then I moved on to MMORPG’s and Flash games.

Right now, I have time to work on this as a hobby, but soon I’ll be able to practice in my chosen field (which is actually healthcare related) and my energies will be need to be focused elsewhere. With that said, I’m not real worried about thinking of something new and fresh for the gaming community. There’s nothing really new under the sun anyway lol. When you boil it all down, no matter how you slice it, it’s all math.

It’s still not working :frowning:

The amount of base health is displayed, but doesn’t change on collision with any object. I have tried to put the variables in according to what you have and what it says to do in the wiki tutorials; none of them are changing the health text - “player.Variable(PlayerHealth.current)” “player.VariableString(PlayerHealth.current”, etc. This was an issue in the beginning until I used global variables.

Try:

ToString(player.Variable(PlayerHealth.current))

You can do a preview with the debugger (the bug button), and monitor all the object (and scene) variables from there :slight_smile:

I’m afraid not, sorry.

The debugger doesn’t tell me a whole lot since I don’t quite know what to look for.

playerhealth.png

I think it’s time to let this go aside from some casual tinkering. Thanks for trying to help.

In your image the “current” variable is a string, it should be a number, note that there are variables conditions and actions specific for numbers or string.

But anyway as you can see it has a value, and although it’s a string the value is the “same” than the base, as it should be, so the event that is failing is the one to update the text… you’re pretty close :slight_smile: