Sony Playstation Portable Lua Programming: Timers
In this tutorial, we will learn how to use millisecond timers in our programs. Timers are very useful for many things in your programs. Timers can make certain code execute only if a correct frame of time has passed.

Learning Timers
This small PSP program below will teach you to use timers and will display a message to the screen. The message dispalyed will depend on the time of the timer.

First, let's set up a white color object.

white = Color.new(255,255,255)

In order to create a timer we use the command Timer.new().
We store this timer into a variable. Let's create a timer below.

counter = Timer.new()

In order to start or stop a timer you use the commands timername:start() and timername:stop()
We want to start our timer at the beginning of this program so add this code:

counter:start()

Notice we used the name of our counter in the command.
Next, we want to create the main loop of our program, and add the code to clear the screen each loop. Add this next:

while true do
screen:clear()

Now, in our program we are going to need to keep up with the timers count. We will use the timers current time to display messages to the screen.
We can get the current time of the counter by using the command countername:time()
Add this code next:

currentTime = counter:time()
This will store the current time of the counter in to the variable currentTime.

Next, we will put some code to print the current time of the timer to the screen each loop. Put this code in:

screen:print(10,10,"Counter Time: " .. currentTime,white)
Now, we're going to use a few if statements to check whether our timer is at a certain time, if so then display a message. Let's put in our first one:

if currentTime < 1000 then
screen:print(100,100,"Less than 1000",white)
end

This code will check to see if the timer's time is less than 1000. If so, then
"Less Than 1000" will be printed to the screen.

Now, let's add another below this.

if currentTime > 1000 and currentTime < 2000 then
screen:print(100,100,"Greater than 1000",white)
end

This will check if the timer's time is greater than 1000, but less than 2000. If so, then "Greater than 1000" will be printed.

Our final if statement will reset our timer back to zero if the time goes over 2000.
We can reset the timer with the timername:reset(number) command.
The number inside the parentheses is what the timer will be set back to. Resetting the timer will also stop the timer, therefore we have to restart it as well. See the next code and add it in:

if currentTime > 2000 then
counter:reset(0)
counter:start()
end

With this code, if our timer goes higher than 2000, our timer will reset back to zero and start again.

Finally, let's wrap up our main loop.

screen.waitVblankStart()
screen.flip() end

Save and run your program to see a timer in action.

Music and Sound | Collision

Please welcome daryy, our newest member.

Who's Online: 3 Guests, 0 Users

Total Members: 556
Total Posts: 13060
Total Topics: 1480
Total Categories: 8
Total Boards: 35

Recent Posts:

Re: Lua 5.1.4 help. by daryy
Re: PGELua Help - need term for a special rotation fix (mathematical issue) by yaustar
Re: Lua Loader? by Buddy4point0
Re: Lua Loader? by osgeld
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl
Re: Lua Loader? by acer5050
Re: Lua Loader? by bumuckl

Copyright © 2006-2007 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie