Using dispatch command to another card gives error

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

raugert
Posts: 112
Joined: Thu May 26, 2016 9:30 pm
Location: Winnipeg, Canada

Using dispatch command to another card gives error

Post by raugert » Mon May 03, 2021 6:58 pm

I am using the dispatch command eg: (dispatch "TurnButtonRed" to card 1), but it gives a (Chunk: no such object) error. However, it works if I specify the path of the object to it's own card within the card's script.

---This code on card 1 doesn't work when using the dispatch command from card 2

Code: Select all

on TurnButtonRed
   set the backgroundColor of Button "RED" to Red
end TurnButtonRed
---This code on card 1 works when using the dispatch command from card 2

Code: Select all

on TurnButtonRed 
   set the backgroundColor of Button "RED" of me to Red
end TurnButtonRed
It appears that the message path remains with the current card and not to the card to which I am sending the dispatch command. I would have thought that dispatching a command to another card would then follow that card's message path.

I have a lot of code in a "loadTemplate" command that doesn't refer all the objects to it's own card. (Normally I wouldn't have to do that.) I can fix it, but it seems counter intuitive and a lot of extra work !. Maybe I'm missing something..

Richard

Here's a example:
Attachments
Dispatch command to another card.livecode.zip
(1.08 KiB) Downloaded 116 times
Livecode Indy 9.6.11 (Stable)
MacOS Sonoma 14.2
Xcode 15.0.1 SDK 17.0

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using dispatch command to another card gives error

Post by bogs » Mon May 03, 2021 7:41 pm

Works here (linux). A few things I noticed about the code you pasted, though...
...the color 'red' should be in quotes, the code I copied from your post (with a mouseup directly on the card above it)...

Code: Select all

# in the card script...
on mouseUp
   turnButtonRed
end mouseUp

on TurnButtonRed
   set the backgroundColor of Button "RED" to "Red" --<-- colors should be in quotations...
   wait 15 with messages
   set the backgroundColor of Button "RED" to ""
end TurnButtonRed
...in card 2's script...

Code: Select all

on mouseUp
   dispatch "TurnButtonRed" to card 1
end mouseUp
also, if you have this arranged as a set number of buttons, instead of dispatch to a card, you might group the buttons as a shared / background group, then put scripts affecting them in the group script, which saves you dispatching anything, or having to move around cards to track down scripts / functions / commands by having them centrally located in the group script.

Thats about all I got.
Image

raugert
Posts: 112
Joined: Thu May 26, 2016 9:30 pm
Location: Winnipeg, Canada

Re: Using dispatch command to another card gives error

Post by raugert » Mon May 03, 2021 8:12 pm

I'm on Mac OS10.15.7. It still gives me an error even with your suggestion of using quotes for "Red". I can trigger the command from card 1, but still get an error if dispatched from card 2.

As far as grouping objects, that would be complicated, as the objects are not a set number. They do change.

I can make this work if I "go to card 1" before sending the dispatch command from card 2. It's not elegant but it works.

Code: Select all

on mouseUp
   go to card 1
   dispatch "TurnButtonRed" to card 1
end mouseUp
I'll keep plugging away.
Thanks Bogs,
Richard
Livecode Indy 9.6.11 (Stable)
MacOS Sonoma 14.2
Xcode 15.0.1 SDK 17.0

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using dispatch command to another card gives error

Post by bogs » Mon May 03, 2021 8:46 pm

raugert wrote:
Mon May 03, 2021 8:12 pm
I'm on Mac OS10.15.7.
...and your probably using a far newer vers. of Lc than I am (it would be hard to imagine anyone who ISN'T haha), so we will have to wait to see if someone else using your OS ( I can't, farthest up the OSX pike I get too is 10.6.7) and your vers. of Lc (which you may want to list) can reproduce the issue, it certainly works without issues on linux at least as far back as 6.7.9 which is what I was using when I read your post, which may indicate a bug :wink:
raugert wrote:
Mon May 03, 2021 8:12 pm
s far as grouping objects, that would be complicated, as the objects are not a set number. They do change.
T'were only a suggestion, as I mentioned, if you had a set number of buttons that didn't change, for instance, a navigation set up or some such :)
raugert wrote:
Mon May 03, 2021 8:12 pm
It still gives me an error even with your suggestion of using quotes for "Red".
If it is any consolation, quoting the colors isn't my suggestion, nore did I expect it to fix the issue your having, it is more along the lines of guidelines. Many languages would require the quotes (or parenthesis), in Lc it merely eliminates (possible) confusion ./ work for the engine to quote literals, colors, object names, etc.

*Edit 1 - I just tested it in 8.1.8 on OSX 10.6.7, and it worked there, so I would say probably bug :cry:
Image

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

Re: Using dispatch command to another card gives error

Post by dunbarx » Tue May 04, 2021 2:54 am

Not sure what you mean by dispatching a command from a card when the handler is on another card:

Code: Select all

This code on card 1 doesn't work when using the dispatch command from card 2
In any case, LC will not know what you are talking about if you:

Code: Select all

set the backgroundColor of Button "RED" to Red
if you are on a card that has no btn "red". It will not know, and will not try to guess, that the button is on some other card.

Craig

raugert
Posts: 112
Joined: Thu May 26, 2016 9:30 pm
Location: Winnipeg, Canada

Re: Using dispatch command to another card gives error

Post by raugert » Tue May 04, 2021 4:46 am

Hi Craig,

Yes, I thought it might be confusing. My app is much more complex, but I made up a simple example with a Button to illustrate. I'll try to better explain.

I made a Button called "Red" on card 1. In the script of card 1, I made a simple handler (see 1st code below) to turn the button red. If I call the handler from card 1 it works fine. But, in my case I want to call the handler from another card using code (dispatch "TurnButtonRed" to card 1). Unfortunately it doesn't work, unless I explicitly identify the path to the Button within the script of card 1 (see 2nd code below). This seems to indicate that when "dispatching" a command from another card, the message path doesn't change to the card that contains the command.

Code: Select all

on TurnButtonRed
   set the backgroundColor of Button "RED" to "Red"
end TurnButtonRed

Code: Select all

on TurnButtonRed
   set the backgroundColor of Button "RED" of card 1 to "Red"
end TurnButtonRed
Not sure if I explained myself any better. :?

Thanks to all you expert folks for taking the time to reply to posts on the forum. Much appreciated.

Richard
Livecode Indy 9.6.11 (Stable)
MacOS Sonoma 14.2
Xcode 15.0.1 SDK 17.0

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

Re: Using dispatch command to another card gives error

Post by SparkOut » Tue May 04, 2021 7:29 am

Use "send" instead of dispatch, and this should solve the issue, if I remember.
As I recall, the context of source/target resolution is different (on purpose) between the two.
But I may be misremembering.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using dispatch command to another card gives error

Post by bogs » Tue May 04, 2021 9:37 am

You know, on reading the replies that followed mine and your answers, I wondered a bit more about the setup and the original question. One question that came to my mind was "Are the 2 cards in the single main stack, or are you talking about 2 cards in two separate stacks?"

In the test I said worked, I set up a single stack with 2 cards and 2 buttons (one on each card).

Craig's post made me think of that, he is right, if you don't have a button on the second card named "red" Lc would drop the error saying that no such object exists on the card your dispatching from.

Other than that, you shouldn't have to "go to card 1" and then dispatch anything, In that chain of events, you are already at card 1, you would just call the command "turnButtonRed".

I think I'd want to see at least a screen shot of what you are trying to accomplish, and probably better yet, a video of the whole problem your having.

In the meantime, this stack is what I thought you were asking -
RedButtons.zip
(870 Bytes) Downloaded 139 times
Anyone of sufficient age should get the joke in the name heh.
Image

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

Re: Using dispatch command to another card gives error

Post by Klaus » Tue May 04, 2021 9:58 am

bogs wrote:
Tue May 04, 2021 9:37 am
...
Anyone of sufficient age should get the joke in the name heh.
I am old: They shoot horses, don't they?
:D

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using dispatch command to another card gives error

Post by bogs » Tue May 04, 2021 10:01 am

At our age, it probably isn't worth the cost of the bullet hahah.
Image

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

Re: Using dispatch command to another card gives error

Post by Klaus » Tue May 04, 2021 10:22 am

That was the name of the film I saw Mr. Buttons in, you %&#¨#?ø! :D

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Using dispatch command to another card gives error

Post by bogs » Tue May 04, 2021 10:53 am

:twisted: :twisted: :twisted:
Image

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Using dispatch command to another card gives error

Post by AxWald » Tue May 04, 2021 11:54 am

Hi,
raugert wrote:
Mon May 03, 2021 6:58 pm
I am using the dispatch command eg: (dispatch "TurnButtonRed" to card 1), but it gives a (Chunk: no such object) error. However, it works if I specify the path of the object to it's own card within the card's script.

---This code on card 1 doesn't work when using the dispatch command from card 2:
... set the backgroundColor of Button "RED" to Red

---This code on card 1 works when using the dispatch command from card 2:
... set the backgroundColor of Button "RED" of me to Red
(quote modified for clarification)

Well, you need to specify which button "RED" to use - there may be more of 'em. Obviously LC wants you to tell it exactly which btn to re-color - in case 1 it just doesn't find it.

LC scripts often work without precise object references - the parser seems to be quite good at guessing. This is one of the boons of the language. But it will bite your behind as soon as a cross a certain limit.

So I've made it a habit to (nearly) always specify "of this cd/ grp" or "of me", especially when dispatching all around my stacks later; this saves a lot of trouble ;-)

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

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

Re: Using dispatch command to another card gives error

Post by dunbarx » Tue May 04, 2021 2:34 pm

I had the same vague memory that Sparkout did, and thought he might have nailed it.

But neither "send" nor "dispatch" can find the target, if in a button on card 2;

Code: Select all

on mouseUp
   dispatch "turnButtonRed" to cd 1
   send "turnButtonRed" to cd 1
end mouseUp
The only way out is to explicitly tell LC where the button is in the target handler, as was noted. Both commands "live" entirely on the card they, er, live on.

Craig

cpuandnet
Posts: 32
Joined: Thu Jan 17, 2019 6:43 am

Re: Using dispatch command to another card gives error

Post by cpuandnet » Tue May 04, 2021 3:39 pm

Maybe LC is getting confused between the button named "RED" with the color "Red". Have you tried naming the button something else?

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”