Circular References to actors

Hi,

I’m converting from UE3, so I have a lot of assumptions that I guess I have to get rid of.
It seems like you need to manage cyclic dependencies in UE4 a lot more carefully.

I have created a weapon class that pawn holds.
So in my pawn I have:

UPROPERTY(BlueprintReadOnly,Category=Weapon)
class AWeapon* CurrentWeapon;

And in my Weapon I have:

UPROPERTY(BlueprintReadOnly,Category="Weapon|Pawn")
class ADrEvilPawn* AttachedTo;

With this, when I spawn SOME pawns. It crashes when I press stop in the editor with these and a few more errors:

[2014.12.23-17.05.28:140][766]LogReferenceChain: (root) (standalone) World /Game/Maps/StartLevel.StartLevel->CurrentLevel
[2014.12.23-17.05.32:752][766]LogReferenceChain:   Level /Game/Maps/StartLevel.StartLevel:PersistentLevel->UE4Editor-Engine.dll!ULevel::AddReferencedObjects() (0x000007feeb613650) + 0 bytes [c:\programmering\drevil\ue4_engine\engine\source\runtime\engine\private\level.cpp:280]
[2014.12.23-17.05.32:752][766]LogReferenceChain:     Spear_C /Game/Maps/StartLevel.StartLevel:PersistentLevel.Spear_C_2->AttachedTo
[2014.12.23-17.05.32:753][766]LogReferenceChain:       Skeleton_C /Game/Maps/UEDPIE_0_StartLevel.StartLevel:PersistentLevel.Skeleton_C_1->Outer
[2014.12.23-17.05.32:753][766]LogReferenceChain:         Level /Game/Maps/UEDPIE_0_StartLevel.StartLevel:PersistentLevel->OwningWorld
[2014.12.23-17.05.32:754][766]LogReferenceChain:           (target) World /Game/Maps/UEDPIE_0_StartLevel.StartLevel
[2014.12.23-17.05.32:754][766]LogReferenceChain:   
[2014.12.23-17.05.32:754][766]LogReferenceChain: (root) (standalone) World /Game/Maps/StartLevel.StartLevel->DefaultPhysicsVolume
[2014.12.23-17.05.32:755][766]LogReferenceChain:   DefaultPhysicsVolume /Game/Maps/StartLevel.StartLevel:PersistentLevel.DefaultPhysicsVolume_1->Outer
[2014.12.23-17.05.32:755][766]LogReferenceChain:     Level /Game/Maps/StartLevel.StartLevel:PersistentLevel->UE4Editor-Engine.dll!ULevel::AddReferencedObjects() (0x000007feeb613650) + 0 bytes [c:\programmering\drevil\ue4_engine\engine\source\runtime\engine\private\level.cpp:280]
[2014.12.23-17.05.32:755][766]LogReferenceChain:       Spear_C /Game/Maps/StartLevel.StartLevel:PersistentLevel.Spear_C_2->AttachedTo
[2014.12.23-17.05.32:755][766]LogReferenceChain:         Skeleton_C /Game/Maps/UEDPIE_0_StartLevel.StartLevel:PersistentLevel.Skeleton_C_1->Outer
[2014.12.23-17.05.32:756][766]LogReferenceChain:           Level /Game/Maps/UEDPIE_0_StartLevel.StartLevel:PersistentLevel->OwningWorld
[2014.12.23-17.05.32:756][766]LogReferenceChain:             (target) World /Game/Maps/UEDPIE_0_StartLevel.StartLevel

However, there is no crash if I manually destroy the pawn before pressing stop.

My solution to the issue is to change the Weapons pointer to the pawn to a weak pointer:

UPROPERTY(BlueprintReadOnly,Category="Weapon|Pawn")
TWeakObjectPtr AttachedTo;

However, variables like Instigator isn’t a weak pointer. So it seems like it’s a real minefield to reference Actors with anything else than a weak pointer. (This makes me think all references in UE3 was weak pointers?)

I don’t understand why I crashes for only some Actors, but I can understand the crash if that’s the reason. Is this how it is intended that you should manage circular references?

Also, is all the blueprint variables weak pointers or can they cause a crash too if I’m referencing Actors?

Cheers,

I’m answering my own question. The reason I saw this crash on some actors and not all actors was a small typo in my BeginPlay. When I spawned my weapons I had typed:

AWeapon* Weapon = GWorld->SpawnActor<AWeapon>( DefaultWeapon, Params );

instead of:

AWeapon* Weapon = GetWorld()->SpawnActor<AWeapon>( DefaultWeapon, Params );

This placed some weapons in another world, and hence I got a garbage collection error when I pressed Stop as I assume all actors in that world didn’t get purged.

This explains the very different behavior from UE3 that I wasn’t expecting.

I hope this helps anyone else who is about to lose a day of programming :wink:

Cheers,

I have found what world they spawned in, the editor world. I found a bunch of spears in 0,0,0 that was rotated away from the camera, so I didn’t see them until I switched to perspective mode.