Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TitLoulou

Stopping and resuming a Lua script

This topic is 5241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello ! I''m using Lua 5.0 in my game. Sometimes, I want my scripts to wait for some event before continuing their execution. For example, I''d like my scripts to stop after each call to the Wait() function. So I tried to call lua_yield at the end of the C++ function corresponding to Wait(), and then lua_resume when I want to reactivate the script, but it doesn''t seem to work (the game crashes when I call lua_yield). Does lua_yield only works on threads created with lua_newthread ? What is the best way to stop/resume scripts ? Thank you (and sorry for my bad english !)

Share this post


Link to post
Share on other sites
Advertisement
AFAIK lua_yield and lua_resume is used for coroutines. Just create a coroutine function that will call your script. Inside the script you can call coroutine.yield to pause and coroutine.resume to resume the script later on.

You can also pass parameters which is very convinient for a Wait 100 ms script function.

Share this post


Link to post
Share on other sites
In fact, I''d like to pause and resume the script outside of it. The script itself don''t have to know about this mechanism.

The goal is to handle dialog boxes in a RPG game, especially those which require the player to select an answer.

The script would look like this :

************************
Dialog =
{
title = "What can I do for you ?",
"1. Can you bring me some coffee please ?",
"2. Give me your super sword !",
"3. It would be great if you could make those scripts work "
}

ShowDialog(Dialog)

if Answer == 1 then
...
else
...
************************

So, in my C++ code, the ShowDialog() function would pause the script, and it would be resumed when the player choose one answer, assigning it to the global variable "Answer".

That''s the idea.

As I am quite new to Lua and scripting in general, all suggestion will be appreciated

Share this post


Link to post
Share on other sites
I only ever used coroutines, so i don''t know if that would be possible with lua threads.

For this to work the calling code of the main script must be in another (real) thread.
In my game i used a coroutine, which would in ShowDialog loop with yield and wait for a answer to be set. I have a list of running coroutines and in every frame i''d call resume until they are done.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!