# Take Function to another class

**URL:** <https://forums.unrealengine.com/t/take-function-to-another-class/490047>\
**Category:** C++\
**Tags:** classes, hierarchy, unreal-engine, functions, CPP, question\
**Created:** [October 21, 2021, 10:56pm UTC](https://forums.unrealengine.com/t/take-function-to-another-class/490047 "2021-10-21T22:56:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MongeRastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MongeRastafari](https://forums.unrealengine.com/u/MongeRastafari)\
**Post date:** [October 21, 2021, 10:56pm UTC](https://forums.unrealengine.com/t/take-function-to-another-class/490047/1 "2021-10-21T22:56:11Z")

</div>

hello guys, how can I call this restartplayer in another class?

.h

virtual void RestartPlayer(class AController\* NewPlayer) override;

.cpp

void ASCoopGameMode::RestartPlayer(class AController\* NewPlayer)

{

```
if (!bSpawnAtTeamPlayer || (NewPlayer->PlayerState && NewPlayer->PlayerState->IsABot()))
{
	Super::RestartPlayer(NewPlayer);
	return;
}

FVector SpawnOrigin = FVector::ZeroVector;
FRotator StartRotation = FRotator::ZeroRotator;
for (TActorIterator<APawn> It(GetWorld()); It; ++It)
{
	ASCharacter* MyCharacter = Cast<ASCharacter>(*It);
	if (MyCharacter && MyCharacter->IsAlive())
	{
		/* Get the origin of the first player we can find */
		SpawnOrigin = MyCharacter->GetActorLocation();
		StartRotation = MyCharacter->GetActorRotation();
		break;
	}
}

/* No player is alive (yet) - spawn using one of the PlayerStarts */
if (SpawnOrigin == FVector::ZeroVector)
{
	Super::RestartPlayer(NewPlayer);
	return;
}

/* Get a point on the nav mesh near the other player */
FNavLocation StartLocation;
UNavigationSystemV1* NavSystem = UNavigationSystemV1::GetNavigationSystem(this);
if (NavSystem && NavSystem->GetRandomPointInNavigableRadius(SpawnOrigin, 250.0f, StartLocation))
{
	// Try to create a pawn to use of the default class for this player
	if (NewPlayer->GetPawn() == nullptr && GetDefaultPawnClassForController(NewPlayer) != nullptr)
	{
		FActorSpawnParameters SpawnInfo;
		SpawnInfo.Instigator = GetInstigator();
		APawn* ResultPawn = GetWorld()->SpawnActor<APawn>(GetDefaultPawnClassForController(NewPlayer), StartLocation.Location, StartRotation, SpawnInfo);
		if (ResultPawn == nullptr)
		{
			UE_LOG(LogGameMode, Warning, TEXT("Couldn't spawn Pawn of type %s at %s"), *GetNameSafe(DefaultPawnClass), &StartLocation.Location);
		}
		NewPlayer->SetPawn(ResultPawn);
	}

	if (NewPlayer->GetPawn() == nullptr)
	{
		NewPlayer->FailedToSpawnPawn();
	}
	else
	{
		NewPlayer->Possess(NewPlayer->GetPawn());

		// If the Pawn is destroyed as part of possession we have to abort
		if (NewPlayer->GetPawn() == nullptr)
		{
			NewPlayer->FailedToSpawnPawn();
		}
		else
		{
			// Set initial control rotation to player start's rotation
			NewPlayer->ClientSetRotation(NewPlayer->GetPawn()->GetActorRotation(), true);

			FRotator NewControllerRot = StartRotation;
			NewControllerRot.Roll = 0.f;
			NewPlayer->SetControlRotation(NewControllerRot);

			SetPlayerDefaults(NewPlayer->GetPawn());
		}
	}
}

```

}

---

<div class="post-metadata">

**Author:** ![Nokturnal334](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nokturnal334/32/88713_2.png) [@Nokturnal334](https://forums.unrealengine.com/u/Nokturnal334)\
**Post date:** [October 22, 2021, 12:34am UTC](https://forums.unrealengine.com/t/take-function-to-another-class/490047/2 "2021-10-22T00:34:01Z")

</div>

Do you want to call RestartPlayer from your GameMode?

---

<div class="post-metadata">

**Author:** ![MongeRastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MongeRastafari](https://forums.unrealengine.com/u/MongeRastafari)\
**Post date:** [October 22, 2021, 10:44am UTC](https://forums.unrealengine.com/t/take-function-to-another-class/490047/3 "2021-10-22T10:44:02Z")

</div>

no, i want call this function in another script and put in another function.for example, I have this function that restarts the player in a script, in another one I have one that is called killed(), I want to call this restart function in this function killed

---

<div class="post-metadata">

**Author:** ![MongeRastafari](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MongeRastafari](https://forums.unrealengine.com/u/MongeRastafari)\
**Post date:** [October 22, 2021, 10:47am UTC](https://forums.unrealengine.com/t/take-function-to-another-class/490047/4 "2021-10-22T10:47:27Z")

</div>

i used this: AScoopMultiplayer\* MyController = Cast(???);  
but it didn’t work because I don’t know what I have to put in these question marks

---

<div class="post-metadata">

**Author:** ![CtnDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ctndev/32/1133321_2.png) [@CtnDev](https://forums.unrealengine.com/u/CtnDev)\
**Post date:** [October 22, 2021, 11:11am UTC](https://forums.unrealengine.com/t/take-function-to-another-class/490047/5 "2021-10-22T11:11:02Z")

</div>

Try this `AScoopMultiplayer* MyController = Cast<AScoopMultiplayer>(APlayerController::StaticClass())`
