Page 1 of 2
Activating procedures with multiple buttons
Posted: Mon Dec 01, 2014 10:18 am
by LMW-YBC
Hello, LiveCode Forums,
I'm working on a project for my Computing course, and I've tasked myself to make a simple "Who Wants To Be A Millionaire" type of game. Each card will have four answers to choose like the show.
What I'm having trouble finding out is how to trigger procedures when either of these four buttons are pressed. I've given each button the name "Answer_[
insert number of button]". Here's the section I want to activate:
Code: Select all
on Check_Buttons
// If either of the four buttons is pressed, activate the procedures below
Check_Answer
Produce_Score
end Check_Buttons
This is in the card's script. I want it so that if either of the buttons are pressed, the selected answer will be compared with the correct one, then a score will be produced based on the outcome. However, none of the procedures actually start upon pushing a button.
The buttons have their own script. Here is an example button's code:
Code: Select all
on mouseUp
put Answer_2 into selected_answer
end mouseUp
The pressed button puts its value into the variable selected_answer, which is then compared in the card's script under the procedure Check_Answer.
So basically, I need to know two things:
- How to start procedures when either of the four buttons are pressed
- Where I should be putting my code when referring to procedures
Any response is much appreciated. If you respond and need clarification on something, I'll try to update you as soon as I can. Thanks in advance if you do help though
- LMW
Re: Activating procedures with multiple buttons
Posted: Mon Dec 01, 2014 3:59 pm
by dunbarx
Hi.
This is in the form of homework:
In the card script:
Code: Select all
on mouseUp
put the short name of the target into checkAnswer
-- now what could go here?
end mouseUp
You will need to lose all the local button scripts (unless you "pass" the mouseUp message, but that may be for later). This should give you a clue how to invoke the action you need.
Why is this so robust? Hint, what happens if you want to add to or reduce the number of buttons? Anything?
Write back if you get stuck. By the way, what happens if you click on the card, off any control?
Craig Newman
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 3:27 pm
by LMW-YBC
Hi, Craig,
Thank you very much for the reply. Your post helped me a lot, I can happily say that the buttons now function correctly thanks to that script you showed me, and the outcome of the procedures work perfectly!
Cheers a bunch
- LMW
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 3:31 pm
by dunbarx
Great.
What about that clicking on just the card?
Craig
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 4:22 pm
by LMW-YBC
When I first posted, I had nothing set to activate on the mouseUp procedure of the card (at least, nothing that works). Now that I know how to store the name of what I click, I've set mouseUp on the card to this:
Code: Select all
on mouseUp
put the short name of the target into check_answer1
if check_answer1 = "Answer_1" or check_answer1 ="Answer_2" or check_answer1 = "Answer_3" or check_answer1 = "Answer_4" then
put check_answer1 into selected_answer
Check_Answer
Produce_Points
else
put "" into check_answer1
end if
end mouseUp
It's not the most efficient way of dealing with clicking the buttons, but that's what works for me right now. Once check_answer1 has the name of one of the buttons in it from clicking, it will begin the procedures for checking the answer and giving a score, which both work fine with the card's mouseUp.
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 4:45 pm
by Klaus
Hi LMW-YBC,
in case you do not hva any other controls with similar names on the card, you can shorten your script a bit:
Code: Select all
on mouseUp
put the short name of the target into check_answer1
if check_answer1 BEGINS with "Answer_" then
put check_answer1 into selected_answer
Check_Answer
Produce_Points
else
put "" into check_answer1
end if
end mouseUp
Did you define check_answer1 or better selected_answer as a local or global variable?
You could also simply pass your variables to the other handlers as parameters, but that's another story
Best
Klaus
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 4:54 pm
by LMW-YBC
Hi, Klaus,
I'm not at school right now (which is were my program is on one of the computers), but I'll keep that script in mind, thanks. In fact, I'll definitely focus on making my code more efficient in general, once I get everything working basically at least.
I have defined both check_answer1 and selected_answer as global variables because they are used in multiple procedures in the card's script. Parameter passing scares me so I'm staying away from that right now
Thanks for replying
- LMW
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:07 pm
by dunbarx
Hi.
If your variables are all used within a single script, I would use script local variables instead of globals. Do you know about these?
Not that there is anything wrong with globals, though they linger all over the place, and show up in the debugger even though they are not necessarily pertinent. I avoid them if at all possible.
Craig
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:08 pm
by Klaus
Hi LMW-YBC"
LMW-YBC wrote:I have defined both check_answer1 and selected_answer as global variables because they are used in multiple procedures in the card's script.
ah, OK!
LMW-YBC wrote:Parameter passing scares me so I'm staying away from that right now

Don't be scared, this is really simple stuff! Check this easy peasy example
Here you define a number as a parameter for the routine "do_the_beep"
Code: Select all
on mouseup
put 3 into tNumberOfBeeps
do_the_beep tNumberOfBeeps
end mouseup
In the card or stack script you have the routine, which uses the parameter just like a local or global variable would do
but without the need of a local or global variable, you just pass the value you would put into the variable:
Code: Select all
command do_the_beep A_parameter_its_name_does_not_matter
beep A_parameter_its_name_does_not_matter
end do_the_beep
Will beep 3 times in this example!
The name of the parameter on the routine must not neccessarily be the same as the name of the variable that you are actually passing.
The routine just awaits ONE parameter, no matter how it is named.
Best
Klaus
P.S.
OH MY GOD! (Power of) THE NUMBER OF THE BEAST!

Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:09 pm
by magice
LMW-YBC wrote:When I first posted, I had nothing set to activate on the mouseUp procedure of the card (at least, nothing that works). Now that I know how to store the name of what I click, I've set mouseUp on the card to this:
Code: Select all
on mouseUp
put the short name of the target into check_answer1
if check_answer1 = "Answer_1" or check_answer1 ="Answer_2" or check_answer1 = "Answer_3" or check_answer1 = "Answer_4" then
put check_answer1 into selected_answer
Check_Answer
Produce_Points
else
put "" into check_answer1
end if
end mouseUp
It's not the most efficient way of dealing with clicking the buttons, but that's what works for me right now. Once check_answer1 has the name of one of the buttons in it from clicking, it will begin the procedures for checking the answer and giving a score, which both work fine with the card's mouseUp.
I'm a little confused as to why you have the if statement checking if it is one of the four buttons. Is this to make sure some other button wasn't pushed? It seems to me that a easier way to do this would be as follows.
in each button:
Code: Select all
on MouseUp
put the id of me into tID
doMyStuff tID --This calls a custom handler and passes the button id to the handler in the card
end mouseUp
Now in your card script:
Code: Select all
on doMyStuff tButtonPressed --this catches the button id and puts it into a variable, in this case tButtonPressed.
--Your script here -- tButtonPressed is a reference to the button, (E.G. answer the label of btn id tButtonPressed) that's a hint BTW
end doMyStuff
edit: Klaus beat me to it.
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:13 pm
by LMW-YBC
dunbarx wrote:Hi.
If your variables are all used within a single script, I would use script local variables instead of globals. Do you know about these?
Yes, but I'm a bit confused on their scope. Do they work within just procedures (e.g. on mouseUp... end mouseUp), or entire object/card scripts? My teacher doesn't mind if I use global variables, but I'm probably better using local variables when possible for efficiency.
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:20 pm
by LMW-YBC
Hi, Magice,
I only used the if statement because it's a function I'm more used to (the tasks I've done for LiveCode in the past involved if statements). I'll give those scripts a go with my program once I'm at school again though.
Thanks
- LMW
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:24 pm
by LMW-YBC
Klaus wrote:LMW-YBC wrote:Parameter passing scares me so I'm staying away from that right now

Don't be scared, this is really simple stuff! Check this easy peasy example
Here you define a number as a parameter for the routine "do_the_beep"
Code: Select all
on mouseup
put 3 into tNumberOfBeeps
do_the_beep tNumberOfBeeps
end mouseup
In the card or stack script you have the routine, which uses the parameter just like a local or global variable would do
but without the need of a local or global variable, you just pass the value you would put into the variable:
Code: Select all
command do_the_beep A_parameter_its_name_does_not_matter
beep A_parameter_its_name_does_not_matter
end do_the_beep
Will beep 3 times in this example!
The name of the parameter on the routine must not neccessarily be the same as the name of the variable that you are actually passing.
The routine just awaits ONE parameter, no matter how it is named.
This is a very helpful guide for parameter passing! I'm still not sure I'm ready to do it yet, but I'll certainly keep this in mind.
Thank you, Klaus
- LMW
Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 5:58 pm
by Klaus
My pleasure!

Re: Activating procedures with multiple buttons
Posted: Tue Dec 02, 2014 7:01 pm
by dunbarx
Hi.
You might say that script local variables are "global" within a script. All handlers in that script have access. Maybe they are "local globals"? Note that they must be declared ABOVE all handlers that need them. (and must be outside any particular handler) so they are generally found at the top of the script.
You do know that ordinary variables may be declared explicitly, but do not need to be. It is likely good practice, however. That is why I never do so.
Anyway if you need those values AMONG scripts, then a global (or a custom property) is needed. These are accessible in any script in any stack in the current session.
Do learn about custom properties. They are way cool.
Craig