• Advertisement
Sign in to follow this  

Running a LUA script one part at a time

This topic is 1909 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

Hiya, I have some experience writing my own scripting languages in the past, but wanted to have a go at using LUA for a little 2d rpg type game.

It was very easy to get it loading a script and running it within the game, so far so good. smile.png

Next stage is, instead of running an entire script at once (via lua_pcall), I want to be able to run through a script, up until it reaches certain time consuming functions, when I want to pause the script until the c++ tells it to resume again.

Here's the flow:

monster.lua:


TextOut("Hello I am a monster")
Wait(5)
TextOut("Now I'm going to eat you")
Wait(2)
MoveToPlayer()
TextOut("Yum yum")


So within most scripts run by characters etc, I need to pause the script, get on with running the rest of the game, other scripts etc, then resume again when the wait condition is satisfied.

The example above has 2 'waiting' functions, Wait (number of seconds), and MoveToPlayer (which may take a varying amount of time).

Hopefully this should make sense.

Does anyone have any idea how I should achieve this in lua? I'm having inkling feelings it may be to do with the yield and resume commands, but I haven't seen any decent explanations.

Many thanks.

Share this post


Link to post
Share on other sites
Advertisement
Pseudo code, you must polish it...


Lua code
function taskA()
TextOut("Hello I am a monster")
Wait("A", 5)
TextOut("Now I'm going to eat you")
Wait("A", 2)
MoveToPlayer()
TextOut("Yum yum")
end
function Wait(what, period)
CppWait(what, period)
coroutine.yield()
end
function resumeA()
coroutine.resume(coA)
end
-- main code
coA = coroutine.create(taskA)

C++ code
void CppWait(std::string what, int period)
{
// add period to some condition list by "what"
}
void YourGameLoop()
{
if(condition for taskA is met) {
callLuaCode(resumeA);
}
}


Hope the code explains itself.
Note: I never used Lua coroutine before...

Share this post


Link to post
Share on other sites
Ahha! biggrin.png

Thanks wqking, that seems to make a lot more sense.

I thought it should be able to do it, just wasn't sure of the syntax. Surprising that I couldn't find more tutorials on this, as it's such a basic necessity for scripting in a game. But maybe I just didn't know what to google for before.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement