Sony Playstation Portable Lua Programming: Introduction
Now, we are going to make our first Lua Program for PSP. Keep in mind throughout the tutorials we will be using the Windows version of LuaPlayer. Some things in the Windows version do not work quite as well, or at all. For example, the sound isn't very good. Some newer functions recently added to LuaPlayer also do not work at all in the Windows version. But, for the things we will be doing, all should be fine.
You should put the luaplayer and luaplayer% folders on your PSP in the PSP/Game directory.
If at anytime you want to test your program on your PSP drop your script.lua file in the directory with the EBOOT.PBP file and run LuaPlayer.

Writing Your First Lua Program
Open up your script.lua file with your choice of text editors. Notepad is fine if that's all you have. Our first program is simply going to print something to the screen. Let's get started!

Comments:
The very first thing we're going to learn is how to make comments in your code.
Comments are used to leave notes or information about your code. It is very useful in case you let others look at your code. Believe me, it also helps yourself. If you stop a project and go back and look at your code months later you will most likely be lost within your own code, without comments!

So how do we write comments? That's what we're gonna do right now. Let's start our new program with a title and author comment.

-- My First Lua Program
-- Author: YourNameHere

As you can see, comments are typed in starting with two dashes ( -- ). Anything you type after this will not be read in as code, but will rather be skipped and ignored completely. But, be aware, you must keep the comment on the same line of code. If you use the return key to go to another line you will have to use the dashes again to comment on the next line. We did this above.
That should do you in for comments for now. See this isn't so bad is it?

Creating a Color Object:
Next we're going to create a color object in order to let us print text to the screen. We will assign a specific color to this and use it with the print command shortly. Here's the code. Go ahead and add it to your program, being sure to put it on it's own line. You may skip a line also to separate the code from your Title info.
WARNING:Lua is case sensitive meaning the commands must be typed in the exact case they are shown. Below the code will not work if you type color instead of Color.

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

Now, we have created our color object. This basicly creates a color using RGB values and stores it in the variable, which is red in this case. You can get RGB color values for any color you want in any image program, even Paint. The variable red is where the color information is stored, but that does not mean if you replace it with the word blue that it will change to blue. The variable has nothing to do with what color it is. The color comes from the RGB value. So, using BigFatTurkey instead of red would not make any difference. If you do not understand what I mean when I say variable, don't worry with that because we will learn all about them very soon!

Printing To The Screen:
Ah, the most exciting part of our program. We will now use the print command to print some text to the screen. Put this code underneath your last bit of code.

screen:print(200, 100, "Look! I made text appear!", red)

Let's break it down so you can understand. scree:print simply tells our program to print something to the screen. Next within the parentheses we have several values. These are called arguments, and each function can have different amounts of arguments as well as different kinds. In the print one we are using it calls for four arguments.
Here it is so you can see: screen:print( x, y, stringOfText, colorObject)
The first argument, x, tells the program how many pixels from the left of the screen to print the text.
The second, y, tells it how far from the top of the screen to print the text.
The third is where you type in the the string of text to be printed. Remember to always enclose it in quotes.
And the fourth...hey it's that red color object we created earlier! This tells the program to print the text using the color red, which we created. Keep in mind you must create the color BEFORE using it in a print statement.

Offscreen Buffer To Onscreen Buffer:
This next bit of code may sound confusing. But don't worry, even if you don't completely understand it, you wont be too bad off. Here it is. Put this below your last code, as usual.

screen.flip()

In a nutshell, everthing is drawn first offscreen, so we have to use this command to make it appear on the screen.

A Loop?:
Yep. Next we are going to create a program loop. What this does it repeat a section of code over and over until something makes it stop. In this case it's so our program won't end as soon as the text appears. Without this we would never see the text because the program would end too fast. Here's the code...I think you know where to put it by now.

while true do
screen.waitVblankStart()
end

We will get into loops later. For now just know it has to be there!
Save your file and double-click your script.cmd file to run your program.
If all went well you should see your text in red on the screen.

Getting Up and Running | Variables

Please welcome daryy, our newest member.

Who's Online: 3 Guests, 2 Users
PL3X, daryy

Total Members: 556
Total Posts: 13060
Total Topics: 1480
Total Categories: 8
Total Boards: 35

Recent Posts:

Re: Lua 5.1.4 help. by daryy
Re: PGELua Help - need term for a special rotation fix (mathematical issue) by yaustar
Re: Lua Loader? by Buddy4point0
Re: Lua Loader? by osgeld
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl


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