Unexpected result when calling a handler in a group script

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Simon Knight
Posts: 407
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Unexpected result when calling a handler in a group script

Post by Simon Knight » Fri Oct 05, 2018 3:05 pm

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.
Attachments
ProblemWithGroup.livecode.zip
A simple stack
(4.55 KiB) Downloaded 42 times
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5582
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Fri Oct 05, 2018 3:57 pm

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

Simon Knight
Posts: 407
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

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

Post by Simon Knight » Fri Oct 05, 2018 4:42 pm

Fair point but changing the name to tFirst makes no difference.
best wishes
Skids

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2254
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

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

Post by jmburnod » Fri Oct 05, 2018 6:25 pm

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

Simon Knight
Posts: 407
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

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

Post by Simon Knight » Fri Oct 05, 2018 7:32 pm

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
best wishes
Skids

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2254
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

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

Post by jmburnod » Fri Oct 05, 2018 7:55 pm

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

Simon Knight
Posts: 407
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

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

Post by Simon Knight » Sat Oct 06, 2018 8:33 am

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
best wishes
Skids

SparkOut
Posts: 1812
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Sat Oct 06, 2018 2:20 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2254
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

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

Post by jmburnod » Sat Oct 06, 2018 6:00 pm

Hi,
long name of me/the target seems the trick in this case
Best
Jean-Marc

SparkOut
Posts: 1812
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Sat Oct 06, 2018 9:01 pm

But why does "the target" and "me" resolve to the same, but have different results?

Simon Knight
Posts: 407
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

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

Post by Simon Knight » Sun Oct 07, 2018 6:33 am

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
Attachments
ProblemWithGroup.livecode.zip
(6.53 KiB) Downloaded 36 times
Last edited by Simon Knight on Mon Oct 08, 2018 9:59 am, edited 1 time in total.
best wishes
Skids

SparkOut
Posts: 1812
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Sun Oct 07, 2018 7:18 am

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4673
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

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

Post by jacque » Sun Oct 07, 2018 3:45 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

SparkOut
Posts: 1812
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » 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?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4673
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

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

Post by jacque » Sun Oct 07, 2018 6:50 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Talking LiveCode”