How to hide entity object in ursina engine python 3.8

Question:

I want to create a flashlight, when you press 1 beam appears, but I want it to disappear after a second. I made it, but my code doesn’t work.
My code:

from ursina import *
app = Ursina()
def flashlight(): # flashlight code
    f = Entity(model='sphere', scale=4, color=color.light_gray) 
    time.sleep(1.0) #pause
    f.visible = False #disappearing

def update(): 
    if held_keys['1']:  flashlight() 
app.run()```

Asked By: robot228

||

Answers:

To make the Entity appear and disappear after 1 second, you can use Ursina’ invoke function. Here is its implementation in your code:

from ursina import *
app = Ursina()
def test_func(e):
    e.visible = False
def flashlight(): # flashlight code
    f = Entity(model='sphere', scale=4, color=color.azure) 
    invoke(test_func, f, delay=.2)



def update(): 
    if held_keys['1']:  flashlight()

EditorCamera()
app.run()
Answered By: Tanay
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.