lua-users home
lua-l archive

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


Have you thought of using a table to keep active object handles?  Then you
just pass the handle in and it knows what to do.  Each object on the C++
side keeps its own handle and can therefore pass it (or heck, use the this
pointer as the handle maybe) to the script engine when it gets its turn to
process AI.

Of course, I've never tried it, I'm just popping off.

You might want to take a look at Game Scripting Mastery as well as
Programming in Lua for more useful hints - GSM has a short example of
getting the Lua interpreter working with a very small game framework in C.

Rich

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Michael Abbott
Sent: Sunday, February 13, 2005 4:58 AM
To: lua@bazar2.conectiva.com.br
Subject: Turning an entire script into a coroutine


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