Download

2 player local players

I have 2 PlayerStart actors in my scene. This is where my players should start by default.

I created an ActorHelper class:


#pragma once

#include "EngineUtils.h"

class TEMPLATEPLATFORMER_API ActorHelper
{
public:
	template<class T>
	T* FindActorsByTypeAndName(UWorld* currentWorld, FString name)
	{
		for (TActorIterator<T> ActorItr(currentWorld); ActorItr; ++ActorItr)
		{
			T* actor = *ActorItr;
			FString name = actor->GetName();

			if (actor->GetName() == name)
			{
				return actor;
			}
		}

		throw "You need to create a player starting position. The naming convention is Player<n>Start where <n> is the player number";
	}
};

This is called from my BaseGameMode classes inherited class GeneralGameMode:


	/**
	* Once the inheriting class has finished loading, MaxPlayers should have been set.
	* This function loops over the requested player count and takes control of the PlayerStart actors.
	*/
	template<class T>
	void LoadPlayersForGameMode(const T* gameMode)
	{
		// There is no point in running this loop if MaxPlayers is not set
		if (MaxPlayers)
		{
			ActorHelper* actorHelper = new ActorHelper();
			for (int32 a = 1; a <= MaxPlayers; a++)
			{
				/*ABasePlayer* newPlayer = ABasePlayer::Instantiate<T>(gameMode);*/
				UWorld* currentWorld = GetWorld();
				FString actorName = FString::Printf(TEXT("Player%dStart"), a);

				ABasePlayer* actor = actorHelper->FindActorsByTypeAndName<ABasePlayer>(currentWorld, actorName);
				if (actor != NULL)
				{
					SetupPlayerModel* setupPlayerModel = new SetupPlayerModel();
					setupPlayerModel->PlayerStartActor = actor;
					setupPlayerModel->PlayerNumber = a;

					actor->Setup(setupPlayerModel);
					

					Players.push_back(actor);
				}
				else
				{
					throw FString::Printf(TEXT("You are searching for a Player%dStart but it could not be found"), a);
				}
			}
		}
		else
		{
			throw "MaxPlayers was not set";
		}
	}

The thing is, when the code runs, it cannot find my 2 actors… In fact it just skips my T* FindActorsByTypeAndName(UWorld* currentWorld, FString name) Actor iterator loop and goes straight to the throw…

Any ideas?

My end goal is to take control over the 2 PlayerStarts and setup their controls:


void ABasePlayer::Setup(SetupPlayerModel* setupPlayerModel)
{

	// Set up player starting position

	FVector location = setupPlayerModel->PlayerStartActor->GetActorLocation();
	SetActorLocation(location);
		
	// A subclass could create a different InputComponent class but still want the default bindings
	if (InputComponent == NULL)
	{
		InputComponent = ConstructObject<UInputComponent>(UInputComponent::StaticClass(), this, *FString::Printf(TEXT("Player%dInputComponent"), setupPlayerModel->PlayerNumber));
		InputComponent->RegisterComponent();
	}
	AController* controller = GetController();

	/*newPlayer->InputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	newPlayer->InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);*/
	InputComponent->BindAxis(*FString::Printf(TEXT("Player%dMoveRight"), setupPlayerModel->PlayerNumber), this, &ABasePlayer::MoveRight);

	/*	newPlayer->InputComponent->BindTouch(IE_Pressed, this, &ATemplatePlatformerCharacter::TouchStarted);
	newPlayer->InputComponent->BindTouch(IE_Released, this, &ATemplatePlatformerCharacter::TouchStopped);*/
}