Sign in to follow this  
PAndersson

Delaying in python, while application continues to run

Recommended Posts

Hey all, I have implemented python in a game I have been working on (written in C, if it matters) in order to be able to run scripts written in it. It works very well but I'm unsure on how to make it possible to have a specific python script delay in its middle until certain conditions are met without also freezing the games main loop. I could expose a function that simply calls the games main loop, but that solution seems inelegant at best. I have thought about calling python scripts in a separate thread, but that brings a whole new host of issues. Does anyone have any suggestions in how to handle this?

Share this post


Link to post
Share on other sites
Generators may be just what you're looking for. Consider a generator that, whenever it wanted to pause, would yield instructions on when it should be reawoken.

Alternatively, take a look at the sched module.

Share this post


Link to post
Share on other sites
What kind of situations do you plan to cover? Can you give some concrete examples?

Either way, look at it as if you were using C functions: you would probably bail out of that specific function once you would detect those unmet conditions, and call the function at a later time again - possibly storing some information for later. You can do the same with external scripts.

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
What kind of situations do you plan to cover? Can you give some concrete examples?

Either way, look at it as if you were using C functions: you would probably bail out of that specific function once you would detect those unmet conditions, and call the function at a later time again - possibly storing some information for later. You can do the same with external scripts.


Mostly being able to call simple UI dialogues and wait for user input until continuing, as theses can sometimes grow quite complex it would be extremely elegant if I can simply call an UI dialogue, wait for a response and then continue with the script after the user has picked an option. Often going down different paths depending on what he/she picks so it.

Share this post


Link to post
Share on other sites
Try something like this. Obviously this is sort of a weird example, as there's no user input.


class Scheduler():
def __init__(self):
self._running_tasks = [] # list of 2-tuples; first val is task; second val is param to send
self._blocked_tasks = {} # key is event string; value is list of tasks waiting on that event

def add_task(self, task_generator):
self._running_tasks.append((task_generator(),None))

def notify(self, event, parameter=()):
if event in self._blocked_tasks:
for task in self._blocked_tasks[event]:
self._running_tasks.append((task, parameter))
del self._blocked_tasks[event]

def step(self):
old_running_tasks = self._running_tasks
self._running_tasks = []
for task,param in old_running_tasks:
try:
result = task.send(param)
if result is not None:
if result in self._blocked_tasks:
self._blocked_tasks[result].append(task)
else:
self._blocked_tasks[result] = [task]
else:
self._running_tasks.append((task, None))
except StopIteration: pass

s = Scheduler()

def greeter():
yield "entry"
print "G: Hello, what is your name?"
s.notify("greeted")
name = (yield "name_input")
print "G: Hi there, ", name

def player():
print "P: Doing something..."
yield
print "P: Doing something else..."
yield
print "P: Going inside to be greeted..."
s.notify("entry")
yield "greeted"
print "P: My name is Player."
s.notify("name_input", "Player")

s.add_task(greeter)
s.add_task(player)
for i in range(12):
print "Stepping..."
s.step()


Share this post


Link to post
Share on other sites
Another approach is the callback approach, where you break the script up into two parts, and have the first part register the second part to be called later, and then return.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this