Forum    News    Downloads    Saved Games


[Tutorial] Quick breakdown of Lua coding [COMPLETE}

<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 9:09 pm

[Tutorial] Quick breakdown of Lua coding [COMPLETE}

[Tutorial] Quick breakdown of Lua coding

Hi folks! I'm going to give you a quick run through of the lua coding language :D

Lua is a relatively easy coding language, that uses text editable 'Lua scripts'.

There are only a two things you need:

First up is displaying text.

The first thing we do is set up a color!
To set up a color, you use the code:
  Code:
color.new(255, 255, 255)

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
The most common way of selecting a color is through a Varible.
  Code:
Black = Color.new(0, 0, 0)

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!!!
  Code:
White = Color.new(255, 255, 255)

screen:clear()
screen:print(120, 120, "Hello World!!!!!", White)
screen.flip()
screen.waitVblankStart(300)

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.
  Code:
Image = Image.load("Image name.png")


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!
  Code:
screen:blit(120, 120, 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:
  Code:
Image = Image.load("Image.png")

screen:blit(120, 120, Image)
screen.waitVblankStart(300)
screen.flip()


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. :D


COMPLETE
Last edited by roxfox64 on Mon Aug 04, 2008 6:05 pm, edited 15 times in total.
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 9:12 pm

Quick question here.
What's the difference between Lua PLayer for windows and Lua player for PSP version 1.50?

P.S. Thanks so very much for taking your time to do this =D
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 9:19 pm

Umm... The only real difference I know of is that one is for PSP, and the other Windows :?

The one for windows might not understand this type of lua though.

I've always thought the PSP lua to be a port of Turtle Lua, but thats irrelevant :P
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 9:24 pm

Ohh, okay thanks.
I'm gonna put the Lua code program into my PSP now.
By the way...with Lua code, could you make homebrew stuff with it?
=D
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 9:26 pm

You can make Lua apps. But, to make full homebrew you need to use C++, and other coding languages.
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 9:34 pm

I think I'll take this one slow...
Let me learn about Lua first.
Then I'll try and find out about C++ and that other good stuff ^.^
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 9:41 pm

LOL fine by me :lol:
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 9:48 pm

I tried following what you said...
But I failed.
It didn't work =\
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 9:50 pm

I know, I forgot one thing :P

#1 I had it as Screen.flip() that 'S' in Screen can NOT be capitolized.

#2 I forgot to add the bit to clear the screen before running the code.
Last edited by roxfox64 on Mon Jun 23, 2008 9:52 pm, edited 1 time in total.
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 9:51 pm

Ohh, hehe ok.
由美子
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 10:28 pm

Still nothin'.
It just gives me this...
"Error: index.lua:5: '<eof>' expected near 'end'

Press Start to restart"
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 10:33 pm

Yes, I took a look at what I typed above and saw a couple errors on my part:

#1 There is an 'end' with nothing that needs closing

#2 color needed to be capitolized( "Color.new( 255, 255, 255)" )

#3 Black text won't show on a black background :oops:

#4 didn't add the line to clear the screen.

All of those are fixed now :D
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 10:38 pm

Same thing...here's a picture.
click on image to enlarge.
Image
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Mon Jun 23, 2008 10:39 pm

I just looked up and noticed I left that unnecessary end in there :oops:
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Mon Jun 23, 2008 10:41 pm

Hehe, ok than.
I'm checking it out now.


EDIT: Noppe...nothing, here's another pic, different though.
Image
由美子
Next

Return to PSP Tutorials

Who is online

Users browsing this forum: No registered users and 12 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for blacklist.org.