I am in the process of creating libraries for most of the PGE functions that you usually need more than one line for to carry out a task that you may require, to use these libraries the usual method is to simply require them at the top of your code.
I learned how to generate some of this code thanks to other developers on this site so credit where credit is due for them. I can't remember exactly what code came from which users so I will just thank anyone that believes any of their code has been used as a basis.
The code snippets will follow in the following format:
file_name.lua
GeSHi (lua):
-- Code of the file, to use the functions or include the functionality place
-- require "file_name" at the top of the code (excluding the file extension).
Created by GeSHI 1.0.7.20
Description:
A short description if given.
script.lua
GeSHi (lua):
require "protected_launcher"
Created by GeSHI 1.0.7.20
Description:
A simple script file that is loaded by the PGE EBOOT simply used for launching the protected loader script.
short_names.lua
GeSHi (lua):
for key, value in pairs(pge) do
_G[key] = value
end
Created by GeSHI 1.0.7.20
Description:
Allows you to use short names for the built in PGE functions, for example instead of typing pge.delay(1000000) you can simply use delay(1000000) to complete the same task.
message_dialogue.lua
GeSHi (lua):
require "short_names"
function messageDialogue(message, options)
if utils.msginit(message, options or 0) then
local status
while running() do
gfx.startdrawing()
gfx.clearscreen()
gfx.enddrawing()
status = utils.msgupdate()
if status ~= PGE_UTILS_DIALOG_RUNNING then
break
end
gfx.swapbuffers()
end
return status
end
end
Created by GeSHI 1.0.7.20
Description:
Allows you to use a one line function to display the message dialogue.
protected_launcher.lua
GeSHi (lua):
require "short_names"
require "message_dialogue"
local function errorReporter(errorMessage)
local fileNameEnd = string.find(errorMessage, ".", 1, true)
local fileName = string.sub(errorMessage, 0, fileNameEnd - 1)
local errorLineStart = string.find(errorMessage, ":")
local errorLineEnd = string.find(errorMessage, ":", errorLineStart + 1)
local errorLine = string.sub(errorMessage, errorLineStart + 1, errorLineEnd - 1)
local formattedErrorMessage = string.sub(errorMessage, errorLineEnd + 1, string.len(errorMessage))
messageDialogue("An error has occoured." .. "\r\n" ..
"\r\n" ..
"Please report the following information:" .. "\r\n"..
"File: " .. fileName .. "\r\n" ..
"Line: " .. errorLine .. "\r\n" ..
"Cause: " .. formattedErrorMessage .. "\r\n" ..
"\r\n" ..
"Press Circle to restart."
)
end
local environment = {}
setmetatable(environment, { __index = _G })
while running() do
local script, scriptMessage = loadfile("main.lua")
if script then
setfenv(script, environment)
if xpcall(script, errorReporter) then
break
end
for key, value in pairs(environment) do
environment[key] = nil
end
for key, value in pairs(package.loaded) do
package.loaded[key] = nil
end
collectgarbage('collect')
else
errorReporter(scriptMessage)
end
end
Created by GeSHI 1.0.7.20
Description:
A protected launching environment for your script, if you use this then you will require no need to make your global variables local any more. You will still need to define local variables though. The file launches your script which you should call main.lua but you are able to change this by editing the library.
print_to_file.lua
GeSHi (lua):
function print(...)
local f = file.open("print output.txt", PGE_FILE_CREATE + PGE_FILE_RDWR + PGE_FILE_APPEND)
for key, value in ipairs(arg) do
f:write(value .. "\r\n")
end
f:close()
end
Created by GeSHI 1.0.7.20
Description:
Overwrites the usual print function to write to a file instead named "print output.txt" which you can change, this is usefule if you need to test a script and can't make use of the print functions normal functionality.
states.lua
GeSHi (lua):
states = {}
function states.init()
states.groups = {}
states.group = 1
states.state = 1
end
function states.setGroup(groupID)
if type(groupID) == "number"then
states.group = groupID
else
for i = 1, #states.groups do
if states.groups[i].name == groupID then
states.group = i
end
end
end
end
function states.getGroup()
return states.group
end
function states.addGroup(groupName)
local groupTable = states.groups
table.insert(states.groups, { name = groupName, states = {} })
end
function states.setState(stateID)
local group = states.getGroup()
local stateTable = states.groups[group].states
if type(stateID) == "number" then
states.state = stateID
else
for i = 1, #stateTable do
if stateTable[i].name == stateID then
states.state = i
end
end
end
end
function states.getState()
return states.state
end
function states.addState(stateName, codeFile)
local group = states.getGroup()
local stateTable = states.groups[group].states
table.insert(stateTable, { name = stateName, code = loadfile(codeFile) })
end
function states.run()
local group = states.getGroup()
local stateTable = states.groups[group].states
for i = 1, #stateTable do
if states.getState() == i then
stateTable[i].code()
end
end
end
Created by GeSHI 1.0.7.20
Description:
Provides an easy to use state system to mnage different parts of your code, it includes state groups so that you can easily make states inside other states in a different group.
counter.lua
GeSHi (lua):
counter = { status = 0, count = 0 }
function counter.create()
local newCounter = {}
setmetatable(newCounter, counter)
counter.__index = counter
return newCounter
end
function counter:start()
self.status = 1
end
function counter:stop()
self.status = 0
self.count = 0
end
function counter:pause()
self.status = 2
end
function counter:set(value)
value = value or 0
self.count = value
end
function counter:get()
return self.count
end
function counter:update()
if self.status == 1 then
self.count = self.count + 1
end
end
Created by GeSHI 1.0.7.20
Description:
Provides some functions for managing a counter, a timer is different than a counter as a counter is based on loop cycles.
fps.lua
GeSHi (lua):
require "short_names"
require "counter"
fps = { frames = 0, counter = counter.create(), timer = timer.create() }
fps.counter:start()
function fps.calculate()
if fps.timer:totaltime() >= 1 then
fps.timer = timer.create()
fps.frames = fps.counter:get()
fps.counter:set()
else
fps.counter:update()
end
return fps.frames
end
Created by GeSHI 1.0.7.20
Description:
Calculates the number of frames that the PSP is displaying every second.
If you can suggest any improvements to these scripts then please feel free and if valid I will attempt to implement them, if you have an idea for a library that I could make then also please feel free to suggest it, if you feel that I have used your code or code that is very similar to yours and have a problem with that then please feel free to contact me via the private messaging service on these forums, any other manner will be ignored or not viewed.
INITIAL POST: Added: script.lua, short_names.lua, message_dialogue.lua and protected_launcher.lua
UPDATE 1: Added: print_to_file.lua
UPDATE 2: Added: states.lua and counter.lua
UPDATE 3: Modified: counter.lua
UPDATE 4: Added: fps.lua
UPDATE 5: Modified: protected_launcher.lua
UPDATE 6: Modified: print_to_file.lua
Thanks,
Craig.