Download

How to enable delay in python script instead of time.sleep(t)

Hi , I would like to call a function after specific duration. My python script runs at start of project.
The need is that I need to call some functions once a file has been created at specific location. All of this during editor time, not during gameplay.

ex:
filename-> dataConfig.ini
location-> content/dataConfig.ini

lets say the solution would be:
print(“function has started”)
after the file has been created by external program
then print(“file has been created”)

But, python in unreal pauses for the duration and then resumes. I dont want unreal to pause.
/////////////////////////////////////
import time

print " at 0 seconds"
time.sleep(5)
print “after 5 seconds”
//////////////////////////////////////////

another approach i tried was to use threading . This doesnt work either.

//////////////////////////////////////////
import threading

def testFunc():
print “function started”
t=threading.Timer(5.0,testFunc)
t.start()
//////////////////////////////////////////

I’m using PySide which is a Qt framework Python bindings.
It offer a QTimer class which you can implement the singleShot method.

//////////////////////////////////////////

import unreal
from PySide import QtCore
QtCore.QTimer.singleShot(1000,lambda:unreal.log(‘hello,world’))

//////////////////////////////////////////

Unlike using time.sleep , it would not freeze the unreal engine.

Have a look at threading.Timer. It runs your function in a new thread without using sleep().

from threading import Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

The second method to delay would be using the implicit wait method:

driver.implicitly_wait(5)

The third method is more useful when you have to wait until a particular action is completed or until an element is found:

self.wait.until(EC.presence_of_element_located((By.ID, 'UserName'))

Python sleep() will pause for an hour, day or whatever if given the proper value. It does not allow other processes take place (in same script) however. A better way is to use an event which will create an event on timeout.