Calling scripts in objects directly without using pending messages

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SteveFI
Posts: 30
Joined: Tue Mar 16, 2021 6:15 pm

Calling scripts in objects directly without using pending messages

Post by SteveFI » Tue Apr 13, 2021 4:50 pm

Hi all,

I'm sure I used to know hot to do this but how does one call a script in an object whilst a handler is executing rather than after it's executed?

To elaborate, say I've a mouseUp handler and during executivon, I wish to halt it, call a routine in another object and then carry on with the rest of the mouseUp. Or, in other words, effectively insert the call into the running script so said object can do something.

As things stand, using a send command adds the call to the pending messages, which calls after the mouseUp has finished. Call and dispatch both seem to execute after mouseUp has finished.

I've reworked my approach to overcome the situation but, like so many things returning after 10 years of not doing any coding of any nature at all, I'm sure I once knew how to do this but left scratching my head.

Thanks,


Steve

P.S. Sorry if this has come up before and is in the forum history somewhere. I've spent quite a few hours now looking for things and getting reacquainted with LC's foibles and workarounds!

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

Re: Calling scripts in objects directly without using pending messages

Post by Klaus » Tue Apr 13, 2021 6:40 pm

Hi Stevem

use DISPATCH!

Code: Select all

...
## do your stuff..
## more stuff
dispatch "mouseup" to btn "another button"
## still more stuff
...
This will wait with -> still more stuff
until the mouseup script has finished.

Best

Klaus

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

Re: Calling scripts in objects directly without using pending messages

Post by dunbarx » Tue Apr 13, 2021 9:46 pm

Hi.

"Send" and "Dispatch" do slightly different things, but this seems to work with good ol' "send". On a card with a button and a field 1, in the button script:

Code: Select all

on mouseUp
   put 1 into fld 1
   wait 20
   send "X" to this cd
   wait 20
   put 3 into fld 1
end mouseUp
and in the card script:

Code: Select all

on x
   put 2 into fld 1
end x
The called handler "x" does its thing and returns control back to the "mouseUp" handler in due course and in due order. I would have written it this way without thinking about any odd issues. What am I missing?

Craig

SteveFI
Posts: 30
Joined: Tue Mar 16, 2021 6:15 pm

Re: Calling scripts in objects directly without using pending messages

Post by SteveFI » Fri Apr 16, 2021 12:23 pm

Thanks both.

I'm not sure why dispatch didn't appear to work for me. My code had become a bit convoluted, so maybe other things were going on. After reworking things, I got the outcome I was looking for.

Not sure if this means anything without seeing the stack but a mouseUp handler in a button caused an exitField event to occur in a field and what they were both trying to do would conflict.

Consider me reminded about the message path, pending messages, send and dispatch!

I'm really enjoying being back.

Thanks again,


Steve

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

Re: Calling scripts in objects directly without using pending messages

Post by bogs » Fri Apr 16, 2021 12:44 pm

I'm not sure if you ever saw this before, but Richard had a nice little write up about send, call, and dispatch here: https://www.fourthworld.com/embassy/art ... _path.html
Changing the Firing Order of Messages

While messages ordinarily travel only through the message path as shown in the figures above, you can invoke handlers in scripts of objects outside of the current message path with the send and call commands:

send "mouseUp" to button "Cancel"

call "CalculateTotals" of button "Total"

dispatch "resizeIt" to grp 1 with "40,120,240,580"

The difference between the send and call commands is that the send command changes the context so that object references are treated as relative to the object you send the message to, while the call command does not change the context and continues to treat object references relative to the original object.

The dispatch command is similar to send, but with two advantages: it allows a more graceful way to send arguments using the optional with portion, and benchmarks about 30% faster. I don't know why dispatch is so much faster than send, but in my tests the performance advantage is consistent enough that I tend to use it in place of send for everything written for v3.5 at later.

With send and call you can send only one string to the target object, so if you want to include any parameters with your call you need to include them there. For example, here we send the value "44" as an argument to the call to a handler named GetObjInfo:

send "GetObjInfo 44" to button "Tester"

Sending params with send and call can get a bit tricky, since the string is evaluated when it's sent, so you'll have to be careful to put multi-word values in concatenated quotes, e.g.:

send ("GetObjInfo "&quote&"44 88"&quote) to button "Tester"

Dispatch makes this much simpler with its optional with phrase, where you can pass arguments to a command just as you would if you were calling it directly in the message path, even passing in arrays if you like:

put "100" into tMyArray[1]
dispatch "AddThis" to button "Tester" with tMyArray
The BYU site also delved into the diferences between the 3 that might be useful to you, however, I don't have the direct link to the page anymore :(
Image

SteveFI
Posts: 30
Joined: Tue Mar 16, 2021 6:15 pm

Re: Calling scripts in objects directly without using pending messages

Post by SteveFI » Sun Apr 18, 2021 4:08 pm

Thanks very much for that!

Post Reply

Return to “Talking LiveCode”