Page 1 of 2

Unexpected result when calling a handler in a group script

Posted: Fri Oct 05, 2018 3:05 pm
by Simon Knight
I have created a simple group that comprises two fields and a handler in the group. When called the handler returns the value in the field. Well that was the plan. The handler is called using the dispatch command.

The code in the group script reads data from the field "Destination" which is part of the group :

Code: Select all

On DestinationVolume
   return field "Destination" of me
end DestinationVolume
This handler is called using the following button code:

Code: Select all

on mouseUp pButtonNumber
   dispatch "DestinationVolume" to group "SelectedVolumes"
   put  the result into tDestinationVol 
   answer   tDestinationVol 
end mouseUp
This displays the contents of the field as expected. So far so good.

I added a second card with a button. The button uses the following code :

Code: Select all

on mouseUp pButtonNumber
   dispatch "DestinationVolume" to group "SelectedVolumes" of card "First"
   put  the result into tDestinationVol 
   answer   tDestinationVol 
end mouseUp
Unfortunately this does not work. Using the debugger it is possible to confirm that the handler in the group is being called but when called, from a second card, it fails to read the contents of the field.

I have attached a demo stack which has additional buttons that read the field contents directly. Both the direct reads work so I don't need to get the dispatch call working but I am interested in knowing what is wrong with my code.

Thanks for reading.
Simon K.

Re: Unexpected result when calling a handler in a group script

Posted: Fri Oct 05, 2018 3:57 pm
by dunbarx
Hi.

"First" is a reserved word. Try changing it and see if that fixes the problem.

In general, never use something like that for a name. It is almost as fraught with peril as using "1". :wink:

Craig Newman

Re: Unexpected result when calling a handler in a group script

Posted: Fri Oct 05, 2018 4:42 pm
by Simon Knight
Fair point but changing the name to tFirst makes no difference.

Re: Unexpected result when calling a handler in a group script

Posted: Fri Oct 05, 2018 6:25 pm
by jmburnod
Hi Simon,
All works fine in cd "First" but not in cd 2
(Please consider Craig's comment about name. I often can verify how names are precious)

I'm a litle confuse about message path because I can verify that the message is catched by group script. (a break point works) but it return empty.
I wonder if that is possible to dispatch/send a message out of messages path.
Same result with a send command

What said the doc
Use the dispatch command to send a message to an object, via the message path and find out whether it was handled or not.
Best regards
Jean-Marc

Re: Unexpected result when calling a handler in a group script

Posted: Fri Oct 05, 2018 7:32 pm
by Simon Knight
The wording about dispatch and the message path is confusing (to me?). In this case the message does get actioned but for some reason the code fails to read the field. My interpretation of the dictionary about groups is that they sit outside the message path unless they are background groups.

Simon

Re: Unexpected result when calling a handler in a group script

Posted: Fri Oct 05, 2018 7:55 pm
by jmburnod
My interpretation of the dictionary about groups is that they sit outside the message path unless they are background groups.
No. If that was the case your script should not work in you card "First"
I understand that send and dispatch work with a group of the current card
Jean-Marc

Re: Unexpected result when calling a handler in a group script

Posted: Sat Oct 06, 2018 8:33 am
by Simon Knight
I was under the impression that the message path is the path that a message takes automatically, whereas dispatch and send are commands that allow a message to be directed to a specific control bypassing the
default message path and entering/following a new path starting with the specified control.

However, as I and others have said the message is being directed correctly as the handler is run; the problem is that when run this way it fails to get a value from the field.

best wishes
Simon

Re: Unexpected result when calling a handler in a group script

Posted: Sat Oct 06, 2018 2:20 pm
by SparkOut
This will work

Code: Select all

on DestinationVolume
   put the long name of me into tMe
   return field "Destination" of tMe
end DestinationVolume
I think there is something screwy with "me" resolving to the "short name" of the group, ie group "SelectedVolumes". Field "Destination" of group "SelectedVolumes" does not exist on the current card, and without an explicit reference to the card name of the first card, it is not finding a field from which to return the contents. I am not sure why that returns empty rather than throwing a no such object error. I'm not sure if it's expected behaviour or not, but my gut feels like it should not be.
[EDIT]

This also works:

Code: Select all

on DestinationVolume
   return field "Destination" of the target
end DestinationVolume
This is more distressing still, since

Code: Select all

on DestinationVolume
   put the target into tTarget
   put me into tMe
   if tTarget is tMe then
      --both resolve to 'group "SelectedVolumes"
      --neither have the card name qualifier
      answer "tTarget and tMe are identical"
   end if
   put the long name of the target into tLongTarget
   put the long name of me into tLongMe
   if tLongTarget is tLongMe then
      --both resolve to the full name including card and stack names
      answer "tLongTarget and tLongMe are identical"
   end if
   return field "Destination" of the target
   --returns the data from the field, without having to use the long name, although the same resolution goes for "me"
   --what gives?
end DestinationVolume

Re: Unexpected result when calling a handler in a group script

Posted: Sat Oct 06, 2018 6:00 pm
by jmburnod
Hi,
long name of me/the target seems the trick in this case
Best
Jean-Marc

Re: Unexpected result when calling a handler in a group script

Posted: Sat Oct 06, 2018 9:01 pm
by SparkOut
But why does "the target" and "me" resolve to the same, but have different results?

Re: Unexpected result when calling a handler in a group script

Posted: Sun Oct 07, 2018 6:33 am
by Simon Knight
Simple - as you wrote "I think there is something screwy...."

I've updated my example stack (attached) and posted a bug report No. 21630 https://quality.livecode.com/show_bug.cgi?id=21630

The updated stack shows that the "feature" is not limited to groups; it also occurs with cards and I suspect other containers.

Also chaining dispatch calls does not work i.e. dispatching a MouseUp to a button (named UseDispatch) that then issues a dispatch causes an error to be thrown: button "UseDispatch": execution error at line 2 (Chunk: can't find background), char 1.

I was under the impression that "the target" should return the name of the original button : "Returns the object which received the message that started execution." but I suppose its not clear which execution the dictionary is referring to.

Simon

Re: Unexpected result when calling a handler in a group script

Posted: Sun Oct 07, 2018 7:18 am
by SparkOut
Great thanks (but your link has the first two digits the wrong way round and goes to the wrong bug report).

Also I too was slow to try "the target" as that is normally the control that generates the event, not handles it. It was only the wording of the dictionary that made me test what it was going to resolve.

Re: Unexpected result when calling a handler in a group script

Posted: Sun Oct 07, 2018 3:45 pm
by jacque
Dispatch generates an event as though it was sent by the engine to the control and then the message follows the normal message path. In this case a second dispatch command would effectively generate a new event and the target would change to the second control.

Re: Unexpected result when calling a handler in a group script

Posted: Sun Oct 07, 2018 5:24 pm
by SparkOut
Thanks jwack. I don't suppose you have any insight as to why the target and me both resolve to report the same, in essence the short name, but one works and the other doesn't?

Re: Unexpected result when calling a handler in a group script

Posted: Sun Oct 07, 2018 6:50 pm
by jacque
SparkOut wrote:
Sun Oct 07, 2018 5:24 pm
Thanks jwack. I don't suppose you have any insight as to why the target and me both resolve to report the same, in essence the short name, but one works and the other doesn't?
I'm not sure, unless there is something in the engine that resolves the short name into a long name if necessary.

"of me" fails because it returns the short name. The engine looks on the card for a group with that name, and there isn't one, so it returns empty. Using "the long name of me" does work, as mentioned.

That said, there may be something wrong because changing the command to use "send" instead should work. "Send" is supposed to use the context of the card where the receiving handler lives (card "First") and in this case it isn't doing that.