EDIT: UE 4.7!
Hey guys,
i’m just digging into UE4 C++ and started myself a small project to learn a tiny bit of everything for the start.
What am i trying to do?
I want to let the GameMode (Serverside) spawn a weapon for every connected player.
My GameMode.h:
#pragma once
#include "GameFramework/GameMode.h"
#include "SmallCoopTDGameMode.generated.h"
UCLASS(Blueprintable)
class ASmallCoopTDGameMode : public AGameMode
{
GENERATED_BODY()
/** Max Players to be waited for before starting the match **/
int32 MaxNumberOfPlayers;
/** Difficulty, regarding the strength of the enemies **/
int32 Difficulty;
public:
ASmallCoopTDGameMode(const FObjectInitializer& ObjectInitializer);
//void SetStartWeapons();
UFUNCTION(reliable, server, WithValidation)
void ServerSetStartWeapons();
bool ServerSetStartWeapons_Validate();
void ServerSetStartWeapons_Implementation();
virtual void BeginPlay() override;
};
And my GameMode.cpp:
#include "SmallCoopTD.h"
#include "SmallCoopTDGameMode.h"
#include "SmallCoopTDPlayerController.h"
#include "SmallCoopTDCharacter.h"
#include "UnrealNetwork.h"
#include "TestWeapon1.h"
ASmallCoopTDGameMode::ASmallCoopTDGameMode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
// use our custom PlayerController class
PlayerControllerClass = ASmallCoopTDPlayerController::StaticClass();
// set default pawn class to our Blueprinted character
/*static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/TopDown/Blueprints/TopDownCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}*/
DefaultPawnClass = ASmallCoopTDCharacter::StaticClass();
}
bool ASmallCoopTDGameMode::ServerSetStartWeapons_Validate()
{
return true;
}
void ASmallCoopTDGameMode::ServerSetStartWeapons_Implementation()
{
AGameState* TempGameState = GetGameState<AGameState>(); //Ignore this. Was a test for getting the Controller with the PlayerStateOwners etc.
ASmallCoopTDCharacter* TempCharacter;
int32 i = 3; // DEBUG
for (FConstControllerIterator It = GetWorld()->GetControllerIterator(); It; It++)
{
GEngine->AddOnScreenDebugMessage(i, 20.0f, FColor::Green, FString::FromInt(i)); // DEBUG
i++; // DEBUG
ASmallCoopTDPlayerController* PC = Cast<ASmallCoopTDPlayerController>(*It);
if (PC)
{
TempCharacter = Cast<ASmallCoopTDCharacter>(PC->GetPawn());
if (TempCharacter)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Instigator = Instigator;
SpawnParams.Owner = PC;
SpawnParams.Name = "MyTestWeapon";
SpawnParams.bNoCollisionFail = true;
ATestWeapon1* TempWeapon = GetWorld()->SpawnActor<ATestWeapon1>(ATestWeapon1::StaticClass(), FVector(0, 0, 500), FRotator(0, 0, 0), SpawnParams);
//TempWeapon->SetActorLocation(FVector(0, 0, 500));
TempWeapon->AttachRootComponentTo(TempCharacter->GetMesh(), FName("WeaponSocket"), EAttachLocation::SnapToTarget, false);
TempCharacter->SetCurrentWeapon(TempWeapon);
GEngine->AddOnScreenDebugMessage(13, 20.0f, FColor::Green, PC->GetName()); // DEBUG
}
}
}
}
void ASmallCoopTDGameMode::BeginPlay()
{
Super::BeginPlay();
if (Role == ROLE_Authority)
{
ServerSetStartWeapons();
}
}
What does it do by now?
It spawns a weapon for the Server only. The Weapon is also only visible for the server, so i guess he is the only one spawning here at all.
Replication and RPCs still giving me headache x). I guess i need a multicast function, but can someone point me into the right direction for the start? Also the ControllerIterator only does all the things ONE time, although there are more Controllers (or at least there should be more, since only the server should call this function).
I hope someone can give me a bit of an insight on what i am doing terribly wrong here.
(: Thanks in advance you awesome people.