Passing a variable between events

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
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Passing a variable between events

Post by redfield » Mon Apr 08, 2019 7:24 pm

Hi all,
for practicing I am trying to code a simple guess-the-number-game. I tried it with one button and the mouseUp event but didn't get anywhere. I tried now by adding another button and another mouse event and am trying to pass a variable from one to another but I think it's not working. This is the code:

button 1 (start):

Code: Select all

on mouseUp
   put random(100) into field f3
   put 0 into x
end mouseUp
button 2 (guess):

Code: Select all

on mouseUp
   put "Too high!" into toohigh
   put "Too low!" into toolow
   put "Perfect guess!" into bingo
   if field f3 is not empty then
      repeat while x is 0
         if field f1 > field f3 then
            put toohigh into field f2
         else if field f1 < field f3 then
            put toolow into field f2
         else
            put bingo into field f2
            put 1 into x
         end if
      end repeat
   end if
end mouseUp
Any hint? :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Passing a variable between events

Post by dunbarx » Mon Apr 08, 2019 7:39 pm

Hi.

Not sure what your user interface is. It could be anything, but I think you are overdoing it here. Let's start from the absolute beginning. In a button script:

Code: Select all

on mouseUp
   ask "Guess a number!" 
   if it = random(100) then answer "You got it! The number was" && it
   else answer "Nope. Try again?"
end mouseUp
What does this lack, besides beauty, cleverness and style?

As for the actual topic of your post, do you see how this works, even though it is not necessary, and as it stands, does nothing more than the one above?

Code: Select all

on mouseUp
   ask "Guess a number!" 
  if it is an integer then  findIfTrue it
  end mouseUp

on findIfTrue tGuess
   if tGuess = random(100) then answer "You got it! The number was" && tGuess
   else answer "Nope. Try again?"
end findIfTrue
I get in trouble with this, but as homework can you add your "Too high/too low" gadgetry to the above, so that the user can ever narrow the guessing? You will not be able to use a random number at the start, of course.

Craig Newman

redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Re: Passing a variable between events

Post by redfield » Mon Apr 08, 2019 8:52 pm

Thanks for the speedy reply!
I should have been more precise about what I'm trying to do. The user interface is just the two buttons and three fields: one for the user input (f1), one for the feedback after a guess and one invisible (f3), to hold the random number. I wanted the user to hit the START button and then the GUESS button, until he got the random number originally created when the START button was pressed.

dunbarx wrote:
Mon Apr 08, 2019 7:39 pm
What does this lack, besides beauty, cleverness and style?
Probably you mean something else, but for me it lacks a loop or so, to enable the user of guessing multiple times, until the guess is correct. As far as I understand the code, it will produce a new random number after every guess. What I am trying, is to keep the one random number, until the user gets it right.

As for the actual topic of your post, do you see how this works,

I tried but no, not really :( . What is 'findIfTrue', I can't find it in the Dictionary? Is it also an event like mouseUp?

Well the homework looks like this :|

Code: Select all

on mouseUp
   ask "Guess a number!" 
  if it is an integer then  findIfTrue it
  end mouseUp

on findIfTrue tGuess
   put "Too high!" into toohigh
   put "Too low!" into toolow
   put "Perfect guess!" into bingo
   if tGuess < random(100) then 
      answer "Too low"
   else if tGuess > random(100) then
      answer "Too high"
   else
      answer "You got it! The number was" && tGuess
   end if
end findIfTrue

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Passing a variable between events

Post by dunbarx » Mon Apr 08, 2019 9:30 pm

Try this, just because. Make a button and two fields. Name one field "latestGuess" and the other "randomNumber". In the button script:

Code: Select all

on mouseUp
   put random(100) into fld "randomNumber"
   ask "Guess a number!" 
   repeat
      if it = fld "randomNumber" then
         answer "You got it! The number was" && it
         exit to top
      end if
      put it into fld "latestGuess"
      if fld "randomNumber" > fld "latestGuess" then 
         ask "Guess again" with "Your guess was too low" else ask "Guess again" with "Your guess was too high" 
      end repeat
   end mouseUp
Now then.

You do not need those fields, but that is for later. Right now they will show you directly what is going on as the procedure develops.

I had mentioned that reloading a new random number would not work here.

The "findIfTrue" is a custom handler I made up to show you how to pass a value from one handler to another. It is the subject of your post after all :wink: , though, again it is not needed here.

Write back with complaints. The process as written is fragile. See if you can break it; it may need defenses.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Passing a variable between events

Post by jacque » Tue Apr 09, 2019 5:33 pm

I think it's more useful to help with the actual question than to start new ones. :-)

The reason the scripts fail is because variables are only good inside the handler that creates them unless you set them up in specific ways {which would require a slight change to your script organization.} When the first mouseUp finishes, the value of x disappears. There are many ways to manage this, but the easiest way for now (though not the best way) is to put the value into a hidden field so that the second mouseUp knows where to find it. The second button just has to get the field's text and evaluate it.

Later you'll want to handle things differently but try this first. When you get it working, ask us about script local variables and how to call one handler from another.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Passing a variable between events

Post by dunbarx » Tue Apr 09, 2019 6:28 pm

Jacque.

The OP is on two different tracks at the same time. The first track ostensibly was about passing variables. The second track was the gadget itself, which was the actual issue he was having, and was introduced right at the start. The first is premature. A bit more familiarity with LC is required.

I recommend that he build the gadget first, then move on to variable management and passing parameters between handlers. I think working with an actual stack is the best way to learn, adding functionality and solving problems with actual code that one cares about.

@RedField. You can go either way or both. Write back with your progress. We can handle either or both.

Craig

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Passing a variable between events

Post by MaxV » Mon Apr 22, 2019 10:57 pm

I suggest you to study the custom properties: https://livecode.fandom.com/wiki/Custom_properties
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”