lua-users home
lua-l archive

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


Hello, I'm using Lua for a small game and I'm looking
for a better way of implementing something.
In my program, I start in C++, and then launch a Lua
script which takes control of the flow. The script can
then make calls to C++ functions to do various
specialized things for my game like print text to the
screen or draw certain things. All these functions
were declared static because that was the only way I
could get Lua to call a C++ function. My goal was to
make a scripting interface that was very simple to use
and push off all the hard stuff to C++. I designed
this to be procedural driven rather than event driven
so the control flow would be really simple to
understand for the scripter.

Because Lua controls the flow and my application is
single threaded so things like getting input and
drawing cannot happen automatically. Fortunately, it
happens that when the scripter expects the player to
make a choice, I can make this function block and this
is the perfect time to do all the things like get
keyboard inputs and render the scene. This is all
handled by static C++ functions called by a single
registered Lua function (e.g. userChoice =
MakeChoice(...)).

This all works very well for me except for one case.
Because all the hard processing and rendering cannot
happen until "MakeChoice" is called, I have found that
sometimes I need to save information from other Lua
function calls so I can actually act on them later
when "MakeChoice" is actually called. But because all
my functions are declared static, it makes it
impossible to save any information unless I declare
static variables to hold everything.

So for example, in my code, I have declared a bunch of
static variables to hold the data. Things as simple as
what the panel size (e.g. SetPanelSize(...)) should be
or as complicated as creating an animation list are
all declared static. The scripter can then make
function calls to change the panel size or load new
images and create a new animation sequence. All this
data is saved for use in the render stage which is
triggered when the "MakeChoice" is finally called.

So, I generally like the end result, but I'm concerned
about all the static variables I'm using. In addition,
this isn't the only game mode that is used, so it
seems wrong that all these variables are present when
they're not being used. (Incidently, a different game
mode actually uses Lua in an event driven style.) Is
there a better way to do this? My preference would be
to have some regularly declared variables or objects
and pass around some references and let everything
negotiate through that, but the static restriction has
prevented me from using anything that has an instance.

Thank you



__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com