Should have figured this out by now!!

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
revRider
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 49
Joined: Sat Apr 02, 2011 4:06 pm

Should have figured this out by now!!

Post by revRider » Tue Nov 20, 2012 1:40 pm

I've been working with LiveCode for a few months now and there is one major thing I can't figure out, how do you share/use subroutines across objects and across cards?

I have 3 buttons on a card, and I would like the data on all the fields on the card to be saved at the end of each "object" completion. If I add the code to each button it works, but duplicated code, harder to edit.

If I setup all a global variables that seems to help, but with the same results, I have to code the variables on each object/button.

What am I missing?

And is there a list of the ON reserve commands? On CardOpen, on ScriptOpen, etc!? Haven't found list like this in the manuals or online help...

Thanks.
~David

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Should have figured this out by now!!

Post by Klaus » Tue Nov 20, 2012 3:07 pm

Hi David,
revRider wrote:I've been working with LiveCode for a few months now and there is one major thing I can't figure out, how do you share/use subroutines across objects and across cards?
I have 3 buttons on a card, and I would like the data on all the fields on the card to be saved at the end of each "object" completion. If I add the code to each button it works, but duplicated code, harder to edit.
If I setup all a global variables that seems to help, but with the same results, I have to code the variables on each object/button.
What am I missing?
Do not put the logic (means the atcual handler) into the buttons scripts, but in the card or stack script!
(Cheap) Example: Instead of scripting your button(s) like this

Code: Select all

on mouseup
  beep 3
  answer "I beeped 3 times"
end mouseup
Put this into the card script:

Code: Select all

command do_the_beep_and_answer_it howOften
    beep howOften
    answer "I beeped" && howOften && "times"
end do_the_beep_and_anser_it
Then you can have a lot of buttons that do almost the same but with this script:

Code: Select all

on mouseup
  do_the_beep_and_answer_it 2
end mouseup
Or have another button do the same but with another parameter:

Code: Select all

on mouseup
  do_the_beep_and_answer_it 14
end mouseup
If you ever need to modify the script, just do it and the buttons will follow :D
You get the picture...
revRider wrote:And is there a list of the ON reserve commands? On CardOpen, on ScriptOpen, etc!? Haven't found list like this in the manuals or online help...
Thats "opencard" actually! 8)

Open the dictionary (menu: Help) and click "Language -> Message", all these mesages that will appear are the reserved message names "ON".


Best

Klaus

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

Re: Should have figured this out by now!!

Post by dunbarx » Tue Nov 20, 2012 3:26 pm

Hi.

This is a basic concept that you absolutely MUST learn. The good news is that it is easy, and easy to experiment with.

Make a stack with three buttons. Name them as you wish. In the card (or stack) script, write:

on mouseUp
answer the name of the target
end mouseUp

Press any button. You will get the name of that button. Read up on "the Target". Try to embellish this simple script.

But this is only one small experiment into learning how messages pass up a hierarchy of objects in a natural, predictable way. Read the user manual. Write back when you get stuck again (you will). But by that time you will be well on your way...

Craig Newman

Post Reply