lua-users home
lua-l archive

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


On Tue, Jul 19, 2011 at 19:44, Eddie Ringle <eddie@eringle.net> wrote:
> Recently ran into this issue, and it's prevented me from continuing
> development on my engine.
> Let's say I do this in a script:
> x = 3
> And in C/C++, I do this to retrieve the value:
> lua_getglobal(m_luaState, "x")
> g_console->WriteLine("x: %d", lua_tonumber(m_luaState, -1));
> The output is then this:
> x: -16
> The problem seems to be using lua_tonumber. If I use lua_tointeger, the
> correct value is returned. However, I use tolua++ to give Lua access to some
> of my C/C++ types, and I'm encountering this issue with these usertypes. I
> believe this is because tolua uses lua_tonumber underneath.
> Why is lua_tonumber behaving this way (i.e., returning seemingly random
> values)?
> Thanks,
> Eddie Ringle
>

lua_tonumber returns a lua_Number, which is usually a double. You're
then printing it with the %d format, which expects an integer.
Compilers usually warn you about that kind of mismatch, but maybe
yours didn't...

-- 
Sent from my toaster.