Blender Game Health Bar

So, i was watching a tutorial on how to make a health bar byd123s404 on YouTube and I finished the tutorial only to come out to a problem. My health bar has to take damage at least once before it moves, the actual health bar moves only when the ghostHP is changed. I have posted the code below. When I run the game the console shows no errors.

import bge




def main():


    cont = bge.logic.getCurrentController()
    healthBar = cont.owner


    space = cont.sensors['Keyboard']
    
    percHP = healthBar['hp'] / healthBar['maxHP'] * 100
    percGhostHP = healthBar['ghostHP'] / healthBar['maxHP'] * 100


    healthBar['animHP'] = percHP * 10
    healthBar['animGhostHP'] = percGhostHP * 10
    
    if space.positive:
           
           healthBar['ghostHP'] = healthBar['hp']
           healthBar['hp'] = healthBar['hp'] - 30
    
           healthBar.playAction("HealthBarAnimation", healthBar['animGhostHP'], healthBar['animHP'], play_mode= bge.logic.KX_ACTION_MODE_PLAY, speed = 30.0)


main()

I guess you forgot to trigger the bar at game start with an always sensor. And this code animates on keyboard input only.

Remarks:

I think this method has to much dependencies. E.g. why does an healthbar read keyboard input?

Beside of that I do not see what “ghost” means. Looks like a the keyframe to start an animation with.

Nevertheless I recommend to focus on showing the bar regardless if it is health or something else. Have a look at BGE Guide to Messages incl. Healthbar tutorial. This is very flexible and can be used with any value you want to display. There is no dependency to any input method (mouse, keyboard) just the interface between the objects (messages). The real value is not managed by the bar (as it is “just” a display) but the object it belongs to (e.g. player).

Thanks for the reply, I will go check that tutorial out in a little bit.