Controlling other objects from a different card

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Controlling other objects from a different card

Post by PoLyGLoT » Sun Aug 26, 2012 12:40 am

Hi,

I'm curious about this. Is it possible to store the code for other objects or buttons on places other than the buttons themselves?

For example, let's say I have several different begin buttons, but all of them have similar code. Must I put the code for each begin button within each begin button? Or could I have one "button" which houses all the code for all the "begin" buttons?

I'm trying to organize my files so that I can quickly check over all the relevant code without having to navigate to a million different cards / buttons to individually check the code (and instead just store all the code in one place to look it over).

Any tips to quickly check over or organize my code in this manner would be greatly appreciated.

Thanks (and P.S. this is one of the friendliest and most helpful forums I've ever come across).

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Controlling other objects from a different card

Post by dunbarx » Sun Aug 26, 2012 2:13 am

Hi.

Several ways to do this.

The message hierarchy is the basic way. If you place a handler in the stack script, all messages, such as "mouseup", that are not handled explicitly in the buttons themselves, can be dealt with at that level, in that single handler.

It is important that the stack handler know which button is actually pressed, as there may be variations that pertain to each, and this is managed with such functions as "the target". So make three buttons, name them "b2", "b3" and "b4". Place this in the stack script:

Code: Select all

on mouseup
   if "button" is in the target then
      answer the short name of the target
      beep char 2 of the short name of the target
   end if
end mouseup
Why the first line conditional? You need to play around with this sort of thing.

Also, read up on behaviors, as this is another powerful method, essentially creating a "library" script stored in a button to which many other objects can share.

Is this new to you? It is fundamental to working with LiveCode, especially the second part. Write back if you need more.

Craig Newman

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: Controlling other objects from a different card

Post by PoLyGLoT » Sun Aug 26, 2012 3:02 am

How does your script tell the program which button you are referring to (i.e., how does it know whether "b2" or "b3" was pressed?)

What if I want to have different code for each button (e.g., if it's b2, go to card "XX", if it's b3, go to card "XY", etc.)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: Controlling other objects from a different card

Post by dunbarx » Sun Aug 26, 2012 4:15 am

Hmmm.

Did you run the script with those few buttons?

The answer command used "the target", a function that returns the object of interest, that is, the one that first received a message, such as "mouseUp. So if you had several buttons, you could tell which one the mouse was clicked on.

The (short) name of one possible button was, in the example, "b3". The script deconstructs this simple string, taking the second character (3) and beeping that number of times. The previous line actually put up a dialog with the button name itself.

So you could do something like this:

Code: Select all

on mouseUp
   go cd char 2 of the short name of the target
end mouseUp
:


Or, possibly:

Code: Select all

on mouseup
   if "button" is in the target then
      switch char 2 of the short name of the target
         case 2
            go cd 2
            break
         case 3
            go cd "3"
            break
         case 4
            go first card
            break 
      end switch
   end if
Do you have enough experience to execute and read these, step by step, and play with them? If not, we will do this together, but I need to know that you are actually experimenting. If you are really new, then we will take another tack.

Craig Newman

Post Reply