LuaIs similarC # eventsThe Mechanism learning tutorial is the content to be introduced in this article, mainly to learnEventInLuaFor more information, see.LuaThe syntax of metamethod is very flexible. It can be used to simulate the features of many languages.
C #We use the event as follows:
- xxx.Click += new System.EventHandler(xxx_Click);
-
- private void xxx_Click(object sender, EventArgs e)
- {
- /**/
- }
To achieve the same effect in Lua and support event multicast mechanism, the key is to rewrite metamethod _ call so that not only functions can be called, but also tables can be called.
The main idea is to use a table to save several response functions for event registration, and then use the table as the function to call it. After rewriting _ call, implement traversal and execution of the Registration Method in the table when calling the table.
It needs to be executed on lua5.0 or lua.net, and lua 5.1 is slightly changed.
- test.lua
- do
Event prototype object. All events are generated by this prototype.
- Event = {}
-
- Function Event: New ()
- Local event = {}
- Setretriable (event, self)
- -- Overwrite _ index Logic
- Selfself. _ index = self
- -- Overwrite _ call logic
- Selfself. _ call = self. Call
- Return event
- End
Event registration: registers the response method to the event.
@ Source: the object to which the response method belongs
@ Func: Response Method
- function Event:Add(source, func)
- table.insert(self, {source, func})
- end
Internal method. The default _ call logic is rewritten. When an event is triggered, the response method registered in the event is cyclically executed.
@ Table: when an object is called, it is passed in.
@: Call Parameters
- Function Event. Call (table ,)
- For _, item in ipairs (table) do
- -- Item [1] is the source, item [2] is the func RESPONSE METHOD
- -- Unpack (arg) is not required in lua 5.1 and can be directly used.
- Item [2] (item [1], unpack (arg ))
- End
- End
The following are test cases:
Create a window object and register the Click Event of the button
- Window = {
- Name = "Simonw's Window",
- }
- function Window:Init()
Register the event. self is the Window object source.
- Button.ClickEvent:Add(self, self.Button_OnClick)
-
Event Response Method. sender is the passed Button object.
- function Window:Button_OnClick(sender)
- print(sender.Name.." Click On "..self.Name)
- end
Create a button object with events such as ClickEvent
- Button = {
- Name = "A Button ",
- -- Create an event
- ClickEvent = Event: New (),
- }
Execute the button action
- Function Button: Click ()
- Print ('click begin ')
- -- Trigger the event. self is the sender parameter.
- Self. ClickEvent (self)
- Print ('click end ')
- End
Execute from here
- Window:Init()
- Button:Click()
- --[[
Execution result:
- > dofile 'test.lua'
- Click begin
- A Button Click On Simonw's Window
- Click end
- ]]
-
- end
Summary: AnalysisLuaIs similarC # eventsThe content of the Mechanism learning tutorial is complete. I hope this article will help you!