lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Lloyd,

Monday, June 1, 2009, 3:04:39 PM, you wrote:

> Event1(MyEventHandler1)

> function MyEventHandler1(EventObj obj)
> --some action mechanism
> end

this exact code will not work - function definition is just a
assignment of anonymous function object to variable and here you try
to use variable MyEventHandler1 before any assignment so it will pass
just nil to Event1

there are infinite number of ways to support events, i will try to
show the simplest one:

1) in C, you create Lua interpreter:

  L = luaL_newstate();
  luaL_openlibs(L);

2) you run inside this interpreter your script that defines event
handler:

  luaL_dofile (L, "event-handler.lua");

where event-handler.lua is:

function MyEventHandler1(eventObj)
--some action mechanism
end

3) each time you need to call event handler, you run MyEventHandler1
in Lua instance you've created:

    lua_getglobal  (L, "MyEventHandler1");
    lua_pushnumber (L, 123);  // eventObj
    
    if (lua_pcall(L, 1, 1, 0) != 0)
      error(...);

4) at the end:

  lua_close(L);

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin@gmail.com