Calling scripts in objects directly without using pending messages
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Calling scripts in objects directly without using pending messages
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!
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!
Re: Calling scripts in objects directly without using pending messages
Hi Stevem
use DISPATCH!
This will wait with -> still more stuff
until the mouseup script has finished.
Best
Klaus
use DISPATCH!
Code: Select all
...
## do your stuff..
## more stuff
dispatch "mouseup" to btn "another button"
## still more stuff
...
until the mouseup script has finished.
Best
Klaus
-
- VIP Livecode Opensource Backer
- Posts: 9665
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Calling scripts in objects directly without using pending messages
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:
and in the card script:
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
"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
Code: Select all
on x
put 2 into fld 1
end x
Craig
Re: Calling scripts in objects directly without using pending messages
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
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
Re: Calling scripts in objects directly without using pending messages
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
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 anymoreChanging 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 ""e&"44 88""e) 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
Re: Calling scripts in objects directly without using pending messages
Thanks very much for that!