Sony Playstation Portable Lua Programming: Variables
In this tutorial we are going to learn how to use variables in your programs. Variables are very important and will surely become one of your best friends later on. You can think of variables as storage containers that will hold information for you that you can recall at anytime in your program.

Write a Simple Program That Uses Variables
Now we are going to make a simple program that will put some variables to use.
Also, we will learn how to do simple arithmetic in Lua.

This time around we will be commenting each line of our code.
Let's start off once again by creating a color object, which we'll use to print our information to the screen. This time let's use the color green.

-- Green Color Object
green = Color.new(0, 255, 0)

Now, let's create our very first variable. This will be a variable used to store your birth year. Feel free to put your own birth year in place of 1981. In fact, I recommend it!

-- Store birth year in variable myBirthYear
myBirthYear = 1981

There! What we've done is take the number 1981 (or your birth year if you used it) and stored it in the variable which we named myBirthYear. We will later use this variable in our program.

Let's make another variable under this one now that will store the current year.

-- Store current year in currentYear
currentYear = 2006

Now, let's create one in a slightly different way under that one.

-- create an empty variable
myAge = nil

Notice this time we assigned the nil value to the variable. This simply means nothing is assigned to the variable yet. We will later store something in the variable.

OK, now we are going to create one last variable just to show that you can also store strings (text) into them as well as numbers. This is done by enclosing the text within quotes.

-- Store some text in a variable
someText = "My age is roughly "

Alright, I'm sure you've had it with creating variables, so let's use them now.
Here's what we're going to do with this program. We're gonna use our variables and some basic math to roughly calculate your age. So, go ahead and add this code to your program.

-- Subtract myBirthYear from currentYear and store in myAge
myAge = currentYear - myBirthYear

This will take the value stored in myBirthYear and subtract it from currentYear, and then store the answer in the myAge variable. Now myAge has been assigned a value!
Now, let's use the print command that we used in the last lesson to print our text variable to the screen.

-- Print our text variable to the screen
screen:print(10,100,someText,green)

The only thing we did different from the last time we used this (besides the color) is the fact that we are printing a variable's information instead of direct text. Note that no quotes are required to do this. So, this will print whatever is stored in someText onto the screen in green text at X-10 Y-100.
We are going to want to print the result of our age right beside this line so let's learn a simple new command. To print myAge to the screen right beside the text of someText we can use double periods (..)
This is called concatenation. So now, CHANGE the last bit of code you did in order to make it look like the code below.

-- Print our text variable and age to the screen
screen:print(10,100,someText .. myAge,green)

Finally, let's throw in the screen.flip() and a loop to finish off our program.

-- Buffer offscreen to onscreen
screen.flip()
-- Loop forever
while true do
screen.waitVblankStart()
end

Save your program and run it to see the result.
By using these variables, we can change myBirthYear and the program will automatically calculate someone elses age. Without variables you would have to change multiple (sometimes THOUSANDS) of lines of code in some programs.

In this tutorial we only used subtraction. For this we simply used ( - ) just as you learned in school.
For addition you use +
To multiply you use *
To divide you use /

Writing Your First Lua Program | Button Input

Please welcome anthonyjee, our newest member.

Who's Online:

Total Members: 522
Total Posts: 13085
Total Topics: 1515
Total Categories: 7
Total Boards: 42

Recent Posts:

Re: Hello every body... by DeniseVera
Need help please by Robbynator
Re: Hello every body... by horvathann
Re: background criminal record check by backgroundchecker
background criminal record check by backgroundchecker
Intro to Perl Part 2: What are Scalars? & Examples by Chi Kitory
Intro to Perl Part 1 About Perl and Hello World by Chi Kitory
Re: Trying To Get Rid of the Spammers by Charlie


Copyright © 2006-2009 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie