Objects sharing scripts

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

Post Reply
bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Objects sharing scripts

Post by bd525 » Tue Apr 19, 2016 9:43 pm

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!

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

Re: Objects sharing scripts

Post by dunbarx » Tue Apr 19, 2016 11:13 pm

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.

bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Re: Objects sharing scripts

Post by bd525 » Wed Apr 20, 2016 1:22 am

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!

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

Re: Objects sharing scripts

Post by dunbarx » Wed Apr 20, 2016 1:46 am


FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6851
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Objects sharing scripts

Post by FourthWorld » Wed Apr 20, 2016 1:51 am

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.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Re: Objects sharing scripts

Post by bd525 » Wed Apr 20, 2016 2:01 am

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?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6851
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Objects sharing scripts

Post by FourthWorld » Wed Apr 20, 2016 2:21 am

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.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Re: Objects sharing scripts

Post by bd525 » Wed Apr 20, 2016 2:15 pm

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.

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”