Hello World Part 1

From PSP Developer wiki
Jump to navigation Jump to search

Lesson 01
Hello World
The Creation of a Lua App for the PSP: A Walk-through.
This tutorial series focusses on writing programs for the PlayStation Portable (PSP) in Lua. Lua is a general purpose scripting language allowing for rapid development and easy distribution. It is a cross-platform language (meaning that you can run your scripts with minor modifications on PC, PSP, Linux, and any other system that has a Lua interpreter). It is a bit different than C in that it is a scripting language. This means that you will not compile your program as you would if you were writing a C Program. You, rather, just have a .lua file which really is just a text file with a different extension. All of the processing goes on through the Lua interpreter (which has been ported to the PSP by Shine).

If, at any time in this tutorial series, you need help please head over to our forums and post a question. We are always happy to help a fellow programmer.

So, before you start, you will need to download the latest version of the Lua Player from the official website here (unless you are using the GTA EBOOT Loader, then you will need to get Lua Player 0.14 from the Lua Player archive).

If you have a 1.50 Firmware PSP, you will need to take the two folders, 'luaplayer' and 'luaplayer%' in the PSP/GAME/ directory on your PSP's memory stick. With newer firmwares, you will need to use Fanjita's Eboot Loader. If you have a version 1.00 PSP, I trust you know how to install homebrew (if not, do a quick Google search and I'm sure you'll find what you're after).

Now create a blank text file (you can do this in your favorite text editor, whether it be Notepad, WordPad, or whatever else tickles your fancy) in the 'luaplayer' folder. Rename it to "script.lua" (from "whatever.txt").

You are now all set up, so on to the tutorial!

This is definitely a very easy program to create. It is only about 4 lines of code. They are very easy to understand. And no compiling is necessary!

This program will just print one line of text that says "Hello World." It is the classic introductory program ("Hello World" programs have been written in perhaps every language in existence). The point is not to create a functional program per se, but rather to introduce you to the syntax of the language.

So, start your code in "script.lua" out with this line: blue = Color.new(0, 0, 255) What this does is define a colour. The particular color that we have defined, this time, is blue. It creates a variable and stores the information that will tell the Lua Player what "blue" is. And the Lua Player will then interpret this line and allocate the appropriate system resources on the PSP to store it. But, the beauty of Lua is that we don't have to worry about that. The Player will handle all of the details.

Let's examine the line of code more closely to see exactly how it is instantiating (which is just a long word for creating) this color object.

"blue = Color.new," is telling the Lua interpreter "Hey, doofus, make a new color and call it blue." So now when we use the abstract identifier "blue," we are referencing the tangible information coding for the color blue in the PSP's memory.

"(0, 0, 255)" is the most important part of the code for us. This is how we actually tell the player what color we want our variable, "blue," to be. When we typed blue = Color.new, we could have typed, 'red', 'yellow', 'green' or even 'Cublecursome.' But if we had used this same "(0, 0, 255)," it would have still come out as blue. The interpreter has no idea what "blue" means, all it knows is what we tell it. We simply used "blue" as the identifier for simplicity. Obviously, if we had named it "white," and it was actually storing the information for the color blue, it would have gotten confusing down the road in our program (and much more so if our program was going to be more complex).

So the three numbers, in this case "0" "0" and "255", tell the PSP what the color displayed will be. These numbers are part of the RGB color mode. The first number stands for red, the second for green, and the third for blue.

Continue this tutorial with part two.