Accessing collision events in particlesystemcomponent causes pc to freeze

I am trying to track which actor a particle system has collided with using the below global function.

UFUNCTION(BlueprintCallable, category = "CollisionSystem", meta = (WorldContext = "ActorObject"))
	static void ParticleCollide(UParticleSystemComponent* ParticleCreated)
{
	if (!ParticleCreated) { return; }
	auto CollisionEvents = ParticleCreated->CollisionEvents;
	
	for (auto CollisionEvent : CollisionEvents)
	{
		if (CollisionEvent.HitResult) {
			auto Actor = CollisionEvent.HitResult->Actor;

			if (Actor.IsValid())
			{
				// do something with the actor found
			}
		}
	
}

However, when ue4 call this function, my pc freezes entirely and does not give me any opportunity to debug. Could someone point to me what went wrong in this function and also if there is a better way to obtain which actor collided with a particle system?

Hi Ianlerin,

How are you binding to this function? Also have you verified that the freeze is being caused by one of the lines of code you’ve provided and not by something inside of the actor validation check?

Below is how i bind and call the particle collide function. The reason why i believe it is the reason causing my pc to freeze is because everything works fine if i don’t connect the “Particle Collide” function node. The particle collide function is exactly the same as i have pasted above, i have not added anything additional to it.

Thank you.

Hi Ianlerin,

Have you tried stepping through ParticleCollide with the debugger attached to see what could be causing the freeze?

I could not because my pc freezes entirely upon calling. I tried attaching debugger to it but it still freezes my pc.

Thank you.

Hi Ianlerin,

If you make a build using DebugGame Editor, add a breakpoint to the beginning of ParticleCollide, and then launch with the debugger attached (by pressing F5 in Visual Studio), you still get the freeze before the breakpoint is hit?

Thanks to your suggestion i have managed to single out the line causing the freeze.

auto CollisionEvents = ParticleCreated->CollisionEvents;

My understanding now is that auto specifier does not work well with c++ array, thus it would not work well with TArray as well, but causing my entire pc to freeze completely might be something worth for Epic to look into.

However, back to my question, after taking out the above line and made some modifications there are still error remaining.The current function looks like

UFUNCTION(BlueprintCallable, category = “LoadSystem”, meta = (WorldContext = “ActorObject”))

	static void ParticleCollide(UParticleSystemComponent* ParticleCreated)
{
	UE_LOG(LogTemp, Warning, TEXT("ParticleCollideEvent"));

	for (auto CollisionEvent : ParticleCreated->CollisionEvents)
	{
		if (CollisionEvent.HitResult) {
			auto Actor = CollisionEvent.HitResult->GetActor();

			if (Actor)
			{
				// do something with the actor found
			}
		}

	}
}

When i reach line:

auto Actor = CollisionEvent.HitResult->GetActor();

Microsoft Visual Studio give me the following error message for the line above:

Exception thrown at 0x00007FFAC43D2D74 (UE4Editor-CoreUObject.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Hi Ianlerin,

auto has nothing to do with the array itself, it is simply a convenience feature of C++ to not have to write out the type every time. Your crash has nothing to do with using auto. Another thing to know about auto is that it does not preserve references, so for your loop it would be better to say const auto& CollisionEvent (which will also avoid unnecessary copies and prevent you from accidentally modifying the array).

How do you have access to an FHitResult on FParticleEventCollideData, though? Have you modified the engine’s source to include it? I’m not seeing it anywhere in the 4.17 or 4.20 source.

Hi, yeah i just realised i changed the engine source code awhile ago but i never rebuild it manually. When i revisited it i thought it was part of the original source code.

I rebuilt the engine and the function no longer crashes.

Onto to the auto part, i thought auto CollisionEvents = ParticleCreated->CollisionEvents would translate the auto into TArray< FParticleEventCollideData> which crashes my pc. It seems to me that you are referring to the auto specifier in for (auto CollisionEvent : ParticleCreated->CollisionEvents). Correct me if im wrong.

Thank you.

Hi Ianlerin,

Yes I was referring to the loop part, however it is the same situation with auto for the array. auto is doing its job there by simply preventing you from having to type out the full type name. Like I said though, auto does not preserve references so you declaring CollisionEvents like that is causing you to copy the entire array into a new variable.

Hi Ianlerin,

We haven’t heard from you in a while, so I’m going to marked this question as closed for tracking purposes.

For anyone who comes along and sees this post in the future: the problem was that the engine was modified but not re-compiled when a new, custom feature was used.