Jump to content
  • Advertisement
Sign in to follow this  
Prozak

Lua doesnt pass numbers back?

This topic is 4435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just integrated Lua with a c++ app I'm developing for PDAs, but whenever a lua script tries to return a numeral, on the c++ side of things, all I get is zero, every time. On the other hand I can receive strings with no problem whatsoever... Does any know where under the hood I should look to fix this?

Share this post


Link to post
Share on other sites
Advertisement
Hi,

Can you give us the version # of Lua you're using, any wrapper you're using, etc...?

After executing your function call, what do you call to retrieve your numeral?

Do you call lua_tointeger or lua_tonumber (double) right after your function call?

Cheers

Share this post


Link to post
Share on other sites
From lua.h:
Quote:
#define LUA_VERSION "Lua 5.0"
#define LUA_COPYRIGHT "Copyright (C) 1994-2003 Tecgraf, PUC-Rio"
#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"

I just do a simple:
Quote:

if(lua_isnumber(L, 1))
{
int v = lua_tonumber(L, 1);
}

Share this post


Link to post
Share on other sites
lua_tonumber() returns a double but you're storing it to an int; is the number you're returning from the Lua script less than one? It'd be a silly mistake, true, but that's the only thing that stands out to me from that little example.

If not, post more code (C-side and Lua).

Share this post


Link to post
Share on other sites
Things that pop up in my mind... as jpetrie pointed out you store a double in an int.

If that's a real copy paste of your code, are you sure the right variable gets assigned? as you seem to declare an int v then exit this scope... is that "int" a mistake?

If you're using VC++, what does the Auto tab read as a return value to lua_tonumber after you've stepped over it?

Hope this helps

Eric

Share this post


Link to post
Share on other sites
Are you defining your function as something like:

function add(firstNumber, secondNumber)
return firstNumber + secondNumber;
end



If so are you calling it via:

lua_getglobal(LuaStatePtr, "add");
lua_pushnumber(LuaStatePtr, firstNumber);
lua_pushnumber(LuaStatePtr, secondNumber);
lua_call(LuaStatePtr, 2 /*2 arguments*/, 1 /*1 return value*/);

int nSum = (int)lua_tointeger(LuaStatePtr, -1);



There are some good intro LUA tutorials at gamedevgeek.com.

Hope that helps!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!