figuring out this language

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

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

figuring out this language

Post by ethanCodes » Sun Feb 14, 2016 9:14 am

Hi! I have never used a language like LiveCode before. I have always used C# or Java before. I am having trouble figuring out the language. I am supposed to be making an app that lets the user input a number between 1-100 and then presses a submit button. The program then outputs a string into a textbox saying whether the number guessed matches the randomly generated number or not. I've been able to search to find most of the syntax (at least I think most of it is right), but I am having trouble figuring out the output portion of it. If anyone could let me know what I'm doing wrong that would be great! Here is the code as I have it now:

Code: Select all

on mouseUp
   if field = random(100) then results = "Amazing! You guessed it!"
   
   Else if field < random(100) then results = "Nope! Too low!!"
   
   Else if field > random(100) then results = "Oops! Too high!"
end mouseUp

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: figuring out this language

Post by jmburnod » Sun Feb 14, 2016 10:55 am

Hi Ethan,
I just see some scripts for this job in my stacks.
I hope this help.

One btn "bVerifNumber" wit this script

Code: Select all

on mouseup
  verifNumber
end mouseup
--for a new Number
One btn "bNewNumberToGuess" wit this script

Code: Select all

on mouseup
   doNewNumberToGuess
end mouseup
-- card script

Code: Select all

local sNumberToGuess
on opencard
   doNewNumberToGuess
end opencard

on doNewNumberToGuess
    put getNumberToGuess(100) into sNumberToGuess
end doNewNumberToGuess

function getNumberToGuess pNumber
   return random(pNumber)
end getNumberToGuess

on verifNumber
   ask "Type one number between 1 to 100"
   if isNumber(it) is false  then 
      answer "I need a number"
      exit verifNumber
   end if
   if it = sNumberToGuess then   put "Amazing! You guessed it!" into tAnswerResult
   if it < sNumberToGuess then put "Nope! Too low!!" into tAnswerResult
   if it > sNumberToGuess then put "Oops! Too high!" into tAnswerResult
   answer tAnswerResult
end verifNumber
Best regards
Jean-Marc
https://alternatic.ch

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: figuring out this language

Post by Klaus » Sun Feb 14, 2016 12:10 pm

Hi Ethan,

1. welcome to the forum! :D

2.
I have always used C# or Java before.

We wouldn't have guessed from your code! :D

3. You may want to take a (quick) look at these stacks to get the very basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

3. What Jean-Marc says!

4. Here some general hints on your code:
...
if field = random(100) then results = "Amazing! You guessed it!"
...
You need to add a descriptor of the target field like:
field "name of field here"
or
field 1
or
field ID 1004

Everytime you query: random(100)
it will return a different number, so all of your IF ELSEIF cases could be correct!
I think you want to FIRST put random(100) into avariable and then check what
the user has enrtered against this variable.

To fill a vairiable with values you cannot:
...
xyz = "astring or whatever"
...
like in other languages, you need explicitely:
...
put "a valid string or whatever" into xyz
...
To show the user an "alert" or dialog just use:
...
answer "Whatever you want to tell the user..."
...


Best

Klaus

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: figuring out this language

Post by FredBeck » Sun Feb 14, 2016 12:21 pm

Hi, welcome to the forum!
You syntax is horrible! I suggest you make use of the user guide and the dictionary...
Also I think there is a hangman example in the sample stacks...

A few points

1) How would you do it in your favourite language? Here you're generating three different random numbers, the game is rigged!

2) to assign a value to a variable, use
put [value] into tVar
not tVar = Value

3) "field" is a keyword to point at a certain field in your stack. You say
field 1 of this card
field "Input"
field ID 1003
etc.

4) There are no types in livecode script, so it's up tu you to check that the input is valid

...

Here's my approach (stack attached)

Code: Select all

local sNumberToGuess, sMaxNumberOfTries, sCuuentTryNumber

on mouseUp
   startGame
end mouseUp


command startGame
   
   -- initialize game variables
   local tRanMin, tRanMax
   put field "RanMin" into tRanMin
   put field "RanMax" into tRanMax
   put tRanMin + random(abs(tRanMax - tRanMin)) into sNumberToGuess
   
   put field "TryMax" into sMaxNumberOfTries
   put 1 into sCuuentTryNumber
   
   -- ask first guess
   askForNumber
   
end startGame


command askForNumber
   
   -- do we have any tries left?
   if sCuuentTryNumber > sMaxNumberOfTries then
      -- quit game
      answer "You lost! the number was" && sNumberToGuess & "."
      exit askForNumber
   end if
   
   -- ask for input
   local tGuess
   ask "Try" && sCuuentTryNumber && ": What is your guess?"
   put it into tGuess
   
   -- player is bored or clicked on Cancel button?
   if tGuess is empty then 
      -- quit game
      exit askForNumber
   end if
   
   -- is the input valid?
   if tGuess is not a number then  -- maybe we should chech if it's a positive iteger?
      -- ask again with same sCuuentTryNumber
      askForNumber
   end if
   
   
   -- we have a number!
   if tGuess = sNumberToGuess then
      answer "Congratulations! You win i" && sCuuentTryNumber && "try!"
      exit askForNumber
   else if tGuess > sNumberToGuess then
      answer "Too High!"
   else
      answer "Too Low!"
   end if
   
   -- increment the current try number and roll again!
   add 1 to sCuuentTryNumber
   askForNumber
end askForNumber
Attachments
guessNum.zip
updated
(1.61 KiB) Downloaded 256 times

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: figuring out this language

Post by Dixie » Sun Feb 14, 2016 1:36 pm

Code: Select all

on mouseUp
   put random(100) into theNumber
   
   put random(100) into theNumber2
   if theNumber = theNumber2 then 
      answer "Matched"
   end if
   if theNumber < theNumber2 then
      answer "Too Low"
   end if
   if theNumber > theNumber2 then
      answer "Too High"
   end if
end mouseUp

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: figuring out this language

Post by FredBeck » Sun Feb 14, 2016 2:23 pm

Small modifications after testing, mainly typos, and the exit parts.
After a while you'll see livecode as plain english :D

Code: Select all

local sNumberToGuess, sMaxNumberOfTries, sTryCounter
-- changed sTryCounter (prev. sCuuentTryNumber, too long and with a typo)

on mouseUp
   startGame
end mouseUp


command startGame
   
   -- initialize game variables
   local tRanMin, tRanMax
   put field "RanMin" into tRanMin
   put field "RanMax" into tRanMax
   put tRanMin + random(abs(tRanMax - tRanMin)) into sNumberToGuess
   
   put field "TryMax" into sMaxNumberOfTries
   put 1 into sTryCounter
   
   -- ask first guess
   askForNumber
   
end startGame


command askForNumber
   
   -- do we have any tries left?
   if sTryCounter > sMaxNumberOfTries then
      -- quit game
      answer "You lost! the number was" && sNumberToGuess & "."
      exit to top
      -- exit askForNumber caused a bug. If it wasn't caled on the firdst try, then the first guess call was still alive.
   end if
   
   -- ask for input
   local tGuess
   ask "Try" && sTryCounter && ": What is your guess?"
   put it into tGuess
   
   -- player is bored or clicked on Cancel button?
   if tGuess is empty then 
      -- quit game
      exit to top
   end if
   
   -- is the input valid?
   if value(tGuess) is not a number then  -- maybe we should chech if it's a positive iteger?
      -- ask again with same sTryCounter
      askForNumber
   end if
   
   -- we have a number!
   if tGuess = sNumberToGuess then
      answer "Congratulations! You win in" && sTryCounter && "tries!"
      exit to top
   else if tGuess > sNumberToGuess then
      answer "Too High!"
   else
      answer "Too Low!"
   end if
   
   -- increment sTryCounter and roll again!
   add 1 to sTryCounter
   askForNumber
   
end askForNumber

bhh32
Posts: 7
Joined: Tue Feb 16, 2016 8:31 am

Re: figuring out this language

Post by bhh32 » Tue Feb 16, 2016 8:48 am

Hi OP,

I too and used to languages such as C#, Java, and C++. This LiveCode language is driving me up the wall. I don't understand it at all, and there are really no good tutorials out there for it that I can find. I have this same exact project that I'm working on. Must be a common beginner project. Good luck, hopefully you can find something a little more understandable.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: figuring out this language

Post by Klaus » Tue Feb 16, 2016 10:03 am

Hi bhh32,

1. welcome to the forum! :D

2.
bhh32 wrote:...I don't understand it at all, and there are really no good tutorials out there for it that I can find....
if you are still interested, you could check the link I posted above:
http://www.hyperactivesw.com/revscriptc ... ences.html
Great resources to learn the basic concepts of Livecode!

And of course the lessons from the mothership:
http://lessons.livecode.com

Where did you say you've been looking for tutorials? 8)


Best

Klaus

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

Re: figuring out this language

Post by jacque » Tue Feb 16, 2016 6:04 pm

bhh32 wrote:Hi OP,

I too and used to languages such as C#, Java, and C++. This LiveCode language is driving me up the wall.
If it helps, I once tried to learn C++ and it took me 2 days to write a single line of code that would compile. It's all in what you're used to.

LiveCode attempts to be as English-like as possible. All statements begin with a verb. References to objects on a card are descriptive. The message hierarchy is important and you'll need to know about it for anything other than the most simplistic scripts.

If you think how you'd describe what you want to do in pseudo code, you'll often be close to the LiveCode syntax (there are exceptions.)

Do look at the scripting conference stacks that Klaus linked to, at least the first few. They were written for users who are new to LiveCode and assume no previous experience. Take them in order.

Browse through the dictionary randomly and read the example syntax statements. You'll start to see how the language is structured. Also look at the LiveCode lessons on the web site, not so much to accomplish a task but just to read the scripts. The best way to learn the language is to read what others have written.

Here's another online course that may help:

http://livecode.byu.edu/indexgeneric.php

And don't give up. Once the penny drops it all falls into place at once.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: figuring out this language

Post by Thierry » Tue Feb 16, 2016 6:35 pm

jacque wrote: And don't give up. Once the penny drops it all falls into place at once.
This is the tutorial I'm pointing for any experienced developer wanting to learn about LiveCode:

https://livecode.com/resources/guides/developers-guide/

It did help them... well, some of them...

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: figuring out this language

Post by ethanCodes » Thu Feb 18, 2016 3:29 am

Thank you every one for the replies! A lot was explained that makes a lot of sense now! Hopefully I will be able to figure it out now. I know my syntax is terrible in the code I posted, I had literally no idea what I was doing because I can't seem to find what the syntax should be so I was just guessing at whatever! I have to say I have never had so much trouble trying to find what the syntax for a language is, and the Dictionary for this is horrible. But I'm sure once I start learning it it will work out great!

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

Re: figuring out this language

Post by dunbarx » Thu Feb 18, 2016 3:43 am

Hi.

I don't think your first syntax was terrible, I thought it was terrific.

Of course it didn't work very well.

But it ought to indicate to you that the english-like syntax of LC is indeed so (seemingly) simple and accessible that you thought you could jump right in with what appeared to be valid code. Could you possibly say that if you tried to write machine code? Assembler? C++?

My point is that you are MUCH closer to actually "getting" this language than you think. And the reason for that is LC is indeed very simple and accessible. It takes a few months to learn to drive. So does a car. And the similarities are striking. When you see a car, you sort of know how to drive, but in order not to kill yourself, you need instruction and practice. In order not to embarrass yourself, make a calculator or an address book. Take the time.

Oh, the dictionary will not seem so unhelpful once you learn to drive it.

Get goig.

ethanCodes
Posts: 46
Joined: Sun Feb 14, 2016 9:08 am

Re: figuring out this language

Post by ethanCodes » Thu Feb 18, 2016 4:04 am

I am definitely starting to catch on! This is what I've got so far. I'm getting a runtime error when trying to empty the field Input. Any ideas?

Code: Select all

put random(100) into rand
on mouseUp
   
   put field Input into guess
   repeat while guess is not rand
if rand = guess then 
   answer "Great job! You guessed it!"
   empty field Input
else if guess < rand then
   answer "Sorry! You guessed too low!"
   empty field Input
else if guess > rand then
   answer "Not Quite! Too High!"
   empty field Input
end if
end repeat
end mouseUp


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

Re: figuring out this language

Post by dunbarx » Thu Feb 18, 2016 5:33 am

Right on.

Your syntax is just awf... er, terrific.

I did not look at it closely,, but will soon. I just know it.

Know right now that ALL lines of code must be placed inside a handler. That is, between the "on" and the "end".

Get going.

Craig

EDIT. I put that line where it should be. Not sure if it is what you intended, but it compiled, and I get an answer. And please ALWAYS quote literals and object references. For example: field "input". Don't get Klaus on your case...

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: figuring out this language

Post by Simon » Thu Feb 18, 2016 6:20 am

Hi ethanCodes,
You are going to give yourself a headache.
Look at how your loop never stops (well until the input is correct), it's pretty much going to lock-up the computer. I'd be surprised if you can type a new answer into the "input" field.
Check out "exit repeat" and "on enterInField" in the dictionary.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”