[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Looking for a better way with Lua, C++, and static members
- From: Eric Wing <ewing2121@...>
- Date: Fri, 15 Aug 2003 02:19:34 -0700 (PDT)
Hi, thank you for the response. I'm still kind of new
to Lua so if you could provide me a little source code
example, I would appreciate it.
First, I thought I should demonstrate what I'm doing
to give you a feel of what's going on.
Here's a stripped down, trivial example of what I'm
doing. I've registered two functions to Lua,
MakeChoice and SetFontSize.
As I mentioned before, I have to save values I change
because the render stage doesn't occur until
MakeChoice is called by the scripter. So I have a
variable called font_size. Because my functions are
static, my variable is forced to be static.
static int MakeChoice(lua_State* ls);
static int SetFontSize(lua_State* ls);
// This is what I would like to do but can't
// int font_size;
// This is what I end up doing
static int font_size;
// get parameter from lua stack here
font_size = (int)lua_tonumber(ls, // some stack
// clean up
// First sort out lua stack
while( choice_not_made )
choice_not_made = GetInput();
// Use the font_size here
So in the Lua code, the scripter can write:
-- do stuff here
MakeChoice("Option 1", "Option 2", "Option 3")
-- more stuff happens here
In this example, I would like a way to encapsulate
font_size so it isn't a static (or global) variable.
These methods didn't really need to be embedded in a
class, but it just kind of worked out that way when I
started trying to encapsulate things, not knowing at
the time that I had to declare things as static.
However, even as regular C/C++ functions, I still
don't know how to make font_size accessable without
making it static or global.
In my real code, I have other things I need to store,
from window pane sizes and colors to lists of images
to control flip book type animation. So my number of
static variables has been increasing which is starting
to concern me. I was hoping to find a cleaner way to
do what I have done without burdening the scripter
with extra stuff to worry about.
Thank you again
> From: Diego Nehab
> You mean you want Lua to call a method of a object?
> If it is just a C++
> function, Lua should have no problem calling it
> regardless of whether it
> is a static or an extern function.
> Methods are a little trickier. The way I like doing
> is to declare a
> simple function for each method. Each function is
> associated with the
> object instance in some way. Here there are several
> options: receive it
> as a parameter, retrieve it from an upvalue, take it
> from a global
> variable, get it from the registry etc. When the
> function is called from
> Lua, it retrieves the pointer to the object and
> call the appropriate
> method. You can even make it so that you actually
> "object:method()" in
> our Lua script.
> toLua does this binding automatically for you.
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software