Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075913 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 02:37:08 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Pause function until X - more like SOLUTION X (seriously, though: solved!)
Pages: [1]
Print
Author Topic: Pause function until X - more like SOLUTION X (seriously, though: solved!)  (Read 955 times)
Droqen
Level 10
*****

aka nick fury


View Profile WWW Email
« on: July 22, 2009, 02:14:08 PM »

def function():
 text("Testing")
 text("More Testing")
 quit()

---- ---- ---- ----

This is just a quick little example, but currently I am trying to deal with text boxes.
They're a good example because they are time-limited and ALSO require user input.

Is there any way to say 'do NOT continue through this function until the first called function is completed' or 'do NOT continue through this function until variable == value' ?

Thanks for your help.


--> Oh, and by the way; I'm using pyglet if that matters Smiley

---- ---- ---- ----

def pause():
 freeze the current function
 and resume other things
 until something I want to happen happens
 and at that point come right back here!
 ( do I need to use multithreading for this? )

def function():
 text("Testing")
 pause()
 text("More Testing")
 pause()
 quit()
« Last Edit: July 22, 2009, 09:10:00 PM by Droqen » Logged

Kadoba
Level 3
***


Casey_Baxter@Hotmail.com
View Profile
« Reply #1 on: July 22, 2009, 02:33:02 PM »

I guess I don't understand what you are saying. When you call a function, the program flow is immediately handed to that function and after it's over, logic is given back to the point which called it.

You can prematurely end a function by using 'return'.

Though probably you're going to have to restructure your logic. You most likely want to have a queue which you shove text box dialogue into whenever you want. Then a text box object reads from this queue until it's empty.
Logged
Droqen
Level 10
*****

aka nick fury


View Profile WWW Email
« Reply #2 on: July 22, 2009, 02:38:24 PM »

Hmm. I need to have a set of functions within a single 'block' as these will be fitting inside a separate piece of code (scripted object or something like that).

Perhaps a queue could work... Now I just have to figure out how to manually parse python code I guess ;p

---> Okay. Thanks. I'll try something like that..
« Last Edit: July 22, 2009, 03:02:21 PM by Droqen » Logged

nihilocrat
Level 10
*****


Full of stars.


View Profile WWW Email
« Reply #3 on: July 22, 2009, 04:01:14 PM »

If you want to implement it exactly as you're writing above, you'd have to use threading of some sort, like with Stackless Python.

However, as you might suspect, things are rarely done this way. What you would want is something that's a lot less sequential and spread out across various things. Here's an idea:

Code:
script = [
 "What is a man? A miserable pile of secrets!",
 "But enough! Have at you!",
]

def advance_script():
  textbox.text = script.pop(0)

## controller-related code ##
def on_key_pressed(keycode):
  if keycode == key.SPACE:
    advance_script()

## window-related code ##
def on_draw():
  textbox.draw()

I have some code that does precisely this, but also slowly writes it out character-by-character like lots of classic games.
Logged

Droqen
Level 10
*****

aka nick fury


View Profile WWW Email
« Reply #4 on: July 22, 2009, 05:04:43 PM »

I enjoy that text in 'script' :D

---> However I understand. This is rather helpful! I forgot about pop, and this is wonderful... of course this specific code is just for multiple text boxes in a row, I see how this could be done for other stuff. Thanks, nihilo. ... crat.


--- + edit + ---


So I'm granting each scripted object a 'script' variable, and then during its step event (kind of), I execute the next item in that 'script' variable. (and this gets set when, say, the sign gets used or something)

And of course if one of these is executed that puts the game into a 'pause' state (like bringing up a textbox), then this step-script-calling isn't called until it's over.


I guess I'll leave this thread be and if it works and I remember I'll update this with flowers and confetti and thanks.


--- + continued edit + ---


Flowers and confetti!

Thank you :D

I haven't tried this with anything but textboxes yet,
but it works perfectly for them Smiley
I successfully chain one textbox after another and could easily chain other commands also.
« Last Edit: July 22, 2009, 09:20:35 PM by Droqen » Logged

BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #5 on: July 24, 2009, 01:38:50 PM »

Code:
def my_script():
  print "a"
  yield
  print "b"
  yield
  print "c"

stepper = my_script()

def advance_script()
  stepper.next()

advance_script works as above, but this is much more elegant. You can yield halfway through loops, and whatever, and it'll pick up where you left off.
For bonus marks, you can yield values (rather than None, as I'm doing), and make advance_script a trampoline that does different things.
E.g.

Code:
def my_script():
  print "a"
  yield Sleep(10)
  print "b"
  yield WaitForUserInput()
  print "c"

Note that stackless python and threading offer even more elegant solutions, but with the cost of an obscure python implementation, or the performance cost of threads.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic