Testing for the presence of lua functions from C

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing for the presence of lua functions from C

Benedict Walmisley
I'm considering doing some sort of callbacks from my game to the 
level script (for events). What I need is some kind of 
lua_isFunctionPresent.

What I'm considering is something along the lines of:

function LoadLevel()
         -- do stuff
         object = LoadObject("object\fred.x","fredname")
         --
end

function fredname_IsDestroyed()
         -- do something
end

function fredname_IsHitByEnemy()
         -- do something else
end

So when "fredname" is destroyed something like:

if( lua_isFunctionPresent("fredname_IsDestroyed")
      {
      lua_fn = lua_getglobal("fredname_IsDestroyed");
      lua_callfunction(lua_fn);
      }

gets called.

Am I barking up the wrong tree here? No doubt the lua gods will say 
theres some devastatingly simple way to do it (there usually is...) 
but I cannot find it.

Benedict


Reply | Threaded
Open this post in threaded view
|

RE: Testing for the presence of lua functions from C

Vincent Penquerc'h-3
> if( lua_isFunctionPresent("fredname_IsDestroyed")
>       {
>       lua_fn = lua_getglobal("fredname_IsDestroyed");
>       lua_callfunction(lua_fn);
>       }

off the top of my head:

lua_fn = lua_getglobal("fredname_IsDestroyed");
if (lua_fn != LUA_NOOBJECT) {
  lua_callfunction(lua_fn);
}

I do exactly the same type of things for my game, and I cache the existence
of these callbacks, so I don't have to getglobal usually, except when the
Lua script adds/removed callbacks, in which cases it calls a routine to
reread
the present callbacks and recache them.

--
Vincent Penquerc'h


Reply | Threaded
Open this post in threaded view
|

Re: Testing for the presence of lua functions from C

Edgar Toernig
In reply to this post by Benedict Walmisley
[hidden email] wrote:
>
> So when "fredname" is destroyed something like:
> 
> if( lua_isFunctionPresent("fredname_IsDestroyed")
>       {
>       lua_fn = lua_getglobal("fredname_IsDestroyed");
>       lua_callfunction(lua_fn);
>       }

lua_getglobal(L, "fredname_IsDestroyed");	// you get nil if undefined
if (lua_type(L, -1) != LUA_TNIL)		// or: ... == LUA_TFUNCTION
    lua_call(L, 0, 0);
else
    lua_pop(L, 1);

Ciao, ET.



Reply | Threaded
Open this post in threaded view
|

Re: Testing for the presence of lua functions from C

miles
In reply to this post by Benedict Walmisley
How about this?

	if (fredname_IsDestroyed) then
		fredname_IsDestroyed()
	end

Miles Johnson [[hidden email]]
When you put down one mother, you puttin down mothers all over the world!
-- Mr T.

On Mon, 19 Feb 2001 [hidden email] wrote:

> I'm considering doing some sort of callbacks from my game to the 
> level script (for events). What I need is some kind of 
> lua_isFunctionPresent.
> 
> What I'm considering is something along the lines of:
> 
> function LoadLevel()
>          -- do stuff
>          object = LoadObject("object\fred.x","fredname")
>          --
> end
> 
> function fredname_IsDestroyed()
>          -- do something
> end
> 
> function fredname_IsHitByEnemy()
>          -- do something else
> end
> 
> So when "fredname" is destroyed something like:
> 
> if( lua_isFunctionPresent("fredname_IsDestroyed")
>       {
>       lua_fn = lua_getglobal("fredname_IsDestroyed");
>       lua_callfunction(lua_fn);
>       }
> 
> gets called.
> Am I barking up the wrong tree here? No doubt the lua gods will say 
> theres some devastatingly simple way to do it (there usually is...) 
> but I cannot find it.
> 
> Benedict
>