Newbie questions (re: particles, physics, audio)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Newbie questions (re: particles, physics, audio)

Post by Mud »

Hey guys. I discovered Löve last night and started making my first ever videogame, with my 10 year old as designer (that's him shooting lasers from his eyes). I'm sure I'll have a lot of questions in the days to come. Thanks in advance, and thanks for making Love. :ehem:

RE: Particle System

I love the particle systems and want to use them everywhere, but I noticed they don't have a destroy method, like Body and Shape. Are they garbage collected? If I just keep calling newParticleSystem will I go OOM?

I was paranoid about that and wrote some code to cache particle systems and reuse them when "not ps:isActive() and ps:isEmpty()". This works great unless I call setSprite on one of the recycled systems, at which point Love crashes. If it turns out I do need to recycle these, does anyone know why setSprite would cause a crash?

RE: Audio

If I try to play my 1 second laser sound 3 times in a second, only the first call actually plays. It seems if I want N instances of a sound playing simultaneously, I need N instances of that audio source (even though it's the same file). Is that correct?

RE: Physics

I made a bunch of boxes and drop them into a contained space, and they behave pretty much like you would expect. But if I change newRectangleShape to newCircleShape, the bodies fall straight through my floor and/or get stuck in it. Am I doing it wrong?
Last edited by Mud on Fri Nov 05, 2010 8:58 pm, edited 1 time in total.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Newbie questions (re: particles, physics, audio)

Post by zac352 »

Wow.. I've actually never used any of those libraries thus far.
When I tried using physics, it crashed love. When I tried particles, I got errors that made no sense (even after copying and pasting directly from the demo :roll: ). I'm not even going to attempt audio until the time comes. :P
Hello, I am not dead.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Newbie questions (re: particles, physics, audio)

Post by bartbes »

That was epicly funny.
Mud wrote:I love the particle systems and want to use them everywhere, but I noticed they don't have a destroy method, like Body and Shape. Are they garbage collected?
Everything is, bodies and shapes as well, the destroy methods are there because you may very well want the objects to be gone right at that instant, instead of existing until the next garbage collection cycle.
Mud wrote:f I try to play my 1 second laser sound 3 times in a second, only the first call actually plays. It seems if I want N instances of a sound playing simultaneously, I need N instances of that audio source (even though it's the same file). Is that correct?
It is, I've got a simple sound manager here.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Newbie questions (re: particles, physics, audio)

Post by Mud »

zac352 wrote:Wow.. I've actually never used any of those libraries thus far.
That's kinda funny. I was drawn to the things that would look the coolest with the least effort from me. :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Newbie questions (re: particles, physics, audio)

Post by Robin »

Mud wrote:RE: Particle System
As I have never used those, I'll skip right to:
Mud wrote:RE: Audio

If I try to play my 1 second laser sound 3 times in a second, only the first call actually plays. It seems if I want N instances of a sound playing simultaneously, I need N instances of that audio source (even though it's the same file). Is that correct?
Yes: these are Sources, which is like a physical source of sound. If you want more than one "copy" of the sound, you use more sources. ;)
Mud wrote:RE: Physics

I made a bunch of boxes and drop them into a contained space, and they behave pretty much like you would expect. But if I change newRectangleShape to newCircleShape, the bodies fall straight through my floor and/or get stuck in it. Am I doing it wrong?
Hm, odd. Hard to say if you're doing it wrong. If you supplied the .love in which it goes wrong, we might be able to help you further.

EDIT: really? Ninja'd thrice? :(
Help us help you: attach a .love.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Newbie questions (re: particles, physics, audio)

Post by zac352 »

Your youtube video is haunted. First, it somehow crashed FF. Then it gave me an error message. Now it says 401 unauthorised. Now it crashed again.
Hello, I am not dead.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Newbie questions (re: particles, physics, audio)

Post by Mud »

Robin wrote:If you supplied the .love in which it goes wrong, we might be able to help you further.
OK, I'll try to strip it down to the small code that reproduces the problem.
bartbes wrote:Everything is, bodies and shapes as well
Cool. So I just need to keep a reference around, to prevent something from being collected while in use, right?

So I'll just create particle systems willy nilly, keep a reference to them, then remove the reference when "not ps:isActive() and ps:isEmpty()".
bartbes wrote:I've got a simple sound manager here.
Excellent. One thing I noticed in your code:

Code: Select all

--check which sounds in the queue have finished, and remove them
local removelist = {}
for i, v in ipairs(self.queue) do
   if v:isStopped() then
      table.insert(removelist, i)
   end
end
--we can't remove them in the loop, so use another loop
for i, v in ipairs(removelist) do
   table.remove(self.queue, v-i+1)
end
You can remove items during traversal if you using a numerical for loop and go backwards:

Code: Select all

for #self.queue,1,-1 do
   if self.queue[i]:isStopped() then
      table.remove(self.queue, i)
   end
end
Anyway, I tried stripping things down to the bare minimum:

Code: Select all

local raw = {}
function sound_load(name, filename)
   raw[name] = love.sound.newSoundData(filename)
end
function sound_play(name)
   love.audio.play(love.audio.newSource(raw[name], 'static'))
end
But I was getting random crashes. I'm assuming that's because (1) my sound sources were being garbage collected while in use and (2) that's what your 'queue' table was for. Sound about right?

With those assumptions in mind, my KISS sound code is currently:

Code: Select all

local rawdata = {}
local sources = {} -- reference sources so they aren't collected while in use

function sound_load(name, filename)
   rawdata[name] = love.sound.newSoundData(filename)
end

function sound_play(name)
   local source = love.audio.newSource(rawdata[name], 'static')
   sources[#sources + 1] = source
	love.audio.play(source)
end

function sound_update()
   for i=#sources,1,-1 do
		if sources[i]:isStopped() then
			table.remove(sources, i)
		end
   end
end
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Newbie questions (re: particles, physics, audio)

Post by thelinx »

Mud wrote:

Code: Select all

function sound_play(name)
   local source = love.audio.newSource(rawdata[name], 'static')
   sources[#sources + 1] = source
	love.audio.play(source)
end
try

Code: Select all

function sound_play(name)
  sources[#sources + 1] = love.audio.newSource(rawdata[name], 'static')
  love.audio.play(sources[#sources])
end
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Newbie questions (re: particles, physics, audio)

Post by Mud »

thelinx wrote:try

Code: Select all

function sound_play(name)
  sources[#sources + 1] = love.audio.newSource(rawdata[name], 'static')
  love.audio.play(sources[#sources])
end
Thanks for the tip, but I tend to prefer simpler lines over fewer lines. In this case, the extra line eliminates a redundant table lookup and call to the length operator.
User avatar
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

Re: Newbie questions (re: particles, physics, audio)

Post by ninwa »

So glad I happened to stumble on this, I was having problems with my audio earlier today and this fixed it up. The only problem I have with the sound manager is that it's absolutely terrible for large files (because it loads the whole file into memory and decodes it.) Using this for my background music mp3 was a no-go, which was fine because it was one of the few things I wasn't having a problem with. That said, using this for small sound-effects is perfect.

Also, welcome to the community, Mud!

Ninwa
Post Reply

Who is online

Users browsing this forum: No registered users and 92 guests