using the same code in multiple places...
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
using the same code in multiple places...
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.
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...
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
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!
Re: using the same code in multiple places...
Looks like it makes sense. I will play with it a bit and see how it goes. Thanks for your help!
TJ.
TJ.
Re: using the same code in multiple places...
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:
In several other buttons on the same card I would like to have code like:
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.
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
Code: Select all
on mouseUp
if somevariable > 3 then
goBeep
end if
end mouseUp
Thanks again for any advice or suggestions,
TJ.
Re: using the same code in multiple places...
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
.....
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
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
.....
That actually will work just get "on goBeep" out of "on mouseUp"In several other buttons on the same card I would like to have code like:
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!
Re: using the same code in multiple places...
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.
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...
Hi TJ,
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
put the handler "goBeep" (or whatever you name it) into the STACK script!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.
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...
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.
Thanks again to everyone for the help!
TJ.
Re: using the same code in multiple places...
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
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