Hello,
So right now I’m trying to figure something out with my game. I wanted to have my game mode spawn two character BPs and then have each character be controlled by their respective players. But I’m kind of confused on how I would make it work. I wanted to try and make some PlayerController variables in my GameMode class and try to assign them to each client, but I’m not sure if that is right. Can anyone help? Maybe provide somewhere to start?
Here’s my GameMode code in case you need it
Gamemode.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProject.h"
#include "EngineUtils.h"
#include "MyProjectGameMode.h"
#include "BaseCharacter.h"
#include "Arrow.h"
#include "SpawnPoint.h"
AMyProjectGameMode::AMyProjectGameMode(const class FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
static ConstructorHelpers::FObjectFinder<UBlueprint> DesiredCharacter(TEXT("Blueprint'/Game/Characters/Elf/Elf_BP.Elf_BP'"));
if (DesiredCharacter.Object)
{
Player1_Class = (UClass*) DesiredCharacter.Object->GeneratedClass;
}
}
void AMyProjectGameMode::StartPlay()
{
Super::StartPlay();
StartMatch();
//SpawnPlayers();
}
void AMyProjectGameMode::SpawnPlayers()
{
UWorld* World = GetWorld();
if (World)
{
for (TActorIterator<ASpawnPoint> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Spawn"));
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
FRotator SpawnRot = FRotator(0.0, 0.0, 0.0);
FVector SpawnLoc = ActorItr->GetActorLocation();
Player1 = World->SpawnActor<ABaseCharacter>(Player1_Class, SpawnLoc, SpawnRot, SpawnParams);
//Player1 = World->SpawnActor<ABaseCharacter>(Player1_Class);
P1_Control = UGameplayStatics::GetPlayerController(World, 0);
P1_Control->Possess(Player1);
}
}
}
void AMyProjectGameMode::SpawnArrow()
{
FVector ArrowSpawnLocation = FVector(400.0, 400.0, 150.0);
FRotator DefaultRotation = FRotator(0.0, 0.0, 0.0);
Arrow = GetWorld()->SpawnActor<AArrow>(AArrow::StaticClass());
}
Gamemode.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Arrow.h"
#include "BaseCharacter.h"
#include "GameFramework/GameMode.h"
#include "MyProjectGameMode.generated.h"
/**
*
*/
UCLASS()
class MYPROJECT_API AMyProjectGameMode : public AGameMode
{
GENERATED_BODY()
virtual void StartPlay() override;
AMyProjectGameMode(const FObjectInitializer& ObjectInitializer);
int32 P1Score;
TSubclassOf < class ABaseCharacter > Player1_Class;
ABaseCharacter* Player1;
APlayerController* P1_Control;
APlayerController* P2_Control;
UFUNCTION()
void SpawnPlayers();
UFUNCTION()
void SpawnArrow();
public:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Arrow")
AArrow* Arrow;
};