lua-users home
lua-l archive

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


I don't think in term of scripts but in term of entities or actors. An actor
has a given state and that state may have an associated script. When the
actor enters the state it starts executing the script. The script may
contain a yield in its body, but usually, those yields are associated to
other events, for example:

play_anim()

sets an animation, and yields. When the animation ends the entity recieves
an event, that resumes the coroutine, and continues the execution of the
script.

In your case I would have an entity called 'mision_completion', that entity
will always execute the following script:

while ~mission_completed() do
    self:send_resume_event( 0.5 )
    yield()
end
self:get_game():end()


that is, it checks the mission completion status every 500 milliseconds and
does only end the game when the mission is completed.

I usually abreviate that using:

self:sleep( 0.5 )

that again, yields implicitely. This makes the code very readable and
intuitive because you just think about linear actions, and not about events
and message handlers.


Ignacio Castaño
castanyo@yahoo.es


Chris Brodie wrote:
> I'm still not following how this works with my code. Cooroutines seem to
execute within a single script if I read it correctly. A yield will return
control to a central loop also within the script. At what point does my code
start executing again so I actually do all my 'game' code?
>
> For example, when I call lua_dofile it blocks until execution has
completed(as far as I know). So when I'm say loading a level from my code
what I'm looking at doing architecturally is loading a lua file that places
all the entities(models etc) and sets up the mission loop, periodically the
main mission loop would need to check on a mission completion status.
>
> This is all fine so far but with a blocking lua_dofile the mission thread
will never return. My game graphics rendering\input\sound etc will never
take place. Is there a Yield() command in Lua that return from lua_dofile?
Then in my games main loop I can call some kind of lua_dofileResume() to
resume the scripts activity?
>
> Right now the only (ugly) way I can see this would work would be to have a
lua script that call my rendering engine to draw the graphics and do physics
\ gather input etc.
>
> Am I missing the whole point here? I found this C command 'lua_newthread'.
Why does this exist for? I can imagine that this might do half my job in
starting a blocking script in a thread but I'm not sure if this is the
intended use...
>
> Many thanks for your patience,
>
> Chris
>
>
> <Snippety Bits>
> > From: Nick Trout [mailto:nick@rockstarvancouver.com]
> > Subject: RE: Game Integration
> > I've been writing a Lua tutorial and there is a page on coroutines:
> > http://lua-users.org/wiki/CoroutinesTutorial
> >
> > Regards,
> > Nick
>
>
> NOTICE
> This e-mail and any attachments are confidential and may contain copyright
material of Macquarie Bank or third parties. If you are not the intended
recipient of this email you should not read, print, re-transmit, store or
act in reliance on this e-mail or any attachments, and should destroy all
copies of them. Macquarie Bank does not guarantee the integrity of any
emails or any attached files. The views or opinions expressed are the
author's own and may not reflect the views or opinions of Macquarie Bank.