[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Turning an entire script into a coroutine
- From: Michael Abbott <mabster@...>
- Date: Sun, 13 Feb 2005 23:57:47 +1100
I'm fairly new to both Lua the language and the C-API, so please bear
with me. I also hope this hasn't been asked to often, I've done
searches through the archives and come across similar information but
nothing that really solves my particular situation.
I have a number of entities in a game I'm working on and I want to bind
a lua script to each entity that will be used to describe it's
behaviour. I want to do this with coroutines so that each entity can
yield once it has done it's current bit of the update. There appeared
to be some functions called lua_cobegin(), etc. that helped implement
coroutines from the C end in the lua 4 days, but I can't find it in the
version I'm using (5.0.2) so I assume it's deprecated.
I want to implement something that looks like this:
-- various state functions will go in this section
function player_idle()
while true do
yield();
end
end
-- initialisation code and the call to the first state will go here
player_idle();
This doesn't appear possible by what I can tell because the yield()
would need to know what coroutine this script is in relation to (a
script is not a coroutine right?). Please tell me I'm wrong because I'm
not proud of the next solution I started implementing. So I ended up
doing something like this:
function player_idle()
while true do
coroutine.yield(1)
end
end
return coroutine.wrap(player_idle)
From the C-side of things, I lua_open(), luaopen_base() (to get the
coroutines). I then luaL_loadfile() the script. I then call
lua_call(). I assume this now creates a global player_idle function and
then returns a new coroutine. From the C side of things I now have a
number on the stack, but I'm not sure what to do with that number?
Where do I go from here?
Thanks in advance,
- Mab
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.7 - Release Date: 10/02/2005