Sony Playstation Portable Lua Programming: Expressions
Expressions have been inching their way into our programs, so now we're going to go in to detail and learn about them. Some that we have seen so far are the "if" and ">" statements. In the end we will have a simple program to make use of these expressions. This program will be very much like our last program, except with more depth. Start up a new file, but don't add in the code unless you're told to, as in the last tutorial.

Putting Expressions To Use
First we have the arithmetic expressions which are very simple. These are very simple so I won't detail these too much. We can add, subtract... etc with these. Here's an example.

x = 3 + 5 -- Addition
y = 3 - 1 -- Subtraction
a = 4 * 5 -- Multiplication
b = 10 / 2 -- Division
c = 2 ^ 8 -- Power of

Not so bad eh? Keep in mind the things I teach you in these tutorials are to get you going with the basics. I will not introduce every single possible command. As you learn how to use what I give you, you will be able to look up information and apply it on your own for the things I don't teach here.

Our next set of expressions will be the relational expressions. These will be highly used in your programming for comparing data. These return either true or false (known as boolean values). Let me list them for you.

  • == (Equal to)
  • ~= (Not equal to)
  • < (Less than)
  • > (Greater than)
  • <= (Less than or equal to)
  • >= (Greater than or equal to)
We can use these to check for all kinds of useful information. For instance, if one of our enemy's health is zero, shouldn't he die? Or how about checking to see whether we have enough money to buy that new sword! Now, it would be kind of hard to use these completely by themselves. Most times they will be used with our next new set of expressions.

My favorite first, the "if" statement. This checks to see if some condition is true, and if it is then it will perform a section of code. We can also add to it so that if the condition is not true it will do something else. A simple "if" expression is written like this:

if <some condition> then
<perform some code>
end

Now a real world example. Let's say we have a program, and in that program we have two variables, myCash and itemCost. myCash is the amount of money we have and itemCost is the price of the item we want to buy. In a game we would have to check to see "if" we have enough money to buy this item. If we do then we will subtract the amount from our money and print a bought message to the screen. If we don't have enough then we will print a message to the screen saying there isn't enough money to buy it. It could look something like this.

myCash = 253
itemCost = 100

if myCash > itemCost then
myCash = myCash - itemCost
screen:print(100,100,"Item Bought",blue)
else
screen:print(100,100,"Not enough money",blue)
end

Let's analyze it. First, we have our variables which tell how much money we have, as well as how much the item cost.
Then, we have our if expression. This boils down to "if 253 > 100 then". It's asking if the value in variable myCash is greater than the value in the variable itemCost. If so ...
Then our next line will take the value of itemCost and subtract it from the value of myCash. in other words 253-100, and then store the answer in myCash. Then we print "Item Bought" to the screen with the next line.
On the next line we have "else" which is the part of the code that will perform in case myCash is NOT greater than itemCost, and if it isn't in this case it will print "Not enough money" to the screen.
Finally, always end the expression with "end".
In the above example, we do have enough money to buy the item. Therefore, the "else" section of code will not perform.
A lot of times you will not need to use else at all, and at times you may need more than one else. In this case you will use "elseif". Here's an example:

myNumber = 4
if myNumber == 1 then
screen:print(100,100,"Your lucky number is 1",blue)
elseif myNumber == 2 then
screen:print(100,100,"Your lucky number is 2",blue)
elseif myNumber == 3 then
screen:print(100,100,"Your lucky number is 3",blue)
else
screen:print(100,100,"Your number isn't very lucky!",blue)
end

This checks to see if myNumber is equal to 1, 2, or 3. If it is then it will print "Your lucky number is (number)". But if myNumber is not equal to 1, 2, or 3 then it will print "Your number isn't very lucky". Note that in this example this could be simplified down as small as our last example with only an else, and no elseifs by saying "if myNumber > 0 and myNumber < 4 then". Coming up next we will learn about the "and" keyword and some others.

Logical operators can be used to compare more than one thing, as I mentioned above. We are going to learn two of these, "and" and "or". There is also a "not".

Let's say you are making a Role Playing game, maybe something like Diablo. The player of your game is about to try to equip a Dragon Slayer Sword. You want the player to have a level 20 chaacter, and also be a Warrior class character in order to let them equip this item. At the top of our program we could have an array that holds the information, such as:

Player = { }
Player[1] = { level = 15, class = "warrior", weapon = "knife" }

Then later on our code to check if the player can equip this item could look like this:

if Player[1].level >= 20 and Player[1].class == "warrior" then
Player[1].weapon = "Dragon Slayer"
end

The first line checks to see if Player's level is greater than or equal to 20, and if the Player's class is the warrior class. If BOTH are true then Player's weapon will be set to the Dragon Slayer. If only one is true, this results to false, and therefore the weapon will not change. In this case our class is indeed the warrior's class, but as you can see our level is only 15, which is not greater than or equal to 20. Our player cannot use this weapon in the example. You are not restricted to use only one "and", for example you could use this in a program:

if hat == "blue" and shirt == "red" and age > 15 and state ~= "Florida" then
screen:print(100,100,"You are accepted!",yellow)
end

Using "or" is exactly the same except that only ONE condition has to be true in order for it to pass as true. For example:

myCar = "Mustang"

if myCar == "Corvette" or myCar == "Mustang" then
screen:print(10,20,"Nice car!",green)
end

In the example it checks to see if myCar is equal to Corvette or if myCar is equal to Mustang. If either one of these is true, then it will print "Nice Car!" to the screen. myCar is not equal to Corvette, but it is equal to Mustang, therefore this passes as true.

OK, now we're going to put this stuff in action. What we're going to do is take our program we made from the last tutorial and edit it with some things we have learned here. So open up your file if you still have it, and if not don't panic, you can download mine Here

In the last tutorial we made the enemy's health decrease by five points each time we pressed the X button. In this tutorial we are going to add some expressions to make the program bring up another enemy when the current one dies. We will also have to check for when there are no more enemies left.

The first thing we're going to do is add in a variable to tell us what the current enemy we're fighting is. We will assign a number to this variable. If you'll notice, in our Enemy array we have five enemies indexed by numbers 1 through 5. We will refer to those enemies with those numbers. Add this variable underneath the green color variable at the top of the code:

currentEnemy = 1

Now we are going to add some code to check if the current enemy's health is zero, and if so we will increase the currentEnemy variable by 1, but only if the currentEnemy number is not over 5, our last enemy. Above your if pad:cross() line add this code:

if Enemy[currentEnemy].health == 0 and currentEnemy <= 4 then
currentEnemy = currentEnemy + 1
end

Notice here we are using our currentEnemy variable instead of a number in our array Enemy array. This way we set the currentEnemy variable to the correct enemy, and anywhere in our code that uses the Enemy array, we can use currentEnemy instead of having to check for each individual enemy (Enemy[1] Enemy[2] etc...). This makes the code more automated. The currentEnemy variable will not go past 5 with the "<= 4" check. That being said, we have some lines we need to change in our code. Find these lines:

screen:print(250,10,"Enemy Health: " .. Enemy[1].health,green)
screen:print(250,20,"Enemy Type: " .. Enemy[1].type,green)

Change these lines to:

screen:print(250,10,"Enemy Health: " .. Enemy[currentEnemy].health,green)
screen:print(250,20,"Enemy Type: " .. Enemy[currentEnemy].type,green)

Now as the enemy changes, it will automatically print the health and enemy type of the correct enemy. We also need to change these two lines:

if pad:cross() and oldpad:cross() ~= pad:cross() and Enemy[1].health > 0 then
Enemy[1].health = Enemy[1].health - 5

Change them to:

if pad:cross() and oldpad:cross() ~= pad:cross() and Enemy[currentEnemy].health > 0 then
Enemy[currentEnemy].health = Enemy[currentEnemy].health - 5

To end it we are going to add a bit of code to print to the screen "All enemies are dead" if all five have been killed. Place this code in your main loop underneath the screen:print lines that print the enemy health and type.

if currentEnemy == 5 and Enemy[currentEnemy].health == 0 then
screen:print(50,100,"All enemies are dead",green)
end

This code says that if our current enemy is number 5 and it's health is equal to zero then print that all enemies are dead. Save your code and run it. Press X until one enemy dies. Then the next enemy will come up, followed by the next, until all 5 have been killed.

I hope this has helped you get a start on learning some expressions.

Tables (Arrays) | Images

Please welcome ##Veronika, our newest member.

Who's Online: 4 Guests, 2 Users
SolidSnake117, Buddy4point0

Total Members: 520
Total Posts: 12867
Total Topics: 1462
Total Categories: 8
Total Boards: 35

Recent Posts:

Re: Extended ASCII by Nielkie
Extended ASCII by Aphonia
Re: Lua 5.1.4 help. by yaustar
Re: Lua 5.1.4 help. by Buddy4point0
Re: Lua 5.1.4 help. by yaustar
Re: Lua 5.1.4 help. by PL3X
Re: Lua 5.1.4 help. by Buddy4point0
Re: Lua 5.1.4 help. by tacticalpenguin


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