Page 2 of 2

Re: LOVEFEST III: Chance. Space shooter dev log

Posted: Sun Nov 04, 2012 11:03 pm
by spir
Nsmurf wrote:No screenshot, but I'm willing to post some code on my powerup system.

Basically it enumerates the 'powerup' directory, and stores all the files inside it in a table. It gets a random value from that table, and then runs it with love.filesystem.load().

This allows me to create as many powerups as I want, and have them automatically added to the game without any unnecessary code!

This is actually salvaged code from an old project, but still very useful!

Here is the code currently located in powerup.lua:

Code: Select all

function getpowerups(dir)
	powerups = love.filesystem.enumerate(dir)
	return powerups[math.random(#powerups)]
end

function runpowerup()
	powerupc = love.filesystem.load('powerup/'..getpowerups('powerup'))
	powerupc()
end
All you need to do is call runpowerup() and it will run a random powerup from the 'powerup' directory.

Here is an example powerup:

Code: Select all

player.speed = 200
I am considering creating a powerup lib after the lovefest is over. what do other people think about that?
Very nice hack!
Why don't you "enumerate" the powerup files once only at load? Actually, you could even load (compile) them all into Lua funcs, using love.filesystem.load, keep them into an array of funcs, and on power-up run one of them by chance, couldn't you?

Denis

Re: LOVEFEST III: Chance. Space shooter dev log

Posted: Mon Nov 05, 2012 11:56 am
by Robin
spir wrote:Why don't you "enumerate" the powerup files once only at load? Actually, you could even load (compile) them all into Lua funcs, using love.filesystem.load, keep them into an array of funcs, and on power-up run one of them by chance, couldn't you?
Exactly. Depending on the details, that might be enough to make the game run smoothly for someone with a bad computer who would have some occasional lag before.

Re: LOVEFEST III: Chance. Big Red Button

Posted: Tue Nov 06, 2012 12:24 am
by Nsmurf
spir wrote:Very nice hack!
Why don't you "enumerate" the powerup files once only at load? Actually, you could even load (compile) them all into Lua funcs, using love.filesystem.load, keep them into an array of funcs, and on power-up run one of them by chance, couldn't you?
Thanks for pointing that out! I probably will, although I might not have much time to work on code during the week :(
Robin wrote:that might be enough to make the game run smoothly for someone with a bad computer who would have some occasional lag before.
Considering that I'm playtesting (And coding) this on an ~10 year old PC, I don't think that that will be to big of a problem, but I still will :)