Hello,
I want to create an interface to handle events such as net events.
And if it receives net events, it can handle related event, run callback functions to notify other modules.
How do I do this?
For example, we often use interface to deal with APIs.
I wonder how interface works in corona?
Thanks.
One way to do this is you could implement the modules as event listeners to the interface. When net events are received, the interface broadcasts messages to these listening modules.
EventDispatcher: https://github.com/daveyang/EventDispatcher
For your second question, are you talking about the façade (design) pattern and how to implement it in Lua?
Dave
Okay,
So every request function has a listener already.
It is not necessary to create an interface to handle the callback.
But is it possible to implement an interface in Lua?
Perhaps I misunderstood your questions… When you say “interface”, are you referring to:
network interface (such as Node.js http.request() localAddress option),
class interface,
user interface,
a façade object to deal with varies APIs,
or something else?
Maybe if you provide your use case, I can better understand what you’re looking for.
Cheers,
Dave
For example, an interface in Java, you need to implement it to your class
public class MyClass implements MyInterface { }
So MyInterface can callback to MyClass when needed.
This is how we deal with varies APIs as well, isn’t it?
Lua doesn’t have interface like that in Java or other languages. There isn’t even a class, although there are various libraries to help implementing class-like code: http://lua-users.org/wiki/ObjectOrientedProgramming
If you haven’t already read up on Programming in Lua (http://amzn.com/859037985X ), you can read the first edition online, this chapter on “class”:
http://www.lua.org/pil/16.1.html
Lua is simple and efficient, and you just have to “think different” in structuring your code. There’s no need to implement what the interface dictates as there’s no contract in that sense.
Thanks for reply.
I still have a question.
What is the easy way to “callback” to wherever needed without interface?
I tried to set some flags and check them frequently… like in onEnterFrame()
But I wonder if we can do the callback function or there are other better ways?
A callback is simply a function reference that is passed to wherever you want to call back to it.
I’m not sure why you need callback in onEnterFrame(), can’t you simply call the function?
Perhaps if you provide a simple code snippet of what you’re doing…?
Btw, an interface doesn’t call back, it’s simply a contract for a class of what to implement.
Hi,
You have a number of options:
Some modules:
https://github.com/daveyang/EventDispatcher
https://github.com/develephant/Eventable
Add events to the Runtime listener (CoronaSDK):
https://docs.coronalabs.com/daily/guide/events/detectEvents/index.html
Another method is to use an “empty” display group as a dispatcher. But I would look at those links first.
Hope that helps.
Cheers.
Quantumwave:
A callback is simply a function reference that is passed to wherever you want to call back to it.
I’m not sure why you need callback in onEnterFrame(), can’t you simply call the function?
Perhaps if you provide a simple code snippet of what you’re doing…?
Btw, an interface doesn’t call back, it’s simply a contract for a class of what to implement.
Thanks for reply.
Interfaces can be used for callback function in Java/C++.
For example, ClassA implements InterfaceB, so ClassC can call functions via InterfaceB to notify ClassA when needed.
As for why I want to do this is:
Module1 uses Module2 to do some calculation and animations for it.
But Module1 doesn’t know when the jobs are done.
So I want Module2 to call Module1’s functions when needed.
And a lot of things like this.
The thought of using enterFrame is to check some flag to know which job is done. but I don’t like it.
After checking EventDispatcher, I still have some questions:
1.How do I accomplish this? (either by EventDispatcher or not):
Module1 uses Module2 to do some calculation and animations for it.
But Module1 doesn’t know when the jobs are done.
So I want Module2 to call Module1’s functions when needed.
2.Why can we use this?
local EvtD = require "EventDispatcher" local dispatcher = EvtD() -- This doesn't even use EvtD:init() or something like that?
In general, if I do this it will cause error on the second line:
local myModule = require "module1" local test = myModule() -- Runtime error: attempt to call local 'myModule', a table value.
Thanks for reading.
You can do it like this:
[lua]-- main.lua
local M1 = require(“Module1”)
M1.doIt()
[/lua]
[lua]-- Module1
local Module1 = {}
local M2 = require(“Module2”)
local function doSomethingAfterAnim(event)
print(“doSomethingAfterAnim”)
– event.name = “doneAnim”
– event.target = this function: doSomethingAfterAnim
– event.source = M2
end
Module1.doIt = function()
M2:addEventListener(“doneAnim”, doSomethingAfterAnim)
M2.animate()
end
return Module1
[/lua]
[lua]-- Module2 is event dispatcher
local Module2 = require(“EventDispatcher”)()
Module2.animate = function()
– do calculations and animation; when done, dispatch the event:
Module2:dispatchEvent(“doneAnim”)
end
return Module2
[/lua]
For your second question, when you do this:
[lua]local EvtD = require “EventDispatcher”
local dispatcher = EvtD()
[/lua]
EvtD() automatically calls the init() method.
Hope this helps,
Dave
And for your last question, when a regular module returns a table, and you can’t simply create an instance from the table.
EventDispatcher adds the __call metamethod to call init(), and it turns the table into a dispatcher object; that’s why you can use the EvtD() syntax.
Thanks bunches develephant and Dave.
I used another way before you reply.
I just added a pointer (a module) as an argument for function of module2.
So the module2 knows who is the caller.
But this is not ideal I guess?
From your module sample, if I add listener to mutli-module, when the dispatcher “broadcasts”,
all those modules’ callback function will be called, right?
Passing a function reference is what I’d do for simple callbacks.
Yes, the event dispatcher broadcasts messages to multiple listeners if they’re registered for the event. Also, multiple parameters can be passed and the listener can access other properties not available in the simple callback.
Quantumwave:
Passing a function reference is what I’d do for simple callbacks.
Yes, the event dispatcher broadcasts messages to multiple listeners if they’re registered for the event. Also, multiple parameters can be passed and the listener can access other properties not available in the simple callback.
Thanks.
I already tried it by using function callback(event, …)
This works perfectly.
I will mark this solved.
One way to do this is you could implement the modules as event listeners to the interface. When net events are received, the interface broadcasts messages to these listening modules.
EventDispatcher: https://github.com/daveyang/EventDispatcher
For your second question, are you talking about the façade (design) pattern and how to implement it in Lua?
Dave
Okay,
So every request function has a listener already.
It is not necessary to create an interface to handle the callback.
But is it possible to implement an interface in Lua?
Perhaps I misunderstood your questions… When you say “interface”, are you referring to:
network interface (such as Node.js http.request() localAddress option),
class interface,
user interface,
a façade object to deal with varies APIs,
or something else?
Maybe if you provide your use case, I can better understand what you’re looking for.
Cheers,
Dave