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: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

SparkOut
Posts: 2852
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:28 pm

jacque wrote:
Sun Oct 07, 2018 6:50 pm
"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.
Yes, but "the target" also returns the short name, but still works.
And if there isn't an object, shouldn't it generate an error instead of returning empty? If the current card is used instead of card "First" without such an object, there is an error produced.
jacque wrote:
Sun Oct 07, 2018 6:50 pm
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.
I thought that too, I am relieved that I had not ungrokked the way things are supposed to behave, but perturbed by the problematic situation here.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
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 8:29 pm

Yes, but "the target" also returns the short name, but still works.
That's why I wondered if some internal parsing was happening. Maybe it only returns the short name but internally uses the long name. Someone who can read the source code might know.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

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

Post by bn » Sun Oct 07, 2018 11:58 pm

I think this is a simple chunking error.

"me" works for these properties

Code: Select all

Command DestinationVolume
   return the rect of field "Destination" of me
   return the owner of me
   return the rect of control 1 of me
   return the name of control 1 of me
   return the number of controls of me
end DestinationVolume
comment out from top down to see the answers. "Me" is resolved for these properties when called from a different card. But not for the text of a field.

But then I may be wrong and someone please explain to me what the difference of "me" is in the case of asking for the "text of field x of me" vs above examples

Kind regards
Bernd

aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

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

Post by aetaylorBUSBnWt » Thu Jan 06, 2022 12:46 am

The following question still exists, but the error has gone away.

It occurred in 9.6.5, but release 9.6.6 rc 1 which fixed 34 bugs, fixed this problem.
still don't know what "can't find background" means though.


Hi,

I have a far simpler question:

with

dispatch function "aMethod" to anObject;

I get an execution error: (Chunk error: can't find background)

What does "can't find background" mean to begin with?

The above code worked in a prior version of Livecode, but when I came back to it after several months of working on other code, it no longer works in the current version of Livecode.

I know the object exists - I can see the contents of the variable
I know that aMethod exists.
put aMethod() after message does not generate an error.

dispatch "aMethod",anObject works too.
(which is not going to do me any good because I want that function to execute in the context of "anObject".

So what am I missing?

Thanks,
Andrew

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
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 » Thu Jan 06, 2022 3:19 pm

Hi.

I have used the "send" and"dispatch" commands without issue. The syntax you mentioned:
dispatch function "aMethod" to anObject;
should be valid, even though, to me, the inclusion of the word "function" seems odd in the context of what the dictionary says.

Does the variable "anObject" resolve to an actual target? It must.

A "background" is a word that dates back to Hypercard, where there was a separate object class called, er, a "background". This no longer exists in LC. But a group that has its backgroundbehavior set is considered by LC to be a background. So do you have any of these mixed in somewhere?

Craig

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

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

Post by Klaus » Thu Jan 06, 2022 4:11 pm

The syntax for dispatch: dispatch <message> to <target>
And "message" is defined in the dictioanry as -> Is an expression that evaluates to the name of a handler.

So no mention of using "dispatch" with a function!

If you want to address a group that is not on the current card, you can use background instead of group!

Code: Select all

...
dispatch "whatever" to grp "a group"
## Works if group is on current card
...
dispatch "whatever" to background "a group"
## Will work if the group is not on the current card
## In this case make sure all of your groups have different names!
...

Code: Select all

...
put the num of groups
## will return the number of groups on the current card
...
put the num of backgrounds
## will return the number of all groups in the current stack
...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
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 » Thu Jan 06, 2022 5:46 pm

@Klaus
So no mention of using "dispatch" with a function!
That is what I meant by:
even though, to me, the inclusion of the word "function" seems odd in the context of what the dictionary says.
I think we agree.

@ aetaylorBUSBnWt (and Klaus)

On a new card, with two buttons, "B1" and B2", in the script of "B1":

Code: Select all

on mouseUp
   dispatch  function "xx" to btn "b2"
   answer the result
end mouseUp
And in the script of btn "B2":

Code: Select all

function xx
   return random(99)
end xx
Works fine, you get random numbers every time. But if you do NOT have the word "function" in "B1":

Code: Select all

on mouseUp
   dispatch "xx" to btn "b2"
   answer the result
end mouseUp
then the result is always empty. No error thrown, just no go.

I place a breakpoint in "B2". It catches with the word "function" in B1". But without that word, B2 never fires at all.

Craig

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

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

Post by Klaus » Thu Jan 06, 2022 5:55 pm

AHA! Good to know, thank you!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
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 » Thu Jan 06, 2022 5:56 pm

Here is the stack I made to test. The two possible lines are in the B1 script.

Craig
DispatchFunction.livecode.zip
(899 Bytes) Downloaded 92 times

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

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

Post by RCozens » Thu Jan 06, 2022 6:15 pm

Simon Knight wrote:
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
Hi Simon,

When I read you code, I was surprised it worked in the first instance; but from the response of others I guess it does. I would expect the code to read:

Code: Select all

On DestinationVolume
   return the text of field "Destination" of me
end DestinationVolume
or

Code: Select all

On DestinationVolume
   get the text of field "Destination" of me
   return it
end DestinationVolume
I have encountered issues in the past when trying to reference a property of an object in the manner you coded.

Cheers!
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

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

Post by andresdt » Thu Jan 06, 2022 6:18 pm

Hi Simon
I have looked at the stack you shared and put a breakpoint in the SelectedVolumes group script. As already mentioned here comes the message and I did not see any errors or strange behavior. Or if jjj

I don't know how LC handles the text property of fields internally. But apparently it keeps a reference between the card and the field. So if you turn on the sharedText property of your field, the problem is solved.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
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 » Thu Jan 06, 2022 7:33 pm

"dispatch function" is the only way to use a function, as you found out. I wasn't aware of it either until it showed up on the mailing list some time ago. It needs to be documented.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Talking LiveCode”