Sony Playstation Portable Lua Programming: Tables (Arrays)
Tables in Lua are very much like variables. The main difference is that tables can be used to store multiple amounts of data, rather than just one item of data. You can think of this as a storage container with many "drawers" in it for storing different information. Any game you make in the future will most likely use several Tables. Using tables in Lua is basicly the same as using arrays in other programming languages. We are going to learn a few other things in this tutorial as well.

Write a Simple Program That Uses Tables
Let's say we're making a little game that is going to have a total of 5 types of enemies, and 2 players. In order to do that we are going to have to give the program some information about those characters. What information about the players and enemies do you suppose we need?
I'm sure you can think of tons of ideas, but since you're just learning we're only going to keep it very simple. In order for our enemies to be killed they have to have health. So for one, we need to give the program the information to keep up with the enemy health. We will also use information for identifying the type of enemy we are using.
For our players we will need health as well. What else could we use? How about the type of weapon our player has!
That should be able to get us going for now.

We will now start the table learning. Please only add the code to your file when instructed to. In this tutorial I will make several references to code that you shouldn't use in your file. Tables are defined in the following way:

tablename = { }

First is the name of the table (any name you like), followed by the equals sign, and then a left and right (open and close) curly bracket. This creates an empty table with no data inside. To assign data to the table you simply add it inside the brackets, separating each one by a comma. Here's an example:

myTable = { 12, 14, 16, 18, 20 }

Keep it mind this is basicly exactly the same as variable use. It just looks a little different. Now, how do we use this? Well, each section of data inside the table is indexed into an array element that we can call by using the name of the table with opening and closing square brackets to the right of it, with a number of the index inside. WHAT?! Let's look:

luckyNumber = myTable[1]

Here we assigned the value of the FIRST section of data (which is 12) in our table to the variable luckyNumber. Look back up at the table I made. Inside the curly brackets how many sections of data do we have? If you said five you are correct. The first was 12, second was 14, third was 16 and so on. We can reference that data very easily in the way I described above. The indexing starts at 1, unlike some languages which start at zero. So, here are all the table elements that we are able to use from this table.

myTable[1]
myTable[2]
myTable[3]
myTable[4]
myTable[5]

At this point I think you should try to write your own simple program using a table. Remember the first tutorial we did when we simply printed a line to the screen? Instead of variables create a table, and use the print command to print one element of your table. You could also make it print several or all elements with multiple print statements. But remember to change the Y value of the print command if you do, or else it will print that big blob we talked about earlier. You do not have to change the X value, as that value refers to horizontal position. The program will not automatically print to the next line, you have to tell it to. You will have to play with how many pixels down it needs to go to keep it from overlapping, or from being too much distance. Use the print command the same way you did with a variable, but instead with your table element : tablename[number]. Go ahead and try, I'll wait here I swear!

Alrighty, I hope you did well. I would also like to point out that you can create empty tables and later in your program assign values to them. Here's an example of creating an empty table, and then assigning values to different elements of the table.

myTable = { } myTable[1] = "apple" myTable[2] = "orange" myTable[3] = "banana"

Notice you can use strings of text as well, instead of numbers, as long as you enclose them in quotation marks.

Next, we are going to get a little more complex with tables. You can add an extra dimension to refer to your table elements. This is sometimes referred to as dictionaries or types. This is very very useful in making your code organized and readable. Let's see an example. I'll make an empty table and then the next line will introduce this method.

shirt = { }
shirt[1] = { color = "blue", size = "small" }

Basicly what we did was create a table, and then in our element shirt[1] we created another table within our table. Now, let's say we wanted to use this in a program to get our shirt color and size. It would look like this:

shirt[1].color
shirt[1].size

If we used that in a print statement or in any other way the result of shirt[1].color would be blue and shirt[1].size would be small. It isn't so bad, just try not to think about it too much!

Now, on to our program. First, we are going to create a table to store our enemy information. This will hold the health of the enemies and the type of enemy. This is the first line of code (FINALLY!) in our program, so put it in our new empty script.lua file. First line is the old faithful color object. Feel free to use any color besides green.

green = Color.new(0, 255, 0)
Enemy = { }
Enemy[1] = { type = "gargoyle", health = 100 }
Enemy[2] = { type = "vampire", health = 100 }
Enemy[3] = { type = "goomba", health = 100 }
Enemy[4] = { type = "ghost", health = 100 }
Enemy[5] = { type = "zombie", health = 100 }

There. Now we have 5 enemy elements created that tell what the enemy is and its total health at the moment. On to our two players. Add this code next.

Player = { }
Player[1] = { weapon = "sword", health = 100 }
Player[2] = { weapon = "knife", health = 100 }

There we now have all we need for our example program. Note that in a real game with movement, the Player table would be the perfect place to store the player's position on the screen, and the enemies as well in the Enemy table. This example will not be moving any characters around so we will get to that part when the time comes. What we are going to do is print the player and enemy's info to the screen, and take away the enemy's health if we press the X button. We are only going to use one enemy and player 1 for this. Poor Player 2 will have to sit on the bench until a later tutorial, as well as the other enemies, since we will only be using Enemy 1.

Next, we are going to jump right into our game loop. Let's put in our button setup code also, which you should remember. Now, put this code in next.

while true do
pad = Controls.read()

Now, let's add the clear command to clear the screen each loop.

screen:clear()

Now, we're going to print our players health and weapon to the screen each loop.

screen:print(5,10,"Player 1 Health: " .. Player[1].health,green)
screen:print(5,20,"Player 1 Weapon: " .. Player[1].weapon,green)

In case you're lost, in our first print statement we are printing at X coordinate 5, which is 5 pixels from the left edge of the screen, and Y coordinate 10 which is 10 pixels from the top of the screen. The PSP screen is 480 pixels wide and 272 pixels in height. Then we have the string "Player 1 Health: " which will simply print that string to the screen. Then we concatenated (remember that?) our table element Player[1].health to that string so that it will print right beside it. Do you know what is stored in Player[1].health? You should! Lastly, we have our color which we've used several times before. The second statement uses the same method but prints different info about our character a little lower on the screen, under our first line.
Now on the other side of the screen let's print our enemy info. Put this code in.

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

Next, we are going to use the button input command that we learned to detect if we are pressing the X button, and if we are then we are going to subtract 5 points of health away from the enemy. Let's look at it. Add it in to your code.

if pad:cross() then
Enemy[1].health = Enemy[1].health - 5
end

This is saying that if X is pressed then take 5 points away from our Enemy[1].health element.

Finally, let's buffer, flip and end our loop. Add this code now.

screen.waitVblankStart()
screen.flip()
end

Now save it and run it to try it out, and then come right back here!

Whoa what was that! The enemy's health goes down WAY too fast, and it can go into the negative numbers! We can't use this in a game! This shows you just how fast a loop performs. We can fix this, by making the health only go down when we press X, and not while it's held down.

First of all near the top of your code before your tables add this line in. Under or above your color variable will do fine.

oldpad = Controls.read()

Now in the line that is "if pad:cross() then" change it to this:

if pad:cross() and oldpad:cross() ~= pad:cross() then

...and finally, right after your screen.flip() and before "end" add this line in:

oldpad = pad

I won't confuse you with details for now. Put simply this code makes sure you're not pushing the same button that you were the last time the loop performed, and if you are then our "if" statement will not perform. ~= means not equal to. We will learn about operators soon. Save it. Run it and test it.

Well all is fine except that it can still go negative. Speak of the devil. We are going to learn another operator here.

On our line that reads "if pad:cross() and oldpad:cross() ~= pad:cross() then" change it to this:

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

We added in Enemy[1].health > 0. > means greater than, which you may know already. So what this whole line is saying now is if the X button is pressed, AND we are not holding the same button as in the last loop, AND the enemy's health is greater than zero then (decrease health by 5). Note that ALL of these conditions have to be true in order for this to go through. Save it and run it, and now all should work fine. The enemy's health cannot go below zero.

Well, that's about it for this tutorial. Note that the main purpose of this tutorial was tables. So if you don't completely get the other stuff, that is OK. I think we both need a break. I know I sure do. See ya in the next tutorial.

Button Input | Expressions

Please welcome tanck1933, our newest member.

Who's Online:

Total Members: 1203
Total Posts: 13034
Total Topics: 1478
Total Categories: 7
Total Boards: 25

Recent Posts:

Re: Sound problem by chi-kitory
Re: Sound problem by acsct3
Re: Sound problem by cmbeke
Re: Sound problem by Hadesminion13
Re: Sound problem by acsct3
Re: Sound problem by Hadesminion13
Re: Sound problem by acsct3
Re: Sound problem by w00tguy123


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