The Basics Part 1
Lesson 02
The Basics
Lua Programming Techniques
After having read Lua Lesson 01, you should have a basic concept of how to run Lua programs and how to setup a simple script. You are now ready to move on to some more advanced concepts that will prepare you to create fully featured applications and games.
In this tutorial, you will learn to: use if statements, display a background, load and display images, get button input, take screenshots, and make a loading screen, along with a bunch of other neat concepts.
So, basically this tutorial could be called "how to make a souped up, useless, graphical Lua script." But, alas, it's not. It's called "The Basics," because that's what this souped up, useless, graphical Lua script will attempt to teach you.
If you haven't read the first tutorial, I highly recommend it. If you decide to go on anyway, without reading it, and become baffled, and in the process having your mind transformed in to a gooey puddle of radioactive mush, don't come complaining to me. (Aside: actually it's not really that difficult, I just thought I'd add a little drama to the tutorial).
Before you start, you will need to download a zip file containing the images used in this tutorial. (The preceding phrase was a link, for those of you who skimmed right over it).
Now that we have all that introductory BS (baloney sandwich) out of the way, on to the tutorial!
For firmware 1.50 users, there's a nifty little trick that you can use (sorry 2.00+ people, it requires kernel mode which isn't yet available to you) to speed up your development cycle. By adding the following line into the top of your code, you can activate USB Disk Mode, which allows you to edit your script without having to exit out of it and go back through the menu. To enable this, create a new lua file and use this as the first line:
System.usbDiskModeActivate()
Now the USB port on the PSP will be activated when you run your script.
Next we are going to define our colors like we did in the first tutorial. We will be using pink, blue, orange, gray, and black. Logically, we will name our variables accordingly. So, to instantiate these color objects, we will enter the following lines of code:
pink = Color.new(255, 0 , 153)
blue = Color.new(0 , 153, 255)
orange = Color.new(255, 102, 0)
gray = Color.new(153, 153, 153)
black = Color.new(0 , 0 , 0)
If you have forgotten how these lines work, go back to the last lesson and refresh your memory.
Now we want to display a loading screen. Our loading screen will say "Loading: 0%" then "Loading: 1%" and so on and so forth until "Loading: 100%." To start it off, we need to print "Loading: 0%" to the screen. You should remember the screen:print function from Lua Lesson 01. But in case you don't, here's the code that you will need to insert:
screen:print(194, 136, "Loading: 0%", pink)
screen.flip()
As you should recall, "screen:print()" will store our text in an off-screen` buffer, and then "screen.flip()" draws that buffer to the screen.
Next, we need to load our images (which, if you missed it before, can be downloaded here). Extract the zip file into the folder where your script file is on your PSP. Then add this line. It will load the background. background = Image.load("images/background.jpg") Before we move on, let's get a firm understanding of what this line of code actually does. The first thing we see is a variable name, background. We then set that variable equal to the result of the function on the right ("Image.load()") using the assignment operator, or equal sign. The actual loading of the image comes through the "Image.load()" function which is built into Lua. It takes one parameter, the path to the image file we want to load. Our image is in the "images" folder, and the picture we want to load is "background.jpg." Remember, you must always use the file's extension when loading it (in this case, it was a jpeg file, so we used the extension ".jpg").Insert non-formatted text here
When you understand, move on to part two of this tutorial to continue your program.