🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Stopping and resuming a Lua script

Started by
2 comments, last by TitLoulou 20 years, 2 months ago
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 !)
---------------Outch... Pas toujours facile de poster sur un forum anglophone !
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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
---------------Outch... Pas toujours facile de poster sur un forum anglophone !
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.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement