Showing and Getting Information from the User
Here we will learn how to communicate with the user in other ways. We have learned to use the "Answer" command to put up a Dialog Message for the user and the "Ask" command to ask them a question and get back an answer. Now we will learn how to put up a dialog with buttons for answers and then we will explore "Text" fields to exchange information back and forth with the user.
1. Using Text Fields to Get or Put info to the User
An even more useful way to get or show info to the user is by using Text fields. that allow us to show information to the user or get information from the user
We often use the following objects in programs: Label field and Text Entry field. These are both fields (the type of object in LiveCode) withe properties (like - name, color, size, position, etc)
The "Label" field is used to place information on your card/screen to help the user and that generally does not change. You use this for titles "MY Program", labels "Address", and text "Please type your full names"
The "Text Entry" field is used to get information from the user (like their name, age, etc) or put information on the screen and that changes often (like scores or timers).
In the last lesson, we used the Ask command to get info from the user but if you are asking for too many things, it gets tiring always clicking on buttons. (You have to keep going from the keyboard to the mouse and back again) and the Answer command to show info (But it interrupts the flow of the game to have to always be closing boxes.)
Text fields work better.
2. The Get command.
We use the Get command get or retrieve information into the "it" variable. Then you can test it or "Put" it into another more permanent variable. We also use it to move information from the card/screen. (To work with data, we have to put it into a variable, change it and then put it back for the user to see)
Syntax of the command:
Get "x"
where "x" is either an object (field "name") or a variable (theName) or text
e.g.
get field "name"
get field "address"
get "hello"
get theScore
When each of the above commands finish, the variable "it" has the result. You can then test it, look at it or do something with it.
e.g.
get field "name"
put it into x
get field "address"
put it into field "ShipAddress"
get "hello"
put it into field "greeting"
get theScore
if it is > 100 then
answer "Congratulations! You won"
end if
note:
As with other commands, the variable called "it" is only temporary. It will get overwritten with the next command. So do not depend on it. Test it right away or do something else with it (put it into a more permanent place - put it in your own variable
3. The Put command.
We use the Put command to move information to and from the card/screen. (To work with data, we have to put it into a variable, change it and then put it back for the user to see)
Syntax of the command:
Put "x" into "y"
where "x" and "y" are either objects (field "name") or variables (theName) or text
We can use it to move the info from the screen field directly to our own variable (without using Get, It or 2 coammands)
e.g.
put field "name" into theName
put field "address" into theAddress
put "hello" into field "message"
put theScore into field "Current Score"
Example Using Fields
Let's see how we can do our last program using text and label fields
Lets start with a new Mainstack.
We then drag a "Label" field and a "Text Entry" field to the new card
Then change their names using their "Property Inspectors"
Do the same for the Grade:
Drag over a label field and a Text Entry field and name them: "Your Grade" and "Grade"
Add a button and name it "Done"
Then right-click on the button and open up the script ("Edit Script") and add the following code:
// create global variables for saving what the user types
// save what the user types into our variables
put field "name" into yourName
put field "grade" into yourGrade
answer "You are " & yourName & ", and are in grade " & yourGrade
Click on "Apply"
Click on the "Run" button and try it
Now you know how to get info from the user and leave it showing on the screen.
Example - Using "Put" to Update Scores in a Game
"Text Entry" fields are also used to put scores or times on the screen. You just add a "Text Entry" field, name it, and put information into it for the user to see
In this example we have a text field called "score" on the card/screen. We get the score from it, add 10 to it and put it back on the card/screen.
e.g.
put field "score" into yourScore
add 10 to yourScore
put yourScore into field "score"