Announcement

Collapse
No announcement yet.

Spawn particle system in Destroyed()

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Spawn particle system in Destroyed()

    I've been spawning explosion particle systems when something is destroyed in the Destroyed() method. It finally dawned on me that this is why my explosions randomly disappear before finishing. If the owning actor is gone, the particle system can be cleaned up by garbage collection at any time.

    So, what's the right way to make an actor stay around until its explosion is finished? I can just make the actor invisible while the explosion is happening but I don't see any method in UParticleSystem that would return the duration of the particle system.
    Level 17 The Ladon Device

    #2
    You should just be able to use 'SpawnEmitterAtLocation' (located in UGameplayStatics), which will create an emitter that won't be destroyed until it's finished playing out. Give it the location of the object you're destroying and you're away.

    The Emitter is effectively 'Owned' by the World, so therefore won't be destroyed by GC.

    Code:
    UGameplayStatics::SpawnEmitterAtLocation()
    If you aren't aware of the GameplayStatics library, you're programming life just got a lot easier

    Comment


      #3
      Strange, that's exactly how I do it. At random times, I'll have a screen full of explosions and they'll all just disappear. Could this be something other than GC, then?
      Level 17 The Ladon Device

      Comment


        #4
        Pat, it seems that we are on a parallel path through UE4! I have the exact same problem. I spawn a particle emitter when an object is destroyed, that only shows up 50% of the time. I put in a brief delay which does seem to help:

        Code:
                if (DestructDelay < 0) // all over red rover
        	{
        		if (DestructionParticle == NULL)
        		{
        			DestructionParticle = UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), DestructionParticleTemplate, GetActorLocation());
        			DestructionParticle->SetWorldScale3D(FVector(1.0, 1.0, 1.0)*3.0);
        			GetStaticMeshComponent()->SetVisibility(false, true); // hide it
        		}
        	}
        	if (DestructDelay < -0.2) // put in a brief delay otherwise the particle effect doesn't fire sometimes?
        	{
        		Destroy();
        	}
        I've thought maybe it was something to do with being too far from the camera - however I increased the bounds on the particle emitter to (literally) 1,000,000.0 which didn't help at all. I'm still not sure what is causing the issue - I can't see how the Spawned particle emitter is tied to the actor being destroyed in any way.

        Comment


          #5
          Hmm, in ShooterGame - rather than calling Destroy(), they just set the lifespan of the object to around 2 seconds, making it invisible and collision-less.

          Perhaps that's the way to do it!

          Comment


            #6
            While I don't have much experience spawning emitters in C++, spawning emitters in blueprint has an "Auto Destroy" boolean option that cleans the emitter up automatically after the effect 'finishes'. I'm not sure what the exact conditions are for a particle system being finished, but perhaps its useful to look into that. Maybe your particle system is incorrectly thinking it is finished?

            Also, have you considered overriding AActor::EndPlay(EndPlayReason) instead of Destroyed? https://docs.unrealengine.com/latest...lay/index.html
            Perhaps you have more luck with that.
            Journeyman's Minimap - Available on Marketplace - Forum topic - Video

            Comment

            Working...
            X