Jump to content

  • Log In with Google      Sign In   
  • Create Account


Running a LUA script one part at a time


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 lawnjelly   Members   -  Reputation: 429

Like
0Likes
Like

Posted 03 December 2012 - 02:07 PM

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. Posted Image

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.

Sponsor:

#2 wqking   Members   -  Reputation: 756

Like
1Likes
Like

Posted 03 December 2012 - 10:00 PM

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...

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#3 lawnjelly   Members   -  Reputation: 429

Like
0Likes
Like

Posted 04 December 2012 - 03:16 AM

Ahha! Posted Image

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.

#4 Endurion   Crossbones+   -  Reputation: 3484

Like
1Likes
Like

Posted 04 December 2012 - 11:04 AM

Also a hint: You can return a value to C via yield and pass a parameter in via resume. This may come in handy for the Wait function.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS