lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Let's say we have functions defined in C:
    Say(txt, speech)    -- play sound file 'speech' and show message 'txt'
on the screen. Then stop lua until the message is finished
    Wait(time)    -- stop lua for some time

look at this:

(...)
Say("Hey Fox Four, any hot women out there in Aldebaran?", "1.ogg")
Say("I don't think I want to meet any women they got out there.", "2.ogg")
Say("You don't like women with four legs and two heads? Man -", "3.ogg")
Say("One more word and you two are gone from my flight.", "4.ogg")
Wait(5)
    -- let's say that the player is saving game somewhere here
Say("Freedom to all, GE electronic countermeasures detected.", "5.ogg")
Say("Right boys, it's gonna get warm in a minute. No sleeping, no
schlepping.", "6.ogg")
Say("Roger.", "7.ogg")
Wait(3)
    CreateSomeEnemyObjects(...)
Say("Freedom to all, strong group signature growing fast at eighty degrees,
ETA forty.", "8.ogg")
Say("Copy. Sounds like you could use a radical radar upgrade there.",
"9.ogg")
(...)

That way I can construct linear action, dialogues. Imagine the same thing
using machine states. Of course in that case Say() function can't wait. It
just show the message, play the sound and returns.

if state == 1 then
    Say("Hey Fox Four, any hot women out there in Aldebaran?", "1.ogg")
    state = 2
    return
elseif state == 2 then
    Say("I don't think I want to meet any women they got out there.",
"2.ogg")
    state = 3
    return
elseif state == 3 then ..........

Of course using this model I don't need to save the "posiotion of
execution", but how does it look :(? I don't want to use machine states :)

-- Krzysiek Jakubowski

> If I really felt the need to implement logic as a single function rather
than
> a state machine, I might start thinking about preprocessing the Lua code,
> e.g. to turn this:
>
>     function my_game()
>         local x = do_stuff()
>         %YIELDPOINT%
>         return do_more_stuff(x)
>     end
>
> Into this:
>
>     function my_game()
>         local x = do_stuff()
>         return my_yield(42, x)
>     end
>
>     continuations[42] = function(x)
>          return do_more_stuff(x)
>     end
>
> The simplicity of the Lua grammar makes that not too hard to do (and with
care
> you could even avoid screwing up the line numbers for debugging). Provided
> you aren't doing anything funky with function environments, it means that
you
> can just save the global table and any locals using standard methods.
Doing
> it that way is a lot less fragile than messing about with the Lua
internals.
>
> The state machine is the better option though, even if it seems like more
> marginal work. Really.
>
> -- Jamie Webb
>