Odd SpawnActor(?) problem

Hello!

I have stumbled upon something odd when experimenting with playercontrollers and possessing pawns. For some reason my setup for getting into and out of a vehicle suddenly, and seemingly completely randomly, stops working. After testing it seems that the SpawnActor node fails to spawn the pawn the player is supposed to possess after getting out of the vehicle. I am a beginner when it comes to Unreal Engine and Blueprints so any help or alternate solutions are greatly appreciated. Some results of my testing if they are of any use:

  1. The system works correctly for between 10 seconds and about 3 minutes in Play Mode.
  2. Player input has no effect on when the system stops working (Amount of inputs or their frequency does not matter).
  3. Actors moving, colliding or interacting in any way has no effect on when the system stops working (you can leave the Play Mode running after starting and at some point you cannot exit the vehicle).
  4. Entering vehicle works 100% of the time.
  5. When failing to exit the vehicle, the print string “Exiting Submarine” at the end still gets executed.
  6. When failing to exit the vehicle, no actor can be seen spawning.

Sorry, I have absolutely no idea what information should be included or is relevant, but hopefully the basic idea is at least somewhat clear.

Have you tried using breakpoints to debug your code? Click the spawn actor node and press F9, a red dot should appear, when that code is executed the editor should open that breakpoint and you should be able to see which parameters are passed.

You could also try to pass the character class directly (instead of ActiveCharacter->GetClass), just to check if it’s invalid for any reason

1 Like

The unexpected behavior is probably due to using Character Object Reference and getting class from it. After you destroy the actor, at some point the garbage collector mightve cleared the variable. If Active Character is modified by something else, that could also explain why this is happening. Assuming from your variable name, that is potentially being changed. There are 2 options there.

  1. Instead of destroying the character, hide it and place it somewhere in level where its not reachable. This can be useful to avoid spawning which can be cost heavy.
  2. If you are destroying/Spawning character, save class instead of object (Refer to C++ and OOP resources to understanding Classes and Objects Better)
2 Likes

I decided to implement a different approach as suggested by zer0chi. I didn’t manage to catch the culprit behind the random failures, but it might indeed be the garbage collector feature as again suggested by zer0chi, as everything else should be working correctly and the “active character” variable is not modified by anything else. The new solution should be better anyways, at least if executed correctly (so not like I had to do it lol).

Basically I just teleport the original pawn away from the play area and then bring it back. I did run to some problems defining the returning location, as for some reason that I do not understand the GetActorLocation returned the character pawn to coordinates that were consistent, yet incorrect. That is a whole different topic though so I applied some basic math to get it working correctly after some testing.

Again, I believe everything I had to do in the bottom left corner can be ignored, I had to add the math because I’ve most likely made some other mistake somewhere in the project. That means the vector variable and rotator variable are both unnecessary as well.

This solution works 100% of the time and circumvents the need to worry about the character pawn not spawning, so I consider this problem solved. Still, I would advise any beginner viewing this to consult a video tutorial or something if you need a similar system because quite frankly what I did here is convoluted and most likely almost entirely unnecessary.

Thank you for the help!

1 Like