Owned Lua or Me

16 March 2009 | Capstone

The past few days I have been fighting with Lua to in attempts to create a class file that will allow our group to easily load, and save Lua files as configuration / property / settings files. I have found a few good sites on the way that talk about this kind of thing but, the way I wanted to return the data Lua wasn’t having it. What I intended to do was have Lua format the results of the file in such a way that I could store them in a data structure in c++ and I could then use that structure to store the values from lua, and write out new values to a Lua file if needed.
An example configuration file would look like the one below. This file can be read into my class to provide easy access to the values on the c++ side. To access the values it is something like

foo->GetString(“window.Name”); or foo->GetInt(“window.x”);

--Example
Window = {
	X = 0,
	Y = 0,
	Width = 640,
	Height = 480,
	Name =  "My game",
	FullScreen = false
}

The main thing I wanted to do was have Lua build a table and format the table as follows

tableValues = {
	"window.x",
	"window.y",
	"window.width",
	"window.height",
	"window.name",
	"window.FullScreen"
}
--To get this format is simple, just pass the table name and table to this function
tempValues = {}
tempTableNames = {strTableName}
tempTables = {tblTable}
for i,n in pairs(tempTables) do
	for j,m in pairs(n) do
		if(type(m) == "table") then
			table.insert(tempTableNames,string.format("%s.%s",tempTableNames[i],j))
			table.insert(tempTables,m)
		else
			table.insert(tempValues,string.format("%s.%s",tempTableNames[i],j))
		end
	end
end

The problem was getting that tableValues array back to the c++ side. When viewing the tableValues on the stack the datatype of tableValues was of usertype and the stack would not return a pointer to the data so I could access its raw memory. What I ended up doing was querying lua for each index in the tableValues and getting the string back that way. I am not sure if this is a hack way to do it or not but its one of the only ways that worked for me. So I will let you decide if Lua got the best of me, or I got the best of it.


2 Responses to “Owned Lua or Me”

Leave a Reply