lua-users home
lua-l archive

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


> 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