Skip to main content
gamedev.net
🔒 Locked

Function exists in LUA

Started by Lord Faron 1 reply 8.8k views
Original Post
Lord Faron
Lord Faron
I have a little problem with lua. How can I check if a function exists in lua script without calling it? I want to load a script with few functions and call them form time to time, but I must know what functions ar in there. And there's another thing: if I don't call any of the functions in a long time or call just one of them will garbage collector delete the thread?
if (time() == $) { $ = 0; }
jdarling
jdarling
Answers:
From Lua, for a global function:
if (FunctionName ~= nil) then  FunctionName(Args)end

From Lua, for a table contained function:
if (MyTable.FunctionName ~= nil) then  MyTable:FunctionName(Args)end

From source for a global function:
lua_pushstring(L, FunctionName);lua_rawget(L, LUA_GLOBALSINDEX);result := lua_isfunction(L, -1);lua_pop(L, 1);

From source for a table contained function:
lua_pushstring(L, TableName);lua_rawget(L, LUA_GLOBALSINDEX);if lua_istable(L, -1) then  begin    lua_pushstring(L, PChar(FunctionName));    lua_rawget(L, -2);    result := lua_isfunction(L, -1);    lua_pop(L, 1);    lua_pop(L, 1);  endelse  begin    lua_pop(L, 1);    result := false;  end;


- Jeremy
Lord Faron
Lord Faron
Thanks!
if (time() == $) { $ = 0; }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.