Hi
I am not sure if this is the correct behaviour or not, since callback routines don't allow explicit parameters. But what about the implicit 'self' parameter?
In the example below it works if I write: SetTimeOut(Nil,x.cheese,1000), but SetTimeOut(Nil,x:cheese,1000) fails.
x={}
Function x:cheese()
NPrint("Cheese")
EndFunction
NPrint("Here we go")
SetTimeout(Nil,x:cheese,1000)
While True
WaitEvent
Wend
regards
Jesper
Callback functions don't like ':'
Re: Callback functions don't like ':'
Hello,
I think it's because if you write
is the same as writing
so your code
is the same as
Right now I've no time to deeply check what you can do to resolve your problem, but the error you get is caused by passing the result of the function and not the function itself.
Hope this helps!
Fabio
I think it's because if you write
Code: Select all
x:cheese
Code: Select all
cheese(x)
Code: Select all
SetTimeOut(Nil,x:cheese,1000)
Code: Select all
SetTimeOut(Nil, cheese(x), 1000)
Hope this helps!
Fabio
----------------------------
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
- TheMartian
- Posts: 109
- Joined: Sun Feb 28, 2010 12:51 pm
Re: Callback functions don't like ':'
Hi
Yes, as you say it effectively passes a parameter representing the object itself, and callback function don't like that, so I guess that kills of that approach. I was just wondering if I could pass a 'self' parameter as part of a callback function because I was getting into programming in Hollywood in OO-style. For what I was doing the '.' notation was sufficient, so it is no big deal. I suppose that if I really need a reference to the object itself I have to pass it via a global variable. OO-style purists will probably kill me
regards
Jesper
Yes, as you say it effectively passes a parameter representing the object itself, and callback function don't like that, so I guess that kills of that approach. I was just wondering if I could pass a 'self' parameter as part of a callback function because I was getting into programming in Hollywood in OO-style. For what I was doing the '.' notation was sufficient, so it is no big deal. I suppose that if I really need a reference to the object itself I have to pass it via a global variable. OO-style purists will probably kill me
regards
Jesper
Re: Callback functions don't like ':'
Globals are bad things!!!
I'm sure there is a way to work with : and callback functions, maybe using a method to install and disinstall events on your objects, something like:
Just an idea, I'm not sure if this could be the right direction
I'm sure there is a way to work with : and callback functions, maybe using a method to install and disinstall events on your objects, something like:
Code: Select all
MyObject = Object:New()
...
MyObject:InstallEvent(OnMouseMove)
...
MyObject:Disinstall(OnMouseMove)
----------------------------
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
[Allanon] Fabio Falcucci | GitHub for recent works | Support me on Patreon for Hollywood libraries | ☆★ All my links ★☆
- TheMartian
- Posts: 109
- Joined: Sun Feb 28, 2010 12:51 pm
Re: Callback functions don't like ':'
Hi
Yes, that might be the way to go. For now I have sidestepped the issue and just created an eventclass with two methods. One is for handling keys and one for mouse events. Each method decides by itself or is fed the object to act on, and then each object class has its own 'keyaction' or mouseaction' methods as well as sets of permissable keys for the object class (chars and strings). So if input is acceptable it just call object:keyaction(msg.key) and off it goes to do its thing.
In the main script is then a single InstallEventHandler({OnKeyDown=eventclass.keyeventhandler}) statement (plus other events that the program listens for.) This is not OO-style, but then I don't expect to do everything right in the first iterations of the program, so your suggestion is noted and I will try something like that shortly - thanks.
regards
Jesper
Yes, that might be the way to go. For now I have sidestepped the issue and just created an eventclass with two methods. One is for handling keys and one for mouse events. Each method decides by itself or is fed the object to act on, and then each object class has its own 'keyaction' or mouseaction' methods as well as sets of permissable keys for the object class (chars and strings). So if input is acceptable it just call object:keyaction(msg.key) and off it goes to do its thing.
In the main script is then a single InstallEventHandler({OnKeyDown=eventclass.keyeventhandler}) statement (plus other events that the program listens for.) This is not OO-style, but then I don't expect to do everything right in the first iterations of the program, so your suggestion is noted and I will try something like that shortly - thanks.
regards
Jesper
- TheMartian
- Posts: 109
- Joined: Sun Feb 28, 2010 12:51 pm
Re: Callback functions don't like ':'
Hi
Just for completeness...
Some experiments have now taught me that the 'self' parameter can be mimicked by taking advantage of the 'userdata' parameter because you can pass the object itself in 'userdata'. Then the callback function can refer to 'msg.userdata in the same way you would use the 'self' parameter normally. An example...
myObject={}
myObject.txt$="Hello World"
Function myObject.eventHandler(msg)
NPrint(msg.userdata.txt$)
EndFunction
eventstring={OnKeyDown=myObject.eventHandler}
InstallEventHandler(eventstring,myObject)
While True
WaitEvent
Wend
End
regards
Jesper
Just for completeness...
Some experiments have now taught me that the 'self' parameter can be mimicked by taking advantage of the 'userdata' parameter because you can pass the object itself in 'userdata'. Then the callback function can refer to 'msg.userdata in the same way you would use the 'self' parameter normally. An example...
myObject={}
myObject.txt$="Hello World"
Function myObject.eventHandler(msg)
NPrint(msg.userdata.txt$)
EndFunction
eventstring={OnKeyDown=myObject.eventHandler}
InstallEventHandler(eventstring,myObject)
While True
WaitEvent
Wend
End
regards
Jesper