Page 1 of 1

Objects sharing scripts

Posted: Tue Apr 19, 2016 9:43 pm
by bd525
No doubt this is a simple question, but I haven't been able to find the answer in a lot of searching of the forum, Google, and the User Guide. So, I hope somebody can spare a minute or two to save me more time scratching my head.

All I need is for a group of buttons or images on the same card to share the same script. Behaviors seem to be what I'm looking for, and indeed, that's the same term used in Director, the platform I'm migrating from.

When I issue the command:
set the behavior of button "Button 2" to the long id of button "Button 1"
then
button id 1009 of stack "Test Behaviors"
shows up in the Behavior field of the Basic Properties pane of Button 2. So far so good.

But, this script:
on mouseUp
answer the name of me
end mouseUp
assigned to Button 1 executes correctly when I click Button 1, but not Button 2. So, obviously I'm not doing it right.

And the solution is not
on mouseUp me
as it would be on Director.

This stuff is easy and straightforward on Director. I'm sure the same is the case on LiveCode; if I could just figure out how!

Thanks!

Re: Objects sharing scripts

Posted: Tue Apr 19, 2016 11:13 pm
by dunbarx
Hi.

Behaviors are powerful indeed, and can surely do what you asked, but I wonder if the simple use of the message path is all you need. Try this. On a new card, place three buttons. Name them whatever you want. In the card script:

Code: Select all

on mouseUp
answer the target
end mouseUp
Click on any of the three. The handler in the card script is "shared" by all controls on the card that do not explicitly have a "mouseUp" handler in their own scripts. And even that can be dealt with. But is this what you actually needed? The technique can be enhanced beyond this simple example.

For behaviors, make two more buttons. Name the first one "behave". Put this in its script:

Code: Select all

on mouseUp
   answer the name of me
end mouseUp
Now set the behavior of the other button to the long ID of btn "behave". Click on either. See?

Craig Newman

EDIT:

The behavior treats "me" locally, which is why it works. The initiating control is the "local" control.

Re: Objects sharing scripts

Posted: Wed Apr 20, 2016 1:22 am
by bd525
I was right; the solution is indeed simple. The card script example accomplishes all I need.

I now have a better grasp of the conceptual structure of LiveCode. Thanks, Craig!

Re: Objects sharing scripts

Posted: Wed Apr 20, 2016 1:46 am
by dunbarx

Re: Objects sharing scripts

Posted: Wed Apr 20, 2016 1:51 am
by FourthWorld
The problem with using a card script is that now all mouseUp actions, including those on the card itself, will trigger the script. You can mitigate this by adding conditionals for types of objects being clicked on, and for buttons you want to exclude you could add more conditionals to check custom properties, etc. But all the seems like a lot of work.

If you need behaviors (and it sounds like you do), let's figure out what went wrong with those. Many of us rely on them daily so I know they work. Let's figure out why they didn't work for you.

Re: Objects sharing scripts

Posted: Wed Apr 20, 2016 2:01 am
by bd525
My app is not complex, Richard, so it's simple to write "exit mouseUp" conditions in the card scripts for mouseUps that don't apply.

But, it seems as if you're saying that the way I set things up in the first place should have worked. Correct?

Re: Objects sharing scripts

Posted: Wed Apr 20, 2016 2:21 am
by FourthWorld
Sounds like it, but apparently it didn't. :) So the question at hand is: why?

Maybe just try to set the behavior again? You can double-check it after you set it by viewing the Behavior field in the Inspector.

Re: Objects sharing scripts

Posted: Wed Apr 20, 2016 2:15 pm
by bd525
Haven't figured out what's going on yet, but will try again later today (it's morning in the western U.S.) when I have some more time.