lua-users home
lua-l archive

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



void setTableField(lua_State* l,const char* index,double value)
{
	lua_pushstring(l,index);
	lua_pushnumber(l,value);
	lua_settable(l, -3);
}

> 
> As a newbee, I can't figure this one out!
> 
> What have I missed ? I want this C func return
> a lua table.
> 
> -- C code
> 
> int l_GetPosTable(lua_State* l)
> {
> 	printf("Create table\n");
> 	lua_newtable(l);
> 
> 	// Position
> 	setTableField(l,"px",1.0);
> 	setTableField(l,"py",233.33);
> 	setTableField(l,"pz",3.0);
> 
> 	// X orientation vector
> 	setTableField(l,"xx",1.0);
> 	setTableField(l,"xy",0.0);
> 	setTableField(l,"xz",0.0);
> 
> 	// Y orientation vector
> 	setTableField(l,"yx",0.0);
> 	setTableField(l,"yy",1.0);
> 	setTableField(l,"yz",0.0);
> 
> 	// Z orientation vector
> 	setTableField(l,"zx",0.0);
> 	setTableField(l,"zy",0.0);
> 	setTableField(l,"zz",1.0);
> 
> 	return 1;
> }
> 
> -- end C code
> 
> And in my lua script I do like this!
> 
> local tpos = GetPosTable()
> print(tpos) -- says it's a table :)
> print(tpos[1]) -- says nil :(
> 
> /Anders E.
>