I am new to the engine and fairly new to C++ and I have been trying to find out how to do this but I cannot figure it out or find any tutorials on it so I thought I’d try asking here.
https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/PossessPawns/Blueprints/index.html
This might be useful.
Thanks for the reply.
I have looked at this, but I want to do it in C++.
Code:
AController *MyPlayerController;
// It can be your character too. Since the ACharacter class inherits from APawn
APawn *MyPawn;
MyPlayerController->Possess(MyPawn);
Wiki page link:
I hope it helps!
Cheers!
Thanks for the reply.
Where would you call this? (In the gamemode?)
And what if you eg. wanted to make a custom character or pawn class and a custom playercontroller class and then use them?
You can call this anywhere where you need it. Its useful when you want to make a simple respawn logic or so.
I am not sure about your second question what you’re asking exactly but you definitely should make your own Character / Pawn classes for your pawns. Inhertied from the ACharacter or from the APawn. The difference is that, if you use the ACharacter you get a MovementComponent for your Character.
I mean if you add code to project and then click playercontroller or pawn/character would you just do it like this?
AMyCustomController *MyPlayerController;
AMyCustomPawn *MyPawn;
MyPlayerController->Possess(MyPawn);
Also I tried:
H:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/GameMode.h"
#include "ControllerTestGameMode.generated.h"
/**
*
*/
UCLASS()
class CONTROLLERTEST_API AControllerTestGameMode : public AGameMode
{
GENERATED_BODY()
AControllerTestGameMode(const class FObjectInitializer & ObjectInitializer);
virtual void BeginPlay() override;
public:
AController *MyPlayerController;
APawn *MyPawn;
};
CPP:
// Fill out your copyright notice in the Description page of Project Settings.
#include "ControllerTest.h"
#include "ControllerTestGameMode.h"
AControllerTestGameMode::AControllerTestGameMode(const class FObjectInitializer & ObjectInitializer)
:Super(ObjectInitializer)
{
}
void AControllerTestGameMode::BeginPlay()
{
Super::BeginPlay();
MyPlayerController->Possess(MyPawn);
}
And it crashed when i started playing.
The problem is that you’re not setting the
AMyCustomController *MyPlayerController;
AMyCustomPawn *MyPawn;
variables, i just put them into my code so you know their types.
If you’re new to C++ i recommend to try this first:
Then step by step learn the language
What would you set them to?
You need to get an instance of your Controller and Pawn
How would you do that?