Custom events across contexts

DonQuiche

Emperor
Joined
May 23, 2011
Messages
1,122
Hello everyone.

I am trying to use children contexts within my main context (through the LuaContext markup). However, I need a way for the parent to notify the children about some custom events (mod's popup opened / closed).

Since all lua variables seem to be only visible from the declaring context, is there a way for me to achieve this ?
 
Finally found it, thanks to Modularized DiploCorner

For those seeking the same answer... LuaEvents is a "magic" object: as soon as a member is accessed in LuaEvents, it is automatically created (that is, no need to assign it). The created object can be invoked as a function and also has an Add method (and a Remove one I guess).

Example:

Code:
-- Subscriber.lua
local function OnMyCustomEvent()
   print("it work")
end
LuaEvents.MyCustomEvent.Add(OnMyCustomEvent);


-- Notifier.lua
LuaEvents.MyCustomEvent();

Subscriber.lua and Notifier.lua can be in two different contexts. Note that the MyCustomEvent member has never been assigned: instead it has been dynamically created the first time we accessed it.

See Lua metamethods if you want to learn the magic behind it.
 
Back
Top Bottom