0
$\begingroup$

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!

$\endgroup$

1 Answer 1

1
$\begingroup$

Making an object disappear after a few seconds with Python (BGE)

Think event driven!

WHEN happens WHAT?

WHEN

after a few seconds

'After' is relative ... checking your details ...

appear when triggered, linger for a couple of seconds, and then disappear

So I guess you mean:

A few seconds after triggered

Solution

when you know the "triggered" event, you can start a timer, counter, whatever that triggers an event after the desired seconds.

Example: Timer Property

Property: "time", type "Timer" (A timer property constantly increases it's value with each frame with the [configured] duration of the frame.)

enter image description here

When your trigger event happens you set the property to the number of seconds to wait and multiply it with -1 e.g. -3 for 3 seconds.

enter image description here

A property sensor can evaluate when the property "time" is greater than zero. That is the event you are looking for.

enter image description here

WHAT

whatever you want

Remarks

  • Timer properties are always float (avoid "equal" compare)
  • Lags will not be measured by timer properties
  • Timer properties always count upwards
  • Timer properties count always (it will not stop)
  • You can explicitly assign new values to timer properties (e.g. for reset)
$\endgroup$
1
  • $\begingroup$ Thank you, this worked. FYI, I did it slightly differently: In logic bricks, the timer > 0 condition makes the text invisible. I wanted to do the actual "make object visible and reset timer to -3 s" triggering in Python since I'm going to be doing a lot more similar object-lingering later, so this is how I did the Python component: def lingerObject(obj,lingertime): // obj.visible = True // obj['lingerTimer'] = -lingertime $\endgroup$
    – DrSandwich
    Dec 11, 2015 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .