Sign in to follow this  
EmptyVoid

How to find out if a lua function was defined in a script?

Recommended Posts

Well I need to find out if a function was defined inside my lua script, I tried this:
lua_getfield(g_Lua, LUA_GLOBALSINDEX, "On_Start");
if(lua_isfunction(g_Lua, LUA_GLOBALSINDEX))
{
// Function is defind
}

I thought this would get the index of On_Start and set it to the LUA_GLOBALSINDEX then check if it was a function using lua_isfunction but it returns 0 even though I defined it, so I can be pretty sure thats not the way to do it... Does anyone know how to do this?

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
lua_isfunction operates on the stack, as such the index given is an index into the stack, where as 'LUA_GLOBALSINDEX' is a pseudeo-index to a table.


Ohhhh even though I think you were trying to confuse me that actually helped:


lua_getfield(g_Lua, LUA_GLOBALSINDEX, "On_Start");
if(lua_isfunction(g_Lua, -1))
{
// Function is defind
}




I still don't understand but I think -1 is the end of the global index kinda like when you add a C function to lua and you have to read backward. I don't know why 0 is not valid and you have to use -1 but oh well... I'd be interested in a explanation if anyone knows why?

Share this post


Link to post
Share on other sites
Positive stack indices are taken as absolute positions. Negative ones are relative to the top of the stack

Imagine the following lua stack:

'abc'
123
true

Using positive indices, we need to use 1 to access "true", 2 to access "123" and 3 to access the string 'abc'. However, using negative values we can use -1 to access 'abc', -2 to access 123 and -3 to access true.

So, when you have just pushed a value on the stack (e.g. getglobal()), you wat to access the top of the stack. Instead of calling gettop(L) to find out what index it is at, you can just use -1, and this will always be the top.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Positive stack indices are taken as absolute positions. Negative ones are relative to the top of the stack

Imagine the following lua stack:

'abc'
123
true

Using positive indices, we need to use 1 to access "true", 2 to access "123" and 3 to access the string 'abc'. However, using negative values we can use -1 to access 'abc', -2 to access 123 and -3 to access true.

So, when you have just pushed a value on the stack (e.g. getglobal()), you wat to access the top of the stack. Instead of calling gettop(L) to find out what index it is at, you can just use -1, and this will always be the top.


Oh thanks now I understand! [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by EmptyVoid
Ohhhh even though I think you were trying to confuse me that actually helped:


No, I was giving you the information to figure it out for yourself. It's hardly my fault you are poking around in a language without bothering to learn how it works first so that a legitmate answer makes you feel confused.

Jesus... you've just made my 'never help again' list.

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
Quote:
Original post by EmptyVoid
Ohhhh even though I think you were trying to confuse me that actually helped:


No, I was giving you the information to figure it out for yourself. It's hardly my fault you are poking around in a language without bothering to learn how it works first so that a legitmate answer makes you feel confused.

Jesus... you've just made my 'never help again' list.


You do realize the word "pseudeo-index" or even "pseudeo index" has never been used on the entire internet?

Also I thought I was already on that list. [lol]

Share this post


Link to post
Share on other sites
oh big woop! I made a typo. Also if you had searched for "pseudeo index" google would have asked 'do you mean pseudo index?' with a link which gives usage.

And if you had combined THAT with the keyword 'lua' it would have given you, as the 3rd link down, a link to the online edition of 'programming in lua' which would have expained it futher.

But, apprently my mistake was thinking you wanted to learn, not just blunder around like an idiot, not learning anything, and insulting and offending those who want to help.

My mistake, it won't happen again.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this