Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class 'B'
- function B:InitCount()
- self.count = 1000
- end
- //this - optional paremeter (can be nil) which is a copy (i think reference) of the "self" of the class that set the callback.
- function B:SetCallback(func, this)
- self.callBackFunction = func
- self.callBackSelf = this
- end
- function B:RunCallback()
- if self.callBackFunction then
- self.callBackFunction(self.callBackSelf)
- end
- end
- class 'A'
- local bInstance = B()
- function A:InitCount()
- self.count = 7
- end
- function A:Work()
- if not self.count then
- self.count = 1
- else
- self.count = self.count + 1
- end
- Print(self.count)
- end
- function A:SetCallback()
- bInstance:SetCallback(A.Work, self) //if A:Work() didn't rely on self then it could of been done like this: bInstance:SetCallback(A.Work)
- end
- local aInstance = A()
- A:InitCount() // sets A's self.count to 7
- B:InitCount() // sets B's self.count to 1000
- aInstance:SetCallback() //tells the bInstance how to call the A:Work() function
- bInstance:RunCallback() //runs A:Work() function, prints 8 to console
- bInstance:RunCallback() //runs A:Work() function, prints 9 to console
- bInstance:RunCallback() //runs A:Work() function, prints 10 to console
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement