Reputation: 1097
I want to be able to do the following:
MyObject:SetSize( { 10.0, 20.0 } )
But when I iterate over this on the C-side (SetSize is a C function). The order of the parameters is random.
This is the C++ side processing the table:
glm::vec2 State::PopVec2()
{
glm::vec2 v();
lua_pushnil( ls ); // first key
int i = 0;
while( lua_next( ls, -2 ) ) // pops key and pushes next key and value
{
// v[0] is x coordinate. v[1] is y coordinate.
v[i] = (float)lua_tonumber( ls, -1 ); // get number
lua_pop( ls, 1 );// pop value but leave next key.
++i;
}
lua_pop( ls, 1 ); // pop table
return v;
}
How could I ensure consistent order without having to mess up the code on the lua side? (So basically solve this on the C++ side)
Or alternatively, what should I use as a "Vec2" equivalent on the lua side?
Upvotes: 2
Views: 325
Reputation: 146968
You should push 1 and 2 on to the stack (or 0,1 if tables are 0indexed) and use lua_geti instead of iterating over the table with lua_next. Another example of how your current code is incorrect is what happens if the Lua user passes {1, 2, 3}
? You would be accessing the 3rd element of a 2-element vector.
Upvotes: 3