Issues with lua_tonumber

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
EddieRingle
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 69
Joined: Sun Apr 10, 2011 5:58 pm
Programming Language of Choice: C
Location: Detroit, MI

Issues with lua_tonumber

Post by EddieRingle »

I've just recently ran into this rather annoying issue, and it has basically halted any further progress on my engine.

In my lua script, I set a variable x to equal a value:

Code: Select all

x = 3
Then, in C/C++, I try to access that value:

Code: Select all

lua_getglobal(m_luaState, "x")
g_console->WriteLine("x: %d", lua_tonumber(m_luaState, -1));
And the output is this:

Code: Select all

x: -16
I can easily remedy this by using lua_tointeger (which returns the correct value). However, tolua makes use of tonumber in it's conversions, so when converting numeric members of my userdata, it returns bad values.
Any ideas?

EDIT: Disregard this. I had -Wno-format set so I wasn't warned about using %d in my WriteLine call when I should've been using %f.
Post Reply