Objects sharing scripts
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Objects sharing scripts
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!
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!
-
- VIP Livecode Opensource Backer
- Posts: 9678
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Objects sharing scripts
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:
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:
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.
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
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
Craig Newman
EDIT:
The behavior treats "me" locally, which is why it works. The initiating control is the "local" control.
Re: Objects sharing scripts
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!
I now have a better grasp of the conceptual structure of LiveCode. Thanks, Craig!
-
- VIP Livecode Opensource Backer
- Posts: 9844
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Objects sharing scripts
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Objects sharing scripts
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?
But, it seems as if you're saying that the way I set things up in the first place should have worked. Correct?
-
- VIP Livecode Opensource Backer
- Posts: 9844
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Objects sharing scripts
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Objects sharing scripts
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.