Page 1 of 1

Callback functions don't like ':'

Posted: Sun Apr 25, 2010 10:03 pm
by TheMartian
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

Re: Callback functions don't like ':'

Posted: Mon Apr 26, 2010 8:51 am
by Allanon
Hello,
I think it's because if you write

Code: Select all

x:cheese
is the same as writing

Code: Select all

cheese(x)
so your code

Code: Select all

SetTimeOut(Nil,x:cheese,1000)
is the same as

Code: Select all

SetTimeOut(Nil, cheese(x), 1000)
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

Re: Callback functions don't like ':'

Posted: Mon Apr 26, 2010 5:22 pm
by TheMartian
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 :P

regards
Jesper

Re: Callback functions don't like ':'

Posted: Tue Apr 27, 2010 12:23 am
by Allanon
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:

Code: Select all

MyObject = Object:New()
...
MyObject:InstallEvent(OnMouseMove)
...
MyObject:Disinstall(OnMouseMove)
Just an idea, I'm not sure if this could be the right direction :)

Re: Callback functions don't like ':'

Posted: Tue Apr 27, 2010 8:04 pm
by TheMartian
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

Re: Callback functions don't like ':'

Posted: Sat May 01, 2010 2:03 pm
by TheMartian
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