I dont know why its UnPossess half way!

I use this code too Re-Spawn my Pawn but I don’t know why halfway, it’s really hard to explain but better to show it to you.

it’s a Tank game.
this is the code I use for Re-spawn :

{
	auto GetActorloc = GetWorld()->GetFirstPlayerController()->GetPawn()->GetTargetLocation();
	auto GetActorRot = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorRotation();

	GetActorloc = FVector(GetActorloc.Y + 0, GetActorloc.X + 0, GetActorloc.Z + 600);
	GetWorld()->GetFirstPlayerController()->GetPawn()->Controller->UnPossess();

	auto GameMode = GetWorld()->GetAuthGameMode()->DefaultPawnClass;

	if (!ensure(GameMode)) { return; }
	
		GetWorld()->SpawnActor<APawn>
			(
				GameMode,
				GetActorloc,
				GetActorRot

				);
		//If you want detroy the old pawn
		//Destroy();

		TArray<AActor*> FoundActors;
		UGameplayStatics::GetAllActorsOfClass(GetWorld(), ATank::StaticClass(), FoundActors);

		
		auto LastPawn = Cast<ATank>(FoundActors[FoundActors.Num() - 1]);



		if (!ensure(LastPawn)) { return; };
			GetWorld()->GetFirstPlayerController()->Possess(LastPawn);
		
	
}

and this is my input component inbind with C++ :

as you can see it has two Part first set Barrel and Turret tank and AimingComponent moves them and second have left and right Track and responsible for TankMovement.
the big problem here is when I UnPossess the old tank and Possess the new tank, in the new tank TankMovement component work but AimingComponent still attach to the old tank even its aim wherever I aim with the new tank !!!

you can see it in the image the old tank aim anywhere I aim with the new tank but my Movement attached to new tank :

its happen even I use StartSpectatingOnly(); my Barrel and Turret still move!
I will thankful if anyone can say whats happening here! I can think to any logic why this happened ?!

Hi, First of all, thanks for fast replay.
Right now it’s only a tank in the world, you can observe 2 tanks in the image, first one near to camera is the new tank I spawn and the second tank is the old tank with AimingComponent.
yes, MovementComponent is fine but AimingComponent still attached to the old tank.

Answer:
first set camera movement in blueprint :

then in PlayerControl class I calculate Hit location with LineTraceSingleByChannel and send it to AimingComponent in AimingComponent I calculate velocity with SuggestProjectileVelocity and set Barrel and Turret in AimingComponent with connecting it to blueprint ( as the image you can find in brief ) and with some extra calculate to achieve desired speed for barrel and turret and send them to barrel and turret for moving them .
interesting is I spawn the projectile in AimingComponenet class, however, the old tank move barrel and turret but the second tank spawns and shoots the projectile !!!
if you still need codes too, let me know, I will copy them ASAP.

Troubleshooting
1)
if you check the spawn code I use // for destroy() cause when I destroy it I receive errors and warnings :

LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:GetColorAndOpacity_0:00EE
LogScript: Warning: Attempted to access Aiming via property BP Aiming Component Refrence, but Aiming is pending kill
    Aim_C /Engine/Transient.UnrealEdEngine_0:GameInstance_9.Aim_C_9
    Function /Game/Widget/Aim.Aim_C:Amo_left:001E

I connect the Re-Spawn cod with action key input to the player whenever tank rollover can Re-Spawn it again.
but the interesting thing is if I shoot the old tank the new tank will die with it too.

I didn’t get that one, my view changes with the new tank but in new tank barrel and turret not moving but in the old tank turret and barrel still move with aim.
you mean to put the second camera?

Hi, xXZUKINIXx . my number characters for replay to your reply was above the limit, in that case, I force to post my answer to your question as an answer to my question.
please check above your first replay, thanks.

I think I understand a little more as to what is happening.

Your current tank dies so you spawn another Tank(pawn) then move to posses that one. I initially thought you had 2 Tank(pawn) in the same scene but anyways doesn’t matter anymore.

It is interesting that you mentioned you got an error when trying to destroy the pawn. The “pending kill” error you are seeing is because in the code, it is trying to grab the object Aiming but it is up for deletion.

Conclusion

The pending kill confirms that we are trying to reference the old Tank(pawn) and use its Aiming and not the new one.

Troubleshooting

Lets try to UnPossess then Destroy Actor then Create Pawn
As long as the Destory comes right after the UnPosession

Question
The Aiming component is a variable attached to the Pawn correct? We are not making any references to it outside of its BP?

My Understanding

You have multiple Tanks in your game and when one dies, you take over the other one through possession. Now when possession is made the movement controls are fine it is only the aiming portion that is being affected.

Questions

How are you dealing with the aiming? Meaning, for every tank you have its own camera?
What bindings/controls are being used to aim your tank?

Just trying to get a better understanding of what is happening.

Troubleshooting

  1. I would destroy the old Actor before looking for Tanks. Correct me if I am wrong, but you are getting all Actors of Tank and not destroying it would still mean its in the array. Either way I didn’t notice any checks against already dead Tanks in the scene and I would recommend doing so.

  2. At what point do you check if the Tank is dead to Re-spawn. I am just concerned that if the Tank is “dead” but in the scene it is running the same script and getting conflicting behavior, all though I don’t think this is the issue.

  3. Have you tried manually changing the view target to see if you can get it to work as a /hack for now?

Troubleshooting:
the result is the same it doesn’t matter when we destroy the old tank.

Answer:
I upload the whole project, I will be glad if you can check it, sorry its maybe messy cause im practicing in it and try anything comes to my mind :

(there is another bug for movement I couldn’t solve it yet, please just ignore it )

thanks.

  1. Left click on Unreal Engine file then select Generate visual studio files and it will create your visual studio files.

2)open visual studio file on the main folder (MySecondTry.sln) then in visual studio build both Engine folder and Game folder.

251335-asfadsgasdg.jpg

  1. now open project, it must run without any problem.

let me know if there is any more problem.

Thanks again xXZUKINIXx.

I couldn’t open the project, what exact version are you using. You put 4.19 as a tag but I used that and it didn’t work. Also, did you do File → Package project → Zip Up Project

Hi xXZUKINIXx, did you succeed to open the project?

I have not had time yet, sorry, I will take a look on the weekend.

Thanks, xXZUKINIXx, im really curious to know what is your opinion about this problem.
I did some clean up in the project and you can download clean up version from here :