I have two scenes ["scene", "HUD"]. In "HUD" is a healthBar (plane), in "scene" is a player, that contains collision sensor and python controller. I need to reduce player's health and show it in HUD. This is my script:

import bpy
import bge

cont = bge.logic.getCurrentController()
health = cont.owner["Health"] #Health is property
health -= 10

#try change healthBar dimension - full health has dimensions[1] = 1.0
hb = bpy.data.objects["HealthBar"]

hb.dimensions[1] = health/100

cont.owner["Health"] = health

This is not working. Dimensions of "HealthBar" changes, but only after the game (When I run game again, I see healthBar smaller). Where is the problem? Is there any function to update view, refresh or redraw?


You need to change these two lines, because the bpy module does not wok in the game engine.

hb = bpy.data.objects["HealthBar"]
should be changed to the code block below.

hud = None

for scene in bge.logic.getSceneList():
    if scene.name == "HUD":
        hud = scene
hb = hud.objects["HealthBar"]

That loops through all the scenes in the game, and assigning a var of hud to the scene named HUD. Then the last line gets the object named HealthBar in the HUD scene.

Or simply this line if the object was in the same scene.
hb = bge.logic.getCurrentScene().objects["HealthBar"]

Your hb.dimensions[1] = health/100 line
must be changed to hb.worldScale[1] = health/100 (because there is no dimensions attribute in the game engine).


Your Answer

