Search code examples
pythonursina

how to make a delay ursina sounds


so i want to make a wait function without terminating the whole program in ursina for example

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from time import sleep
app = Ursina()
def input(keys):
    global inv
    if keys == 'left mouse down':
        print("hey")
        sleep(1) #here i can move in the game only after 1 sec
        print("done")
FirstPersonController()
app.run()

what i want to is do { sleep(1) } and can still move while it's sleeping. tried Threads but it only work once


Solution

  • There are several ways to do this, but the easiest is probably to use invoke. Like this:

    invoke(Audio, 'my_audio_file.ogg', delay=2) # will play after 2 seconds
    

    Keep in mind you don't do Audio('my_audio_file.ogg') for this, as it would call it immediately. Instead you pass the arguments after the function you'd like to call, separated with commas and finally the delay. The delay must include the keyword and not just the number.