lua-users home
lua-l archive

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


Someone has already pointed out how to access global variables.

If, however, what you were looking for is a method of passing named (and/or
optional) arguments to a C function, then simply have the Lua script pass a
table to the C function.  Then from inside the C function you can check for
specifically named items in the table and/or iterate through the whole
table.  As for not knowing the types ahead of time, you can use lua_type to
check the types of the items in the table (or anything in a "normal"
parameter list).

If you wish to allow a function to be called by specifying the parameters in
the "default" order, and to also allow it to be called using "named"
arguments, then you merely need to be able to distinguish the case of
receiving a single table as the only actual argument from the case of
receiving a "full" list of parameters.  If the only "actual" argument is a
table then you search the table for named parameters, otherwise you wander
through the list of arguments in order.  In order to simplify your C code it
would be a good idea to write a simple helper function that translated to or
from the table form.

Lua even supplies "syntactic sugar" for this.  For instance...

f{x=1, y=2, z=3}

turns into

f({x=1, y=2, z=3})

which means that script writers can easily switch from simple parameter
ordering to explicit parameter naming by just adding Name= prefixes to
parameters and switching from calling using () to calling using {}.

PS: Hopefully your mail reader will render parenthesis() noticeably
different from "curly braces"{}.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Mike Atamas
Sent: Friday, September 12, 2003 5:17 AM
To: lua@bazar2.conectiva.com.br
Subject: C, Lua, variable passing


Hello,
	I am writing a C program which needs to get certain variables from a lua
script that it runs (lua_dofile). I read in the manual that you would get
this using the index of the variable. However, I have several variables and
I do not always know what order they will be in or what values they could
store. Is it possible to have lua pass C a variable knowing only the name of
the lua variable?

Mike
aelfgar@aelfgar.com