Access_Denied's Forums

Site Resources => Tutorials => Topic started by: Access_Denied on June 14, 2007, 03:04:04 AM



Title: Tutorial Three: Variables
Post by: Access_Denied on June 14, 2007, 03:04:04 AM
Now that you already how to print to the screen, we're going to move on to our next lesson: Variables. If you've ever take Algebra or Geometry, then you should already know what a variable is. For those of you that don't know what it is, it's simply a word or number that you use to take the place of another word or number. For example:

x = 24
or
points = "none"

Simple, right?
Now lets start writing our code.
First thing's first, our notes:

--Variables
--June 14,2007:

Now our color variable:

yellow = Color.new(255,255,0)

Now we need to make some variables for our program. Let's say we are going to make a program that tells us how much money you have after buying a new PSP game. First we are going to make a variable that tells the PSP how much money we started with:

StartingMoney = 83

Now we make a variable to tell how much the game was:

GamePrice = 34

Now we need to make a variable to say how much we have left, but since we don't know yet, we'll make a blank variable:

MoneyLeft = nil

By writing "nil" we are saying the variable doesn't have a value. We will give value to it later. Now our main loop:

while true do
screen:clear()

Looks familiar, right? Now we are going to write an equation to see how much money we have left:

--subtract GamePrice from Starting Money to find MoneyLeft
MoneyLeft = StartingMoney - GamePrice

Now, we the PSP knows how much “MoneyLeft” is. But we can't see it because we didn't print it to the screen yet. So let's do that.

screen:print(100,136,"I have "..MoneyLeft.." Dollars Left",yellow)

Now the “screen:print” may look familiar, but what's with the periods that surround “MoneyLeft”, you say? In Lua, by writing two periods like that, you are saying to print the two things next to each other. So by putting the two periods before and after “MoneyLeft”, you are saying to print those words next to the variable money left. Notice “MoneyLeft” isn't in quotes because it is a variable. Variables don’t go in quotes, as only words that are printed go in quotes. Now we'll finish it off with the usual:

screen.waitVblankStart()
screen.flip()
end

Now run your script. If it doesn't work, compare it to this post and try to find out what you did wrong in the script. See ya in the next tutorial!
P.S.: Here're some function signs:
* = multiplication
- = subtraction
+ = addition
/ = division


Hosted by www.Geocities.ws

1