evtmatch fails on using variable:function

Report any Hollywood bugs here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

evtmatch fails on using variable:function

Post by Bugala »

Heres a test code:

Code: Select all

Bubbles = {}


Function Bubbles:ChangeText()
EndFunction

Function p_myfunc()
EndFunction

evt1 = { OnMouseUp = Bubbles:ChangeText(), OnMouseOver = Bubbles:ChangeText(),
            OnRightMouseUp = p_myfunc }
evt2 = { OnMouseUp = Bubbles:ChangeText(), OnMouseOver = Bubbles:ChangeText(),
            OnRightMouseUp = Bubbles:ChangeText() }

MakeButton(0, #SIMPLEBUTTON, 100, 100, 200, 200, evt1)
MakeButton(0, #SIMPLEBUTTON, 300, 300, 400, 400, evt2)

As you can see, First makebutton works fine, but when the second button is made, Hollywood reports an error of "Wrong usage/parameters for this command! Read the documentation!"


Only difference between these two is, that in First button i have used Bubbles:ChangeText() on all but one, and left one of them be p_myfunc (from Hollywood documentation example of makebutton command).

If I use Bubbles:ChangeText() on all of the options, then it fails.

Is this a bug, or expected behavior?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: evtmatch fails on using variable:function

Post by Bugala »

I guess this is basically expected behavior, although perhaps not intentioned.

For I also noticed that when using VARIABLE:FUNCTION as the activated function, it wont pass the MSG part to the function.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: evtmatch fails on using variable:function

Post by airsoftsoftwair »

This is a bug, but in your code ;) Whenever you use the "()" parentheses, Hollywood will actually *call* your function. But that's not what you want here. You want to have a reference to the function. Furthermore, the colon operator is only allowed in function declarations and when calling functions and not when referencing them. Hence, you'd have to write the code as follows:

Code: Select all

evt1 = { OnMouseUp = Bubbles.ChangeText, OnMouseOver = Bubbles.ChangeText,
            OnRightMouseUp = p_myfunc }
evt2 = { OnMouseUp = Bubbles.ChangeText, OnMouseOver = Bubbles.ChangeText,
            OnRightMouseUp = Bubbles.ChangeText }
Still, this won't work correctly because the colon operator ":" declares a method which always gets a self parameter as the first argument, see here:
http://www.hollywood-mal.com/docs/html/ ... thods.html

But this won't work in an event handler. You cannot use methods in event handlers because there is no notion of an owner table in the event handler context. Thus, you need to do it like this:

Code: Select all

InstallEventHandler({OnMouseUp = p_MyFunc})
If you want to use methods, then you need to set up some glue code that handles it for you. This is possible by making use of the "userdata" feature of InstallEventHandler(), i.e. like this:

Code: Select all


Bubbles = {}

Function Bubbles:ChangeText(msg)
	DebugPrint(msg.action)
EndFunction

Function p_MyFunc(msg)
	Local t = msg.userdata
	t:ChangeText(msg)
EndFunction
	
InstallEventHandler({OnMouseUp = p_MyFunc}, Bubbles)
See here for details on how this works:
http://www.hollywood-mal.com/docs/html/ ... ndler.html
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: evtmatch fails on using variable:function

Post by Bugala »

Okay, thanks. That explains it. Was suspecting something about like that, but thanks for clarifying the issue.
Post Reply