• Gwilty's Programming Assignments #2
    191 replies, posted
"[URL="http://en.wikipedia.org/wiki/Bob_Ross"]Happy Accidents[/URL]"
[QUOTE=garry;33062131]"[URL="http://en.wikipedia.org/wiki/Bob_Ross"]Happy Accidents[/URL]"[/QUOTE] My mother said the same thing. :downs:
I like seeing all the creative patterns people are programming here. Not to analyze or anything, just to enjoy watching.
Finally managed to get a class based system working for particles, think and draw functions are still a little iffy though. What's the best way to remove values from a table and then shift all the other values down, for example if I had a table of { a, b, c, d } and then wanted to remove b, how ould I easily make the tabe just {a, c, d }?
[QUOTE=Storm_Ninja;33063636]Finally managed to get a class based system working for particles, think and draw functions are still a little iffy though. What's the best way to remove values from a table and then shift all the other values down, for example if I had a table of { a, b, c, d } and then wanted to remove b, how ould I easily make the tabe just {a, c, d }?[/QUOTE] Take a look at table.remove here: [url]http://lua-users.org/wiki/TableLibraryTutorial[/url] Just note that table key/value pairs are not always in the right order in the table and remove takes a position not a key, so you may want to call table.sort beforehand. [QUOTE=subenji99;33057510]table.remove takes a table and the position in the table the entry to remove is, which as I'm sure you guessed, isn't necessarily the same as the key assigned to the object you want to remove. Instead try: [lua]fireworks[i] = nil[/lua] Lua's garbage collector will do the rest.[/QUOTE]
I tried playing with fireworks[i] = nil; and it just ended up purging the entire table. Is there any benifit to actually clearing the values from the table as opposed to just leaving them and overwriting them when they're not needed?
I think that reusing the particles is more efficient than deleting one and making another. At least it sounds like it would be more efficient...
[QUOTE=Darkest_97;33064864]I think that reusing the particles is more efficient than deleting one and making another. At least it sounds like it would be more efficient...[/QUOTE] It makes so little difference, it doesn't matter [editline]31st October 2011[/editline] [QUOTE=Storm_Ninja;33064679]I tried playing with fireworks[i] = nil; and it just ended up purging the entire table.[/QUOTE] You're doing something wrong.
[media]http://www.youtube.com/watch?v=TzzGvdpGXr4[/media] My XNA version is practically done. Can't think of anything else to add. [editline]Edited:[/editline] Nevermind, just thought of a few things.
Here's what I got done today, still more to do later at some point. [img]http://puu.sh/7Vom[/img]
Dem sparklers. I'm glad someone did this.
No time to work on this one either. I don't have a clue where I would start either. Where are you guys going about making particles?
Just to be sure I actually know what I'm doing. [lua]for k, v in pairs(particleList) do if (v.alive == false) then table.remove(particleList, k) table.insert(particleList, k, Particle.create(love.mouse.getX(), love.mouse.getY(), angle)) break end end[/lua] This removes the particle in spot 'k' of the particleList table, and then inserts a new particle at spot 'k'? It seems to be working how I want. Because the particle table only increases in size when there arent enough dead particles. Is this an okay way to do it? I tried setting the spot to nil but the size of the table never decreases. I also just tried removing all the dead particles at once but the table size wouldnt decrease. I feel like I'm missing something important.
[QUOTE=Darkest_97;33070106]This removes the particle in spot 'k' of the particleList table, and then inserts a new particle at spot 'k'? It seems to be working how I want. Because the particle table only increases in size when there arent enough dead particles. Is this an okay way to do it? I tried setting the spot to nil but the size of the table never decreases. I also just tried removing all the dead particles at once but the table size wouldnt decrease. I feel like I'm missing something important.[/QUOTE] [quote]table.remove(table [, pos]) Remove an element from a table. If a position is specified the element at that the position is removed. [b]The remaining elements are reindexed sequentially and the size of the table is updated to reflect the change.[/b][/quote] Setting a value to nil doesn't then re-index the table like table.remove does, but the size of the table would decrease automatically the next time lua's garbage collector ran and purged out the nil references. table.remove should only be used if you're using integer keys AND the relationship between key and value doesn't matter - e.g. you aren't trying to reference a specific value, as the table re-sort can shift it to a different integer key. If you need e.g. table[5] = mySpecific5thValue then you don't want the re-sorting to shift it, so you would stick to setting values to nil and let the lua GC clean up the table size on its own. If you want to experiment with this, you can trigger the lua GC to run early, refer to [url]http://www.lua.org/manual/5.1/manual.html#pdf-collectgarbage[/url] [lua]collectgarbage("collect")[/lua]
This doesnt have anything to do with the assingment, but its just a quick question about Love... What would be the best way to make saved game files?
[QUOTE=subenji99;33070699]Setting a value to nil doesn't then re-index the table like table.remove does, but the size of the table would decrease automatically the next time lua's garbage collector ran and purged out the nil references. table.remove should only be used if you're using integer keys AND the relationship between key and value doesn't matter - e.g. you aren't trying to reference a specific value, as the table re-sort can shift it to a different integer key. If you need e.g. table[5] = mySpecific5thValue then you don't want the re-sorting to shift it, so you would stick to setting values to nil and let the lua GC clean up the table size on its own. If you want to experiment with this, you can trigger the lua GC to run early, refer to [url]http://www.lua.org/manual/5.1/manual.html#pdf-collectgarbage[/url] [lua]collectgarbage("collect")[/lua][/QUOTE] Actually I found out why my table wasn't shrinking. I was only removing particles in mouse click, and at the same time I was creating particles. So I made a loop in update that sets dead particles to nil. Basically I'm stupid. Thanks!
[QUOTE=Darkwater124;33071526]This doesnt have anything to do with the assingment, but its just a quick question about Love... What would be the best way to make saved game files?[/QUOTE] I'm not sure about the [I]best[/I] way, but you could try using [url=http://www.lua.org/manual/5.1/]Lua's io library[/url] to output all values you want to save into a file. Then, when loading, input the values from the file in the same exact order that they were outputted (this is so you know what values relate to what variables). Use a new line to separate each individual value so that you can use the [url=http://www.lua.org/manual/5.1/manual.html#pdf-file:read]file:read(...)[/url] function to get each value individually into a variable.
I actually have time to work on this today. So, again, I have no clue where to start. Any recommendations?
[QUOTE=i300;33073698]I actually have time to work on this today. So, again, I have no clue where to start. Any recommendations?[/QUOTE] Use the link from the first page about classes. Use the first example to make a 'particle' class. Draw one particle. At least thats where I started.
[QUOTE=Darkwater124;33071526]This doesnt have anything to do with the assingment, but its just a quick question about Love... What would be the best way to make saved game files?[/QUOTE] If you're asking how to read and write them, use [url=http://love2d.org/wiki/love.filesystem]love.filesystem[/url]
Okay, thanks. Ill play with it a bit. Good luck to the rest for this assignment.
[QUOTE=Darkest_97;33073833]Use the link from the first page about classes. Use the first example to make a 'particle' class. Draw one particle. At least thats where I started.[/QUOTE] looking at the code you posted for that I don't get why you have particleList, Particle and Part, where they don't seem to even reference each other but somehow seem to make a load of particles appear
Had time to give this a try so here you go: [IMG]http://i41.tinypic.com/sw4kqv.gif[/IMG] It isn't that good but it works and I think it qualifies. [B]Edit:[/B] The slow-mo at the end (and actually quite generally) is because of shitty recording software...
[QUOTE=Zard;33074826]looking at the code you posted for that I don't get why you have particleList, Particle and Part, where they don't seem to even reference each other but somehow seem to make a load of particles appear[/QUOTE] In his code, Particle is the class - the template particles are based on, and Partcle.create is the constructor function for building one. Part is a particle object, and setmetatable(part,Particle) is what links them. (Look on page 2 for ja_cop's description of how that works.) ParticleList is just an array storing every particle created, for easy iteration through them all regardless of how many there are. Each particle object is placed into this array on creation - the table.insert line in the love.mousepressed callback.
[QUOTE=Zazibar;33062021][video=youtube;L8VDGMG91Ts]http://www.youtube.com/watch?v=L8VDGMG91Ts[/video] I love it when bugs create something awesome.[/QUOTE] Fun fact: The helicopter boss fight in Half-Life 2 at the end of Route Canal was actually a mistake by a developer. He accidentally put the wrong attack into the AI, which made it fire bombs rather than its machine gun. They've adapted it after that to turn it into the bull rush bomb fight we have today.
got the class thing working thanks to the first link but I can't get it to move which is so simple :v: I just don't get it.. [lua] fireworkList = {} firework = {} firework.__index = firework function firework.create(mouseX) local fire = {} setmetatable(fire, firework) fire.X = mouseX fire.Y = 500 return fire end function firework.update(firework) for i = 1, #firework do fire.Y = fire.Y - fire_vy end end [/lua]
[QUOTE=Zard;33077681]got the class thing working thanks to the first link but I can't get it to move which is so simple :v: I just don't get it.. [lua] fireworkList = {} firework = {} firework.__index = firework function firework.create(mouseX) local fire = {} setmetatable(fire, firework) fire.X = mouseX fire.Y = 500 return fire end function firework.update(firework) for i = 1, #firework do fire.Y = fire.Y - fire_vy end end [/lua][/QUOTE] Because your update routine is horribly set up. You're passing, what I assume, to be a number to .update(), which is pass from love.update(), which is almost never a full number, unless your frame delays are huge. You're using a for loop with the length of that number, which will never return anything, and in fact, should error.
so how should I go about making it move in that case?
I did this for movement. [lua] function Particle:move(dt) self.X = self.X + (self.speedX * dt) self.Y = self.Y + (self.speedY * dt) end[/lua] Particle:move is a function for the class. So in love.update() [lua]function love.update(dt) for i = 1, #particleList do particleList[i]:move(dt) end end[/lua]
I gave each of my particles the option to have a custom draw and think function, otherwise use a default one. I also gave my ParticleHandler class the gravity variable, which I guess is kind of weird... but it's cool because I can do this: [lua] temp = Particle(posit,(Vector(x,y)-posit):normalized()*700, function(self) if self.shot then return end love.graphics.setColor(self.color) love.graphics.circle("fill",self.position.x,self.position.y,20,math.random(3,4)) if self.position.y%4<3 then for a=0,3,1 do local partic = Particle(self.position,(Vector(math.random(-1,1)+(math.random()-math.random()),math.random()-math.random()))*50,function(self) love.graphics.setColor(self.color) love.graphics.circle("fill",self.position.x,self.position.y,1,20) end) partic.color = self.color particles:Add(partic) end end end, function(self) if self.position.y<=self.trigger.y and not self.shot or false then firework(self.trigger) self.shot = true end end ) temp.trigger = Vector(tonumber(i),tonumber(y)) temp.color = { math.random(0,255), math.random(0,255), math.random(0,255)} temp.timer = -1 particles:Add(temp) [/lua] That's the code for a firework (which actually is a particle :v:)
Sorry, you need to Log In to post a reply to this thread.