Switching Actors And UI, Pending Kill and Accessed None Error (Solved)
I am trying to find a good way to switch between characters without all these Errors. I have a parent class "PlayableCharacter" with the Component "AC_PCStats" that holds the characters variables like life, energy and ammo. and 2 child classes. One "ThirdPersonSimpleCharacter" the other "FirstPersonSimpleCharacter". I have tried to do this a few different ways and this is where I am currently stuck at.
In the Begin Play Event i first check if it it creating a new character or switching from one using the variable "IfNewPlayableActorSpawn" inside the game instance. If not then it creates The UI and adds it to view port.
On the UI Event Construct it calls the Function "SetMyCharacter" that sets up the Variable "MyCharacter"
MyCharacter is used in this case to get the Health of the character as its bound to a Progress bar.
Switch Character is currently in each child blueprint at the end of the blueprint I am trying to call the SetMyCharacter again in the UI. This Event seems to be where the problem is. When the Character switches, the Variables in "AC_PCStats" or the UI do not switch to the new Character.
Now when I switch the characters I get Access errors from trying to read the variables that are not there.
I am trying to transfer the Variables in the Component "AC_PCStats" between the switched characters, and get the UI to read the current characters stats. So when the Actors switch your health on the UI stays the same.
In game I want to press a button and switch between characters or vehicles in a list who might share the same UI and or Stats. I will also be using this during map transitions and the Save/Load process
In game example - Your Character walks in front of a bus. Now they are a ghost. You wonder around till you posses a Gorilla from the zoo. Now you have to escape so you steel a truck and drive off into the sunset.
The Character, Bus, Ghost, Gorilla, and Truck are all separate Actors that you can poses. When Switching from Character to Ghost then to a Gorilla the previous Actor is destroyed. When entering a vehicle the actor will be locked while in control of the vehicle.
You last screen (BP not errors) is not correct.
You can't spawn actor from class, after you Destroy Actor (reference). It will be always pending kill in best.
Move Destroy Actor after Spawn Actor from Class.
Here other solution, you can set variable from Actor to destroy and use it on Spawn Actor from Class - both solution are good, but I think, move destroy after Spawn... will be enough.
This is if you destroying referenced actor. In other cases, you can use IsValid to avoid 'pending destroy' error message, but only when it working as expected. Generally better when you not need to check IsValid, but sometimes this is necessary.
Follow this question
Once you sign in you will be able to subscribe for any updates here