Activating procedures with multiple buttons

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

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Activating procedures with multiple buttons

Post by LMW-YBC » Mon Dec 01, 2014 10:18 am

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

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

Re: Activating procedures with multiple buttons

Post by dunbarx » Mon Dec 01, 2014 3:59 pm

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

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Re: Activating procedures with multiple buttons

Post by LMW-YBC » Tue Dec 02, 2014 3:27 pm

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 :D

- LMW

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

Re: Activating procedures with multiple buttons

Post by dunbarx » Tue Dec 02, 2014 3:31 pm

Great.

What about that clicking on just the card?

Craig

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Re: Activating procedures with multiple buttons

Post by LMW-YBC » Tue Dec 02, 2014 4:22 pm

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.

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

Re: Activating procedures with multiple buttons

Post by Klaus » Tue Dec 02, 2014 4:45 pm

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 :D


Best

Klaus

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Re: Activating procedures with multiple buttons

Post by LMW-YBC » Tue Dec 02, 2014 4:54 pm

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 :lol:

Thanks for replying

- LMW

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

Re: Activating procedures with multiple buttons

Post by dunbarx » Tue Dec 02, 2014 5:07 pm

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

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

Re: Activating procedures with multiple buttons

Post by Klaus » Tue Dec 02, 2014 5:08 pm

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 :lol:
Don't be scared, this is really simple stuff! Check this easy peasy example :D

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! :twisted:

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Activating procedures with multiple buttons

Post by magice » Tue Dec 02, 2014 5:09 pm

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.
Last edited by magice on Tue Dec 02, 2014 5:21 pm, edited 1 time in total.

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Re: Activating procedures with multiple buttons

Post by LMW-YBC » Tue Dec 02, 2014 5:13 pm

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.

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Re: Activating procedures with multiple buttons

Post by LMW-YBC » Tue Dec 02, 2014 5:20 pm

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

LMW-YBC
Posts: 7
Joined: Thu Nov 27, 2014 4:18 pm

Re: Activating procedures with multiple buttons

Post by LMW-YBC » Tue Dec 02, 2014 5:24 pm

Klaus wrote:
LMW-YBC wrote:Parameter passing scares me so I'm staying away from that right now :lol:
Don't be scared, this is really simple stuff! Check this easy peasy example :D

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

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

Re: Activating procedures with multiple buttons

Post by Klaus » Tue Dec 02, 2014 5:58 pm

My pleasure! :D

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

Re: Activating procedures with multiple buttons

Post by dunbarx » Tue Dec 02, 2014 7:01 pm

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

Post Reply