Skip to main content
gamedev.net
🔒 Locked

suspending a script

Started by mother 5 replies 1.9k views
Original Post
mother
mother
Hi. I've been thinking about how to use Python as a way of programming conversations between the player and an NPC in an RPG game. So what I'd like is the ability to write a .py file which corresponds to a conversation. When the player talks to an NPC, the appropriate script runs. For example: status = get_quest_status("kill_troll_under_bridge") if (status == 0) say("Please, go kill the troll!") else do: result = ask_yesno("Thank you for killing the troll! Will you take this money?") if (result) give_player_money(100) else say("But thou must!") while (not result) I'm sorry if this isn't correct Python... but my question is: how can I set up a Python function which actually suspends the script, and resumes the script once the game decides it's time? For example, the say() function should tell the game engine to display the text in a dialog box, and resume the script once the user has buttoned past the dialog. Any help would be greatly appreciated. Thanks :) james
SiCrane
SiCrane
I believe what you want is a generator. Basically a generator is a function that suspends it's execution when it hits a yield statement, and picks up where it left off when the function is called again.

I don't have any good links off hand, but your favorite Python reference probably will have a good description of how to use yield for your situation.
marcus12024
marcus12024
Quote:
Original post by mother
result = ask_yesno("Thank you for killing the troll! Will you take this money?")
if (result)
give_player_money(100)
else
say("But thou must!")
while (not result)


I'm not sure if you can do this with Python (as I work with Lua almost exclusively), but what I do is this:

ask_yesno("Thank you for killing the troll! Will you take this money?")    result[0] = "give_player_money(100)"result[1] = "say("But thou must!")" 


This way, your C++ (or whatever) code will do the ask_yesno function, showing the choice on the screen. Then you just need to make result[] available to your C++ code, and if the user chooses yes, just execute the "script" line of result[0], otherwise execute result[1].

Of course, this produces a strange union of script/C++ code, but it does work.
Life's Short. Make Games.
Endurion
Endurion
If you want to use Lua you should look into coroutines. They can suspend a script, and resume it later. You can even pass parameters, so a Wait( 1000 ); function is easily done.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
JanusFury
JanusFury
Python's generator functions work excellently for this. Look into them, they should handle exactly what you want.
mother
mother
Thanks for the replies. I looked into generator functions as suggested, and they work fairly well. For example:

def Conversation():    say("hello and welcome to foo town")    yield()    walkTo(4, 4)        yield()    say("more stuff")    return


The Python methods say() and walkTo() are actually implemented in C on the engine side. The yield statements interrupt the script and the game side can continue the script when appropriate.

So for example, the engine would know that a talk dialog is up, and resume the currently running script (if there is one) after the dialog is closed by a button press.

Same thing for the walkTo command. The engine would remember that a walkTo is in progress, and continue moving the character until they reach the destination. Once they do, the currently running script is resumed.

Is this a typical way to do scripting in games?

Also, it's unfortunate that the yields have to be explicitly placed in the script. Is there any way around this?

Again thanks for all the help. :)

james

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.