In Blender Game Engine, I want to have an object (in this case, text in a HUD, but it could be anything) appear when triggered, linger for a couple of seconds, and then disappear. For example, the player hits a key M to change an object from a cube to a sphere (like in my stackexchange question here). Some text in a HUD appears: "Changed to sphere." The text disappears after a couple seconds, and doesn't come back until the player does something again to warrant a HUD message.
Think of it like how a message like "Picked up health kit (+10 HP)" would display in an FPS, and then disappear after a couple seconds. I know how to generate and display the message, but I don't know how to make it disappear.
Another requirements that may need to be considered: the program can't be stuck during the duration of the fade. So if the user hits M M M in rapid succession, and M cycles between a cube, a sphere, and a cone, I don't want the text to be stuck on "Cube" for 3 seconds regardless, or even the whole program to be stuck until the text goes away. I want it to display the text "Cube" "Sphere" "Cone" in real time as the user hits M, and then when the user hits nothing, "Cone" would fade away after a few seconds.
Here's what I tried so far
My first attempt was this
import bge
import time
def lingerObject(obj,lingertime):
#Makes object obj (e.g. HUD text) linger for a certain number of seconds, then disappear
#lingertime should be a time in seconds
start_time = time.clock()
end_time = start_time + lingertime
obj.visible = True
if time.clock() >= end_time:
obj.visible = False
"""Elsewhere in the code, I change the HUD text (successfully)
and call this new function to make the object disappear (unsuccessfully)"""
def someFunction():
#These two lines work:
hudfield = getHUDfield() #getHUDfield is my function that finds the appropriate text object in the HUD scene
hudfield["Text"] = "New Message, e.g. Cube"
#This line (or rather, my function) doesn't:
lingerObject(hudfield,3)
As I point out in the code comments, it doesn't work. Everything else still works just fine, the text changes every time that it's supposed to, but the text never disappears.
I also tried a while loop instead, and it just made the program run very sluggishly, and eventually the text disappeared and never came back:
import bge
import time
def lingerObject(obj,lingertime):
#Makes object obj (e.g. HUD text) linger for a certain number of seconds, then disappear
#lingertime should be a time in seconds
start_time = time.clock()
end_time = start_time + lingertime
while time.clock() <= end_time:
obj.visible = True
obj.visible = False
#Elsewhere, lingerObject(obj,lingertime) is called like in the prior code block
I looked around online, I found a lot of HUD tutorials but none on how to have text disappear after a bit. Any help would be appreciated. Thanks in advance!