Hey everyone. A couple people had recently asked me for help with LUA, and there may be some people out there that haven't ever programmed before. So if you want to start LUA, and have no programming knowledge, I have written a tutorial for those of you. I don't explain things real good, so I just want to see what you guys think, and if I'm not the kind of person to be doing this, I wont write any more tutorials. I just wanted to see if I can help out.
First off, you are going to have to follow the instructions in whitehawk's tutorial on installing LUAPlayer to your PSP.
Now before you start, download 'Lua Player 0.15' thanks to Shine, from the PSPUpdates download sections here, or at the Official Lua Player website here.
You will need to take the two folders, 'luaplayer' and 'luaplayer%'.
Put them in PSP/GAME/ directory on your PSP.
Open up notepad, or a text editing program on your computer. What you are doing now is typing up your first program in LUA, or 'CODING'. What LUAPlayer does is that it reads code that you have typed and does certain things according to what you have typed. The whole point of this tutorial is learning how to code, or PROGRAM. But this tutorial is designed specifically for LUAPlayer coding. Here are some of the basics to programming and to LUAPlayer:
1. Programming Basics - Variables
Now If you have ever taken algebra before, you have probably heard the word variable. It was a letter that stood for a certain value. For example, in math they would tell you to do a problem: If a=1 and b=2 find a2 + b4(3). Now in programming, you can do the same thing. If you put this in a line of code:
then the variable a now holds the value 1. If you did this in your code, and ran it with LUAPlayer this would show absolutely NOTHING. Why? Well, this does not write anything to the screen, it simply stores it in MEMORY for later use. These variables can store certain values like for example, the X and Y coordinates of a character in your game. In programming, math plays a big role. For example, in my code, if I put:Code:a=1
the variable b would be holding the value 3 and the variable c would be holding the value 2. What value exactly does the variable d hold? Because you put d=b+c then that stores this: the value of the variable b which is 3 PLUS the value of the variable c which is 2, which is equal to 5. You can't only just add things but you can:Code:b=3 c=2 d = b + c
So if I was making a program that finds the value of the circumfrence of a circle, I could just put:
- Subtract -
- Multiply *
- Divide /
And the names of variables dont have to be just one letter. Like up above, They can be a whole word, just not too many letters to the word or you could get an error in your program.Code:PI = 3.14159 Diameter = 10 Circumfrence = PI * Diameter
Also, in Programming, you can store words in variables, also known as strings. You have to put them in quotes, otherwise the program will think it is getting the value from another variable. When a variable stores a number, like up above, it is just called a number variable. When a variable stores a string it is called a String variable. Just like you can add regular number variables together, you can also add string variables together, only you cant add letters together, it works a different way.
Instead of putting a plus sign to add a String, I just put two dots because thats what you do in LUA. For example, if I put this in my code:
The strVar3 variable will then hold the string "HELLO" in it. Variables can also hold booleans. There is nothing hard about booleans, they are either just TRUE or FALSE.Code:strVar1 = "HEL" strVar2 = "LO" strVar3 = strVar1..strVar2
For example, if I put this in my code:
The walking variable holds the boolean TRUE and etc.Code:walking = TRUE sitting = FALSE standing = TRUE
2. Programming Basics - While Loops and IF Statements
GREAT!! Now how do you make a game? There is a LOT more to it. If you did not understand the first section, I suggest you quit now, sorry, I dont really explain thing that good :). Now I must teach you WHILE statements and IF statements.
What is a WHILE statement??? It is a part of code that LUAPlayer will LOOP through, meaning the same piece of code will repeat until you tell it to stop. For now, I will teach you while true do. If I put this in my code:
Then the number variable a will be incremented by 1, or it will become 1 unit greater each time it loops through the loop. Any time in code, when you put the lines while true do and end, anything you put inbetween those lines will repeat until you tell the loop to stop. How do you tell the loop to stop? This is where IF statements come in. These are typed almost exactly like while statements, except they work differently. For example:Code:a = 0 while true do a = a + 1 end
Now, this is very complicated here, so let me explain:Code:a = 1 b = 2 c = 2 if c==2 then c=3 end
Remember booleans? You can check if variables are less than, equal to, greater than, etc. and then store if it is true or false in a boolean. For example, when you use an ==, <, >, ~=, <=, or >=, between two variables, the program thinks you are comparing two variables. For example:
The boolean variable isitgreater holds the boolean TRUE inside of it, because the number variable b (which holds the value 3) is indeed greater than (>) the number 1. There are more operators than just >. Here are more:Code:b = 3 isitgreater = b > 1
== - Equal to> - Greater than
< - Less than
~= - Not Equal to
<= - Less than or equal to
>= - Greater than or equal to
So when you have an IF statement, to make it work, you first type if then a space, then a boolean (comparison), then a space and you type then. Then, whatever you want to happen if the comparison is TRUE you type inbetween your if statement, and the word end, which you must not forget.
So then the code:
means that at first, the number variable c will store 2 inside of it, and once LUAPlayer comes up to the if c==2 then statement, it will check if the number variable c does store 2 inside of it, and indeed it does. So it will go inbetween the if statement and the end and do whatever code is there. Inbetween them, it tells it to store 3 in the number variable c. And then the program comes to an end.Code:c = 2 if c==2 then c=3 end
Now, back to while statements. Lets take a look at this code:
What this does is that it will keep looping, incrementing the number variable a by 1 each time it goes through the loop. It ignores the IF statement, until a stores 100. When a does store 100, the while loop will finally end. Why does it end? I put whenever a does store 100 then break. What break does is that whenever you are in a loop, the loop will stop and move on with the code after the loop.Code:a = 0 while true do a = a + 1 if a==100 then break end end
With while loops, our program will not just do something real fast and then be done. If we were displaying something on the screen, we have to give it some time to show. Whenever LUAPlayer reaches the end of your code, it will stop, so if we just put one command to display something on the screen in our code, it will literally run through the code so fast that there will be no time to see it on the screen. With a while loop though, we can make the program loop through the loop after we display something on the screen, and when we want to stop looping the loop, we can hit the START button on our psps. This way, it will keep displaying that on the screen UNTIL we hit START.
3. Programming Basics - Displaying text on the screen w/Lua Player
NOW, we can finally get to displaying things on the screen, but you need to have some knowledge with coordinate planes.
Have you ever graphed anything on a coordinate plane in math? Well in programming, when displaying things on the screen you have to tell the PSP where you want to display it. You have to tell it what X Coordinate you want to display it at and what Y Coordinate you want to display it at. For example:
You see how the text "Hello There" is displayed at a certain position, or in math - (3,4)?? The first number would be the X Position, then the second number would be the Y Position.
But there is also a Color that the text must be, and in LUAPlayer, there is a special type of variable, not a number variable, string variable, or a boolean variable, but a Color Variable. How do you make a color variable? Simple. First you type the name of the variable (in this case the best thing to do would name it the name of the color), then '=' then type 'Color.new(' then type a number between 1-255 telling how much RED the color should have, then type a comma, then type a number between 1-255 telling how much GREEN the color should have, then type another comma, then type a number between 1-255 telling how much BLUE the color should have, then type a ')' and you are all done. For an easier description, I am going to tell you the syntax. To let you know about syntaxes, everything that is BOLD means you specify what it should be. Here is a syntax for defining colors:
So If I want to make all of the basic colors in my program, I would type:Code:variable name = Color.new(RED VALUE, GREEN VALUE, BLUE VALUE)
Now that you know syntaxes, I can teach you things a lot easier. And now I can finally teach you how to write text to the screen, or PRINT, as I call it. Dont get confused, it doesn't print it in a printer, just writes text to the screen. Here is the syntax for "Printing" text to the screen:Code:white = Color.new(255, 255, 255) black = Color.new(0, 0, 0) red = Color.new(255, 0, 0) green = Color.new(0, 255, 0) blue = Color.new(0, 0, 255) yellow = Color.new(0, 100, 0)
Once you call this screen:print() command, it wont show it on command, it gets it ready to be shown on screen in memory. To actually display it on screen, you have to type screen.flip() afterwards. This way, it will take less time than showing it every time you call screen:print().Code:screen:print(X Position (IN PIXELS), Y Position (IN PIXELS), Number or String Variable, Color variable)
Open up notepad, or any text editing program, and type this in and save it as script.lua inside the PSP/GAME/luaplayer/ directory on your psp:
Run LuaPlayer now. You should see in the upperleft corner the text "Hello there" in the color white and it should only show for a short time. How exactly does it run through the code? Well, First off, it sets white as a color. Then it gets ready to print the string Hello there with the color white. Then, it displays everything on the screen, screen.flip(). Then it makes a number variable called loop. It goes through a while loop that increments the loop variable by 1 each time it loops through the loop. Once loop holds the value 1000 the loop stops, which ends the program.Code:white = Color.new(255, 255, 255) screen:print(100, 100, "Hello there", white) screen.flip() loop = 0 while true do loop = loop + 1 if loop == 1000 then break end end
Why are the X and Y coordinates where you print the screen so large? Well, the PSP has 480 coordinates (pixels) going left and 272 going down. But they are real small coordinates, because they are just pixels. If you think you are ready, then you can try whitehawk's tutorial. Or, if you want to continue with my tutorials, you can.
4. Programming Basics - Displaying Images on the screen w/Lua Player
Now that you know how to display text at a certain position on the screen, I will teach you how to display an image on the screen, or blit as I like to call it when im dealing with LUA. Before you actually display an Image on the screen, you have to have an Image Variable. When you declare an Image Variable, you are loading an Image from a certain path. The Image must meet these requirements so that LUAPlayer does not process any errors:
1. The Image must be less than or equal to 500 pixels wide
2. The Image must be less than or equal to 500 pixels tall
3. The Image must be of PNG or JPG format (it is .JPG or .PNG in filename)
Now, we are going to write a new program, so in your text editor program, make a new document. First of all, on your PSP, go inside the PSP/GAMES/Lua Player 0.15/ directory. Right click on the Image below and save it as IMAGE.PNG in that directory.
First of all, I need to teach you the syntax of declaring Image Variables. Here it is:
In the first line of our code we are going to put:Code:variable name = Image.load("Path of Image")
What this does is store the image - "IMAGE.PNG" - inside the variable luaImage. Once you have that done, I will teach you the syntax of displaying Images on the screen. Here it is:Code:luaImage = Image.load("IMAGE.PNG")
Remember, this gets the image ready to be displayed, it does not display it! To display it we have to use screen.flip() after it. Now that we have learned that, we can put it in our code. But remember, this will have to be displayed at a certain x position and y position. We will want to probably center it on the screen. To do this, I have a simple formula for you that calculates what the X coordinates and Y coordinates are of an Image that is displayed at the center of the screen. Here is the formula (this is not code, it is just a formula!):Code:screen:blit(x position (in pixels),y position (in pixels),Image Variable (Image you want to display))
Remember the width and height of the screen? The width is 480 and the height is 272. We can throw these in to our formula:Code:x position = (width of screen - width of image) / 2 y position = (height of screen - height of image) / 2
We have only one more thing to find out, the width and height of our Image. To find this out, go back up and right click on the Image and click properties. Somewhere in there it should tell you how wide and how tall the image is. If you got the right height and width you got:Code:x position = (480 - width of image) / 2 y position = (272 - height of image) / 2
144 pixels wide
80 pixels tall
Now we can plug that in to our formula:
If we do the math in the parenthesis we get:Code:x position = (480 - 144) / 2 y position = (272 - 80) / 2
Now all we do is divide it in half:Code:x position = (336) / 2 y position = (192) / 2
Now we have the correct X & Y position on where to center our Image. Lets put the code to display the Image in the center in our code:Code:x position = 168 y position = 96
Now, we want our Image to display for some time, so add a while loop to your code that ends when a variable is equal to about 5000, remember to increment the variable by 1 each time through the loop. Here is what I would add:Code:screen:blit(168, 96, luaImage) screen.flip()
Lets be more realistic, this actually is what I would add:Code:counter = 0 while true do counter = counter + 1 if counter == 5000 then break end end
This does the same thing the while loop up above would do. This just pauses for a certain amount of time, precisely in milliseconds. Here is the syntax, although it is pretty self-explanatory up above:Code:screen.waitVblankStart(500)
The best thing to do is make your code as short as possible, so we will add the screen.waitVblankStart(50 0) line to our code. Once you add this line to your code, your final code should look like this:Code:screen.waitVblankStart(Number Variable (Amount of milliseconds to pause for))
Lets spice our code up a bit and make it neat. I will teach you about comments now. A comment is something you put in your code that is there for reminder of things, or to tell whomever may be looking at your code what a certain line of code does or etc. The main thing about comments is that LUAPlayer ignores them, it doesn't care about them. So when LUAPlayer runs the code, if it runs up to any comments, it simply skips them. Here is the syntax for a comment:Code:luaImage = Image.load("IMAGE.PNG") screen:blit(168, 96, luaImage) screen.flip() screen.waitVblankStart(500)
It is very simple, just a comment preceded by --. You can put a comment after code on the same line, or on a different line. It does not matter, just anything after -- on the same line, LUAPlayer will ignore. So in our code we could put:Code:--put your comment here
Great, now that you are done, save the code as SCRIPT.LUA in the PSP/GAMES/Lua Player 0.15/ directory. Now load up LUAPlayer, this is what you should get:Code:---------------------------------------------------------- -- IMAGE PROGRAM, Displays an Image on screen -- ---------------------------------------------------------- --Load the image up luaImage = Image.load("IMAGE.PNG") --Get the Image ready to be displayed on screen screen:blit(168, 96, luaImage) --Display the Image on screen screen.flip() --Wait a bit so that you can actually "see" the image screen.waitVblankStart(500)
That is all I have for now, and I will update it more, and tell me what you guys think. It always makes me proud to know I have helped someone.