Page 1 of 1
using the same code in multiple places...
Posted: Sat May 10, 2014 7:32 am
by tjo7777
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.
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 7:52 am
by Simon
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
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 7:59 am
by tjo7777
Looks like it makes sense. I will play with it a bit and see how it goes. Thanks for your help!
TJ.
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 9:21 am
by tjo7777
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.
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 9:47 am
by Simon
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
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 3:54 pm
by tjo7777
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.
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 4:09 pm
by Klaus
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
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 6:37 pm
by tjo7777
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.
Re: using the same code in multiple places...
Posted: Sat May 10, 2014 7:30 pm
by Klaus
Hi TJ,
the "messagepath" is a powerful thing, if used wisely
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
Best
Klaus