[Tutorial] Quick breakdown of Lua coding [COMPLETE}
Hi folks! I'm going to give you a quick run through of the lua coding language
Lua is a relatively easy coding language, that uses text editable 'Lua scripts'.
There are only a two things you need:
- --A text editor.
--A version of LuaPlayer
First up is displaying text.
The first thing we do is set up a color!
To set up a color, you use the code:
Those numbers let you do an RGB(Red Green Blue, in that order) color select.
A quick guide is
- Red = 255, 0, 0
Green = 0, 255, 0
Blue = 0, 0, 255
Black = 0, 0, 0
White = 255, 255, 255
That was an example of a varible.
In that code, 'Black' was the varible.
Color.new(0, 0, 0) is what that varible represents.
Now were going to actually make text appear!!!
Copy & Paste this into a text editor, any will do. Then save it as First_app.lua
Place it in any directory on your PSP, and run it from Lowser(The lua script browser that comes pre-packaged with LuaPlayer.)
Explanation:
screen:clear() - Clears what ever is currently on the screen
screen:print(X, Y, "TEXT", COLOR) - Loads text into the off screen buffer.
screen.flip() - Tells Lua to take what ever is loaded in the off screen buffer, and put it on screen.
screen.waitVblankStart() - Tells Lua to wait before doing anything else. There are 60 Vblanks in one second. In the example the specified number was 300. 300 Vblanks = 5 Seconds.
Next up is Using Images!
The first thing we do is define the image as a varible.
Image.load("") Simply tells Lua that you want it to remember an Image's path.
After you've defined the varible, you need to show the image!
The command screen:blit(X, Y, <Name of Varible>) shows the image that the varible points to. It shows the image in the off-screen buffer, so you need to add screen.flip() so it appears in screen.
Also, add in screen.waitVblantStart(300) or else you'll be left with an Image thats visible for only 1/60 of a second...
All together you've got:
Recognizing Input
For this example you will display text, and move it around the screen.
To define input we use the Controls.read() function.
- Code:
Pad = Controls.read()
X = 240
Y = 136
White = Color.new(255, 255, 255)
function Ctrl()
if Pad:left() then
X = X - 3
end
if Pad:right() then
X = X + 3
end
if Pad:up() then
Y = Y - 3
end
end
if Pad:down() then
Y = Y + 3
end
while true do
Ctrl()
screen:print(X, Y, "Enter Your Text Here", White)
screen.flip()
end
The While loop makes Lua continue executing the code until it becomes false. Since false isn't defined here you need to press and hold start to exit if you're running LuaPlayer on your PSP.
IF statements
IF statements were used in the last example, I'll explain them here.
If statements will be executed if their requirements are met.
In the statement you can use the operators AND, & OR.
If AND is used both arguements must be true, otherwise nothing happens.
If OR is used both arguements must be false for nothing to happen.
Here are examples:
AND example:
- Code:
White = Color.new(255,255,255)
Pad = Controls.read()
N = "You have pressed none, or only one of the required buttons."
Y = "You have pressed both of the required buttons"
Base = N
function Opt()
if Pad:left() and Pad:circle() then
Base = Y
else
Base = N
end
end
while true do
Opt()
screen:print(220, 130, Base, White)
screen:flip()
end
OR example:
- Code:
White = Color.new(255,255,255)
Pad = Controls.read()
Y = "You've pressed a trigger button!"
N = "You do not currently have a trigger button pressed."
Base = N
function Opt()
if Pad:ltrigger() or Pad:rtrigger() then
Base = Y
else
Base = N
end
end
while true do
Opt()
screen:print(220, 130, Base, White)
screen.flip()
end
Use your imagination, and play around with all of the Various Functions listed here.
COMPLETE