Page 1 of 1

Possible to attach multiple behaviors to button?

Posted: Wed Nov 19, 2014 7:32 pm
by fgr33n
Hi, After reading the manual entry on behaviors, and searching this forum, I think I probably already know the answer, but just to double-check... It is possible to attach multiple behaviors to an object-- like a button?

If it is not possible through the property window, can multiple behaviors be instanced, and their properties populated, using code?

if you can't use multiple behaviors, then how would you handle the following?

Imagine you have four buttons. Each button needs to go to a different card. Each button needs to call a handler. But two of the buttons need to call a second handler.

In other languages I'd create two behaviors. One for the "goTo" (custom property: pDestination), and a second for the "doHandler" (custom property: pHandler), and attach them to the buttons as needed. All four buttons would have both goTo and doHandler behaviors, with two of the buttons having a second instance of doHandler.

If multiple behaviors are not possible in LC, how should I approach this? I'm kind of looking for best practices for a large project. Like if you were to multiply the example by dozens or even hundreds of cards, buttons and handlers?

Thanks in advance for any help.

Re: Possible to attach multiple behaviors to button?

Posted: Wed Nov 19, 2014 8:55 pm
by Dixie
Scott Rossi explained 'chained behaviours' like so,,,:-
'Attached is an embarassingly simple stack that hopefully gives you a little glimpse into chained behavior. The reason to use a chained behavior is when you want to make some actions or functions common to multiple object behaviors. In the attached stack, each set of objects has their own custom behaviors, but the "master behavior" applies to all of them -- in this case setting a lineSize on mouseDown and restoring the default lineSize on mouseUp. You'll notice that while each control responds differently when clicked on, they all exhibit the common lineSize behavior from the master.'

I found it to be a great explanation of 'chained' or multiple behaviours...:-)

be well

dixie

Re: Possible to attach multiple behaviors to button?

Posted: Wed Nov 19, 2014 9:44 pm
by dunbarx
This is one adorable stack.

But I think fgr33n was asking the opposite question, whether a single object can have more than one behavior reference. What Scott's example shows is that the parent can hold many behavior handlers.

Unless I have this wrong, it is as if an object was able to reference, say, two parent buttons, each with its own behavior script. Implemented as if there was a list field in the inspector that one could type in the long ID's of several buttons.

Um, no.

Craig Newman

Re: Possible to attach multiple behaviors to button?

Posted: Wed Nov 19, 2014 10:12 pm
by Klaus
I would set a custom property of the buttons that should also execute a second handler
and check this in an IF THEN check inside of the behaviour:

Code: Select all

...
if the cShallIReallyExecuteAnotherHandler of me then
  do_second_handler
end if
...
:D

Re: Possible to attach multiple behaviors to button?

Posted: Wed Nov 19, 2014 10:46 pm
by fgr33n
Hi Dixie, thanks for the speedy reply and the example. I'm looking forward to checking it out tonight.

Craig, thanks for the reply, I kind of thought that was the case, but after wasting quite a bit of time searching the language reference for a way to convert a string to an integer, I thought I would double check on the forum ( Not having to worry about data types is going to be a big time saver, but it is definitely a paradigm shift!)

Klaus, Unless I misunderstood, it looks like the LC way to do my example would be to call, or not call, multiple handlers from a single behavior, based on the values of the clicked button's custom properties?

Thanks again for the help.

Fred G.

Re: Possible to attach multiple behaviors to button?

Posted: Wed Nov 19, 2014 11:18 pm
by Klaus
Hi Ffred,
fgr33n wrote:Klaus, Unless I misunderstood, it looks like the LC way to do my example would be to call, or not call, multiple handlers from a single behavior, based on the values of the clicked button's custom properties?
this is ONE way how to do it in Livecode :D


Best

Klaus

Re: Possible to attach multiple behaviors to button?

Posted: Thu Nov 20, 2014 1:24 am
by Da_Elf
Do you want it to go to another card and then do something? you could set a global variable from clicking the button then check the variable in the "on openCard" to see if you should do what you need