Game Development Reference
In-Depth Information
You can also set a field on a table with the various Set*() functions:
n void SetNil(const char* key)
n void SetBoolean(const char* key, bool value)
n void SetInteger(const char* key, int value)
n void SetNumber(const char* key, double value)
n void SetString(const char* key, const char* value)
n void SetWString(const char* key, const wchar_t* value)
n void SetUserData(const char* key, void* value)
n void SetLightUserData(const char* key, void* value)
n void SetObject(const char* key, LuaObject& value)
To add a z field to the table above, you would do the following:
positionTable.SetNumber(
, 0);
Iterating through tables is possible with the use of LuaPlus
z
'
s LuaTableIterator .
It
'
s somewhat similar in form to STL iterators, but it is not STL compliant. Here
'
s
an example that loops through an entire table:
// set up a test table in Lua and read it into a LuaObject
pLuaState->DoString(
birthdayList = { John =
'
Superman
'
, Mary =
'
Batman
'
}
);
LuaObject table = pLuaState->GetGlobals().GetByName(
globalPosition
);
// loop through the table, printing out the pair
for (LuaTableIterator it(table); it; it.Next())
{
LuaObject key = it.GetKey();
LuaObject value = it.GetValue();
// do whatever you want with the objects
}
As you can see, looping through a table is relatively straightforward. This is a huge
improvement from the Lua C API with the various lua_next() and lua_pop() calls.
Globals
The previous example had a glaring hole in it. I showed you a table in Lua and how
to get values from the C++ representation, but I didn
t show you how to actually read
a Lua variable in C++. In order to do that, I need to pull the curtain back a bit and
show you how global variables are actually stored in Lua.
'
 
 
Search WWH ::




Custom Search