Loading values into C with Lua

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading values into C with Lua

Falko Poiker
Hello,

I'm new here, so apologies if this questions has been asked before...
I'm curious - is there any more detailed documentation somewhere about how
to use Lua to configure (and interact with) a C program?

We're going to use Lua as an embedded scripting language for our next game
and I'm starting by getting lua to load in tweakable numbers, obviously
hoping to expand on that later as I get to know the language a bit better.

At the moment, I'm using lua_getnumber(lua_getglobal("varname")) to load in
variables that I set in a script file.  Is this the best way?  Or is there a
way I can get Lua to point directly to (or change more actively) the
variables I'm setting?   In addition, how do I do the above with tables?
For example, I want to create tables that set the colours of procedurally
created graphics in the game, but the manual skips letting us know how to
load a table into a C program....

If my description is unclear, here's an illustrative example:

In a file called tweak.lua, I'd like to have the following lines:

-- configuration variables for the game
configVar1 = 10.0
configVar2 = 323.0
configVar3 = 5

-- configuration colour for the game
configColour = {255, 10, 10}   -- r,g,b deep red

-- etc...

In the game, I would have these same variables defined either locally in an
object or globally in a namespace, or whatever.  I set configVarX with the
lines

init()
{
	// read in configuration file
	lua_dofile("tweak.lua");

	// read in tweaked values for configuration variables
	configVar1 = lua_getnumber(lua_getglobal("configVar1"));
	configVar2 = lua_getnumber(lua_getglobal("configVar2")); 
	configVar3 = lua_getnumber(lua_getglobal("configVar3")); 
 
	// colours?
...
}


Am I doing this all wrong?  Or am I on the right track?

Thanks for any help you can give.

Falko Poiker
Programmer/Designer
Relic Entertainment Inc.
[hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Loading values into C with Lua

Ashley Fryer-2
> In a file called tweak.lua, I'd like to have the following lines:
>
> -- configuration variables for the game
> configVar1 = 10.0
> configVar2 = 323.0
> configVar3 = 5
>
> -- configuration colour for the game
> configColour = {255, 10, 10}   -- r,g,b deep red
>
> -- etc...
>
> In the game, I would have these same variables defined either
> locally in an
> object or globally in a namespace, or whatever.  I set configVarX with the
> lines
>
> init()
> {
> 	// read in configuration file
> 	lua_dofile("tweak.lua");
>
> 	// read in tweaked values for configuration variables
> 	configVar1 = lua_getnumber(lua_getglobal("configVar1"));
> 	configVar2 = lua_getnumber(lua_getglobal("configVar2"));
> 	configVar3 = lua_getnumber(lua_getglobal("configVar3"));
>
> 	// colours?
> ..
> }
>

If you plan to store this data on the C-side anyway, you might as well
expose access APIs to Lua and have your configuration scripts call those
APIs.  This would eliminate the problem of keeping the Lua variables in sync
with the C variables.

For example:

In tweak.lua:

   configVar1( 10.0 )
   configColour( 255, 10, 10 )


In the C code:

   void configVar1( void )
   {
     double var1 = lua_getnumber( lua_lua2C( 1 ) );
   }

   void configColour( void )
   {
     int red = lua_getnumber( lua_lua2C( 1 ) );
     int green = lua_getnumber( lua_lua2C( 2 ) );
     int blue = lua_getnumber( lua_lua2C( 3 ) );
   }

   init()
   {
     lua_register( "configVar1", & configVar1 );
     lua_register( "configColour", & configColour );
     lua_dofile( "tweak.lua" );
   }


Regards,
ashley


Reply | Threaded
Open this post in threaded view
|

Re: Loading values into C with Lua

Luiz Henrique de Figueiredo
In reply to this post by Falko Poiker
>From: Falko Poiker <[hidden email]>
>
>I'm new here, so apologies if this questions has been asked before...

Some of it has been :-) Check the lua-l archives.

>At the moment, I'm using lua_getnumber(lua_getglobal("varname")) to load in
>variables that I set in a script file.  Is this the best way?

Yes, that's the natural way, except that if "varname" does not contain a
number you'll get 0 and no warnings. You may want to use lua_isnumber before
calling lua_getglobal.

>Or is there a
>way I can get Lua to point directly to (or change more actively) the
>variables I'm setting?

Yes, there's a way: use user data and "getglobal"/"setglobal" tag methods,
but you'll have to program these.
The tolua tool is probably your best option here if you want to export many
variables, of many diferent types.

>In addition, how do I do the above with tables?

Use lua_gettable.
Again, tolua is very useful here because it handles structs too.
--lhf