Sony Playstation Portable Lua Programming: Functions
In this tutorial we are going to learn how to use functions. Creating a function is kind of like creating your own new command for lua. You can use functions for repeating sections of code, and call the function for use at any time. Functions are placed outside of your main loop. A good place for them is at the top of your code around your variable declarations.
Using Functions
First, let's look at how a function is declared. Examine the code below:
function functionName()
(code to perform in function.
Multiple lines of code may be used.)
end
Now that you've seen how a function is created, let's look at an example function that will print a simple message to the screen each time it is called. Have a look!
function printMessage()
screen:print(100,100, "Functions are fun!")
end
Now, in order to print this message in a program we will have to make a call to the function, where ever we want to use it. For the above example using the following code will call the function and perform the code inside the function one time.
printMessage()
That's it!? Ummm.. yeah I'm afraid so! Using the code above will perform the code in the function we created earlier. "Functions are fun!" will print to the screen by calling printMessage().
As you start creating more complex programs or games you will want to use functions a lot. Using these functions calls inside your main loop instead of the actual code will extremely clean up the look of your code. Earlier I said functions are placed outside of your main loop, which is true. But, you are welcome to call them from within your main loop.
Functions may also be created to return a value. This is done by creating arguments in the parentheses of the function when creating it. Let's say for the fun of it we want to create a function that will add two numbers and return the result back to us. First we have to create the function. Look below:
function additUp(a, b)
sum = a + b
return sum
end
Notice this time inside the parentheses we placed the letters "a" and "b". These can be any letters or even words. They are variables to use inside this function, and they only exist inside the function. Since we are going to be adding two numbers we needed two variables to store them in. You may use more or less variables in your functions as long as you separate each with a comma.
Next inside the function we created a variable called "sum" and it will get assigned the value of the variables "a" and "b" added together. This will be clearer once you see how it is called.
Next, we have a command called return. This tells the function that we are returning a value from this function once we exit it. The value we are returning is the value of the variable "sum".
Finally, we end our function.
Now, let's examine how we could use this and explain a bit more. Look at this code:
screen:print(100,100,additUp(5,6), green)
This will print the result of our function to the screen. Notice we use actual numbers when calling the function. We used 5 and 6 for this example. This assigned the value 5 to "a" and the value 6 to "b". Now inside our function declaration that we made earlier, anytime "a" or "b" are used, those variables will be replaced by the numbers we have inserted. So in our function where is says "sum = a + b", it now resolves to "sum = 5 + 6". This will assign the value of 11 to the "sum" variable and then return it to our print command, printing it to the screen.
We could also store the information in a variable if needed. This way we wouldn't have to use it right away if we didn't need it at that moment, or could reuse it over and over without having to recalculate the whole function. Look below:
myTotal = additUp(5,6)
This will store the returned value of our function into the variable myTotal. myTotal will equal 11.
Let's look at the program we made in the last tutorial. It looked something like this:
grass = Image.load("grass.png")
player = Image.load("player.png")
flower = Image.load("flower.png")
screenwidth = 480 - player:width()
screenheight = 272 - player:width()
Player = { }
Player[1] = { x = 200, y = 50 }
while true do
pad = Controls.read()
screen:clear()
for a = 0, 14 do
for b = 0,8 do
screen:blit(32 * a, 32 * b, grass)
end
end
screen:blit(100,100,flower)
screen:blit(300,220,flower)
screen:blit(Player[1].x,Player[1].y,player)
if pad:left() and Player[1].x > 0 then
Player[1].x = Player[1].x - 2
end
if pad:right() and Player[1].x < screenwidth then
Player[1].x = Player[1].x + 2
end
if pad:up() and Player[1].y > 0 then
Player[1].y = Player[1].y - 2
end
if pad:down() and Player[1].y < screenheight then
Player[1].y = Player[1].y + 2
end
screen.waitVblankStart()
screen.flip()
end
We could make some functions to clean up our main loop, in order to see what goes on more clearly. We could put all of the directional checks inside a function. We could also put the screen blitting into a function. Neither of these will require a return value. Look at the code below to see how you could use functions in this:
grass = Image.load("grass.png")
player = Image.load("player.png")
flower = Image.load("flower.png")
screenwidth = 480 - player:width()
screenheight = 272 - player:width()
Player = { }
Player[1] = { x = 200, y = 50 }
-- Function to check player movements
function playerMovement()
pad = Controls.read()
if pad:left() and Player[1].x > 0 then
Player[1].x = Player[1].x - 2
end
if pad:right() and Player[1].x < screenwidth then
Player[1].x = Player[1].x + 2
end
if pad:up() and Player[1].y > 0 then
Player[1].y = Player[1].y - 2
end
if pad:down() and Player[1].y < screenheight then
Player[1].y = Player[1].y + 2
end
end
-- Function to paste images to screen
function pasteImages()
for a = 0, 14 do
for b = 0,8 do
screen:blit(32 * a, 32 * b, grass)
end
end
screen:blit(100,100,flower)
screen:blit(300,220,flower)
screen:blit(Player[1].x,Player[1].y,player)
end
-- Main Loop
while true do
screen:clear()
pasteImages()
playerMovement()
screen.waitVblankStart()
screen.flip()
end
By looking at this, you can see that our main loop has been cleaned up big time. All of the complicated detailed code is kept inside the function, and in our main loop we simply call it. You can also simply read the code straight down in your main loop and see what is going on. All of the cluttered mess within the loop from the previous code is gone, and moved to the top of our code.
I hope this has helped you learn a bit about using functions. Our later tutorials will use functions.
Images |
Reading and Writing Files
|