Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

Getting values returned from lua function

This topic is 4838 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 am having some serious problems doing this... heres the relevent code, BTW Lua is initialized globaly and this is in a seprate function if that makes a diffrence Lua = lua_open(); lua_baselibopen(Lua); lua_dofile(Lua, "Entities.lua"); lua_getglobal(Lua, "GetEntity"); lua_pushstring(Lua,Entity_Name.c_str()); lua_pcall(Lua,1,4,0); std::string Type = (string)lua_tostring(Lua, 1); Image_Path = (string)lua_tostring(Lua, 2); Height = (int)lua_tonumber(Lua, 3); Width = (int)lua_tonumber(Lua, 4); lua_pop(Lua,1); I know enough to know (through various tests and what not) that the values arnt getting through I just cant figure out why. this would be my lua code function GetEntity(ENTITY_NAME) TYPE, Height, Width = "NORMAL", 16,14; Image_Path = "PNG/Ball.PNG"; return TYPE, Image_Path, Height, Width; end thx a ton for any help, ive been at this a while and I'm tierd... [grin] O and just so I dont get flamed, yes i know that as of right now I am passing in a value to the function and doing nothing with it, unless that directly effects my problem it doesnt concern you

Share this post


Link to post
Share on other sites
Advertisement
As far I remember the lua stack goes 'backwards'


-1 == stack top
-2 == next
-3 == ...


so lua_tostring(p,-1)
lua_tonumber(p,-2)

or

s = lua_tostring(p,-1)
lua_pop(p,1)
n = lua_tonumber(p,-1)
lua_pop(p,1)

Share this post


Link to post
Share on other sites
I know its not stack order because I get an error no matter which way I do it and I have accounted for the 'backwardsness' of the lua stack (arnt all stacks back ward)

Share this post


Link to post
Share on other sites
Your stack indices are indexing from the bottom of the stack.

You need to index from the top.

The last return value (Width) is at the top of the stack (-1).

The next (Height) is at index -2.

etc.

Also, the best way to cleanup after a function call is:

int top = lua_gettop( L );

... call function ...

lua_settop( top );

That way you don't have to keep track of the number of lua_pop()'s that you need.

HTH

Share this post


Link to post
Share on other sites
ok maybe I wasnt very good at explaining my problem. The fact is that I cant retreive any values returned by lua or my program crashes, I could have to do with the fact that this function that im using here (C++ side) is a constructor for a global object. I really think that my problem has to do more with the way that the lua decleration and its implementation is split rather then a stack issue. maybe just an example, doesnt have to be complicated AT ALL just where/how to call lua from a class constructor.

thx again =)

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!