using the same code in multiple places...

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
tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

using the same code in multiple places...

Post by tjo7777 » Sat May 10, 2014 7:32 am

Forgive me if this has been posted somewhere already, I did search but was unable to find an answer.

I have a project with several buttons that have chunks of code which are identical.

Is there a way to create a group of predefined commands once, then call that group of commands to be run when needed? Kind of like a global variable that holds a set of commands, rather than a value.

I am hoping not only to save time and space, but also to edit the code once and then have the change apply to all instances where that code is called to run.

Thanks for any help or suggestions,

TJ.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: using the same code in multiple places...

Post by Simon » Sat May 10, 2014 7:52 am

Hi TJ,
Sure this can be a handler/function/command
So in you button
on mouseUp
put "I'm a button"
goBeep
end mouseUp

on goBeep
beep
answer the short name of the target
end goBeep
Anytime you make a call to goBeep (Oh, I just made that name up, you can use any name that's not reserved) it beeps. Of course goBeep should have a lot more stuff in it.

Make sense?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

Re: using the same code in multiple places...

Post by tjo7777 » Sat May 10, 2014 7:59 am

Looks like it makes sense. I will play with it a bit and see how it goes. Thanks for your help!

TJ.

tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

Re: using the same code in multiple places...

Post by tjo7777 » Sat May 10, 2014 9:21 am

So I tried that and it didn't work for me. Let me explain what I'm trying to do in more detail.

I would like to create/declare these functions in my "START" button. So in my start button it would look like:

Code: Select all

on mouseUp

on goBeep
beep
show image "some image"
put "test" into field "test label"
show field "test label"
wait 5 seconds
hide image "some image"
hide field "test label"
end goBeep

end mouseUp
In several other buttons on the same card I would like to have code like:

Code: Select all

on mouseUp

if somevariable > 3 then
goBeep
end if

end mouseUp
My hope is that when the second, third or fourth button is pressed and somevariable is greater than 3 then "goBeep" is called and will execute all the commands listed between "on goBeep" and "end goBeep" in my start button. Is there any issue with using goBeep inside an if/then statement and is there any problem with making goBeep global, so it can be created/declared in on button, then called from another?

Thanks again for any advice or suggestions,

TJ.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: using the same code in multiple places...

Post by Simon » Sat May 10, 2014 9:47 am

Hi TJ,
ooops...
"on mouseUp" is the same kind of thing as "on goBeep". Don't wrap goBeep with mouseUp.

on mouseUp
goBeep
end mouseUp

on goBeep
.....
In several other buttons on the same card I would like to have code like:
That actually will work just get "on goBeep" out of "on mouseUp"

I showed you "the target", I assume you don't want each button to do the same thing? If the button name is "Fish" then
on goBeep
beep
show image (the short name of the target &".png")
....
end goBeep
would show an image called Fish.png
The Name of the button can hold stuff and the Label can be different.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

Re: using the same code in multiple places...

Post by tjo7777 » Sat May 10, 2014 3:54 pm

Thanks for the help Simon!

I'll give that a try and see if I can get it working.

I'm just wondering if I need to do anything to make "goBeep", or whatever I call it, a global. I am hoping to create/declare "goBeep" once in the start button, then be able to call it from all the other buttons on the card.

Thanks again,

TJ.

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

Re: using the same code in multiple places...

Post by Klaus » Sat May 10, 2014 4:09 pm

Hi TJ,
tjo7777 wrote:I'm just wondering if I need to do anything to make "goBeep", or whatever I call it, a global.
I am hoping to create/declare "goBeep" once in the start button, then be able to call it from all the other buttons on the card.
put the handler "goBeep" (or whatever you name it) into the STACK script!
This way you can access it from any object on any card in your stack!

If you put in into the CARD script, then it will (only) be accessible from any object on THAT card.
Maybe that is what you want.

Best

Klaus

tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

Re: using the same code in multiple places...

Post by tjo7777 » Sat May 10, 2014 6:37 pm

Thanks for adding that Klaus. That was the one thing I was missing! I was putting the handler in the button not the stack! Got it working now.

Thanks again to everyone for the help!

TJ.

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

Re: using the same code in multiple places...

Post by Klaus » Sat May 10, 2014 7:30 pm

Hi TJ,

the "messagepath" is a powerful thing, if used wisely :D

You may want to read this article from Richard Gaskin:
http://www.fourthworld.com/embassy/arti ... _path.html

And the rest of the website is also worth a look or two :D


Best

Klaus

Post Reply