• Advertisement

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

This topic is 3417 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

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

##### Share on other sites
Advertisement
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.

#### Share this post

##### Share on other sites
Quote:
 Original post by phantomlua_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

##### 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'123true

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

##### Share on other sites
Quote:
 Original post by rip-offPositive stack indices are taken as absolute positions. Negative ones are relative to the top of the stackImagine the following lua stack:'abc'123trueUsing 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

##### Share on other sites
Quote:
 Original post by EmptyVoidOhhhh 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

##### Share on other sites
Quote:
Original post by phantom
Quote:
 Original post by EmptyVoidOhhhh 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

##### 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

##### Share on other sites
Welp, I think we're done here.

#### Share this post

##### Share on other sites

This topic is 3417 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.

This topic is now closed to further replies.

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 10
• 12
• 10
• 10
• 11
• Advertisement