Pushing a Lua Table

BlitzMax Forums/BlitzMax Programming/Pushing a Lua Table

Ragz(Posted 2006) [#1]
lua_pushstring(state,"object_index")
lua_gettable(state,LUA_GLOBALSINDEX)
	lua_pushnumber(state,index)
	lua_gettable(state,-2)
		lua_pushstring(state,"update")
		lua_gettable(state,-2)
			lua_gettable(state,-1)
			' I need to push the table here.
			Local err:Int = lua_pcall(state,1,0,0)
			If err <> 0 
				Local error:String = String.FromCString(lua_tostring(state,-1))
				lua_pop(state,1)
				Print error
			End If
lua_pop(state,1)


In this example let's say index = 1
And let us say that in lua there is a table:
object_index { [1] = {update = function(self) print(self.test) end, test = "Blaaaaar"} }


How can I pass the 'object_index[1]' table as the first (and only) argument to its own function 'update', in the above code?


taxlerendiosk(Posted 2006) [#2]
I can't be entirely sure, pretty new to Lua myself, but it sounds like you need lua_pushvalue to push a copy of the object table onto the top:
lua_pushstring(state,"object_index")
' ... [ "object_index" ]
lua_gettable(state,LUA_GLOBALSINDEX)
' ... [ {objectindex} ]
lua_pushnumber(state,index)
' ... [ {objectindex} ], [ index ]
lua_gettable(state,-2)
' ... [ {objectindex} ], [ {object} = {objectindex}[index] ]
lua_pushstring(state,"update")
' ... [ {objectindex} ], [ {object} ], [ "update"]
lua_gettable(state,-2)
' ... [ {objectindex} ], [ {object} ], [ update() = {object}.update ]
lua_pushvalue(state,-2)
' ... [ {objectindex} ], [ {object} ], [ update() ], [ {object} ]



Ragz(Posted 2006) [#3]
Ah thanks very much, I didn't know about lua_pushvalue(state, index).


taxlerendiosk(Posted 2006) [#4]
Yeah, it's not obvious by the name what use it has.