How to add delay in Ursina Python Game Engine

1.6k Views Asked by At

I've recently started using Ursina Game Engine (runs in Python), and have run into a problem throughout many of my projects. I don't know how to implement delay, or sleeping between functions. There is a Wait function that I haven't been able to get to work. I've tried alternatives like time.sleep() and having a function that using delay, but none of them seemed to work. Since this isn't a very popular game engine, there aren't many guides or helpful information online.

Wait function documentation

2

There are 2 best solutions below

0
On

Thats actually easy to use delay, here's an example:

from ursina import *

app = Ursina()

player = Entity(model='cube', color=color.gray, scale_y=2)

def input(key):
    if(key == 'space'):
      player.y +=1
      invoke(setattr,player,'y',player.y-1,delay=.25)

app.run()

In this example, basically, when the player press space, the entity's y increases by 1 and after the delay the entity's y decreases by 1, in other words we created a jump with the delay.

Note: The setattr() function sets the value of the attribute of an object. You can see more about this function here: https://www.programiz.com/python-programming/methods/built-in/setattr

0
On

Calling a function with a delay is done by using the invoke() function, like that :

def foo():
    print('bar')
    
invoke(foo, delay=5) # Calls myFunc after 5 seconds

and for functions that require arguments :

def foobar(foo, bar):
    print(foo + bar)
    
invoke(Func(foobar, 'foo', 'this is foo\'s value', 'bar', 'and this is bar\'s'), delay=5)