Game Development Reference
In-Depth Information
To assign a value to a value to a
LuaObject
, use the
Assign*()
functions:
n
void AssignNil(LuaState* state)
n
void AssignBoolean(LuaState* state, bool value)
n
void AssignInteger(LuaState* state, int value)
n
void AssignNumber(LuaState* state, double value)
n
void AssignString(LuaState* state, const char* value)
n
void AssignWString(LuaState* state, const wchar_t* value)
n
void AssignUserData(LuaState* state, void* value)
n
void AssignLightUserData(LuaState* state, void* value)
n
void AssignObject(LuaState* state, LuaObject& value)
n
void AssignNewTable(LuaState* state, int narray = 0, int nhash = 0)
Notice that the various assignment functions require a
LuaState
pointer. This is
because every valid value must be attached to a state in Lua, so when you create a
new value, you tell LuaPlus where to attach it.
LuaObject
has a number of functions and operators specifically written to help deal
with tables. The easiest way to look up a value on a table is to use the overloaded
array access operator. You can also use the
GetByName()
,
GetByObject()
,or
GetByIndex()
functions to retrieve a value from the table. For example, let
'
s say
we have the following table in Lua:
positionVec ={x=10,y=15}
Let
s also say that this value is stored in a
LuaObject
called
positionTable
.We
can access fields on this table like so:
'
GCC_ASSERT(positionTable.IsTable());
// safety first
LuaObject x = positionTable[
“
x
”
];
// this is one way
LuaObject y = positionTable.GetByName(
“
y
”
); // here
'
s another
// let
s fill up a Vec2:
GCC_ASSERT(x.IsNumber() && y.IsNumber()); // more type checking
Vec2 vec(x.GetFloat(), y.GetFloat());
'
That
s all there is to it. Of course, without any type safety, you need to handle all the
error checking yourself.
'