lua-users home
lua-l archive

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


Hello All,

I work for Relic Entertainment and we're currently integrating Lua into our
next game.  I've been given the task of doing this.  As such I've been
pondering quite a few things about Lua lately.  You may remember a post from
me regarding trading values between C++ and lua a while back.   The
responses (from Ashley Fryer and lhf) recommended a wrapper functions for
every one of the variables I want to have visible in the lua portion of my
application or pointed me towards tolua (respectively).

I tried out tolua for a bit, but I think the program is not for me for two
reasons: it seems to have difficulty with tricky programming that tends to
happen in game programming (I spent many a days trying to figure out which
part of the code in my pkg file was causing parse errors).  This would
result in my having to "own" the pkg file, making it difficult for other
developers to add their own scripting elements because they would have no
idea what to do when they got the cryptic error messages from tolua.

The other reason I don't want to use tolua, and this seems to be a problem
with lua in general, is that it creates wrapper functions for every variable
and object/structure element accessed by lua.  Homeworld (our last game) had
over 10,000 tweakables, so exposing this number of variables to lua would
result in over 1 megabyte of memory usage simply for the function wrappers.

So I've been considering another option - modifying the lua code so that it
modifies the game variables directly.  Here's how I think it can happen:

In the game, I create a list of variables that are available to lua.  This
list is looked up whenever a variable is created or accessed from lua.

In luaV_setglobal, the local variable "oldvalue" is set by accessing the
game variable, or ts->u.s.globalval (depending on whether the variable is a
game variable or not), and when the value is set using luaS_rawsetglobal,
the game variable is set at the same time.

In luaV_getglobal, the local variable "value" is set as above (by accessing
the game variable, or ts->u.s.globalval).

I would make a corresponding change to get/settable for structures that are
accessed from the game, though I have a feeling this will be quite a bit
more complicated.


Using the above method has several advantages I can see 
1. there is no need to pre-register any of the game's variables with Lua -
the variables are accessed on a "need to know" basis.
2. I can define classes (such as a vector class) in lua, but still grab in
game values for these structures.
3. As mentioned above, I eliminate the need for wrapper functions.

Since I'm new to lua, I have a few questions:

Are the 4 getglobal/setglobal and gettable/settable the only places where
the game variable lookup need occur?
Would it be wise to create a new type of variable that contains a pointer to
the actual game variable to eliminate the need for a lookup every time the
variable is accessed?
Can all this be done with tags and tag methods without a bunch of wrapper
functions?  If so, how?
Can you see any other dangers to this method?

Thanks for your help!
Falko