Download

Custom movement component won't compile.

I’ve created a new project and a HoverTankPawn (based on APawn) and HoverTankMovementComponent(Based on PawnMovementComponent).

I’m just trying to get the basic class setup first, so when I create a blueprint I get a HoverTankMovementComponent and a MeshComponent. However, my code won’t compile. Any glaring errors that you can see?

HoverTankPawn.h:





#pragma once

#include "GameFramework/Pawn.h"
#include "HoverTankPawn.generated.h"

/**
 * 
 */
UCLASS()
class AHoverTankPawn : public APawn
{
	GENERATED_UCLASS_BODY()

	/* The Static Mesh associated with this hover tank */
	UPROPERTY(Category = HoverTank, VisibleAnywhere, BlueprintReadOnly)
	TSubobjectPtr<class UStaticMeshComponent> Mesh;

	/* Movement Component */
	UPROPERTY(Category = HoverTank, VisibleAnywhere, BlueprintReadOnly)
	TSubobjectPtr<class UHoverTankMovementComponent> HoverTankMovement;

};


HoverTankPawn.cpp:





#include "ExtraNodesTest.h"
#include "HoverTankPawn.h"


AHoverTankPawn::AHoverTankPawn(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	HoverTankMovement = PCIP.CreateDefaultSubobject<UHoverTankMovementComponent>(this, TEXT("Movement Component"));
	Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
}


HoverTankMovementComponent.h





#pragma once

#include "GameFramework/PawnMovementComponent.h"
#include "HoverTankMovementComponent.generated.h"

UCLASS()
class UHoverTankMovementComponent : public UPawnMovementComponent
{
	GENERATED_UCLASS_BODY()

	
	
};


HoverTankMovementComponent.cpp





#include "ExtraNodesTest.h"
#include "HoverTankMovementComponent.h"


UHoverTankMovementComponent::UHoverTankMovementComponent(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

}


As you can see, I haven’t added anything to the movement component. I’ve just tried to add two default components in the constructor of the HoverTankPawn.cpp file. This is the error I’m getting:



1>c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(569): error C2440: 'initializing' : cannot convert from 'UHoverTankMovementComponent *' to 'UObject *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>          c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(567) : while compiling class template member function 'TSubobjectPtrConstructor<UHoverTankMovementComponent>::TSubobjectPtrConstructor(SubobjectType *)'
1>          with
1>          
1>              SubobjectType=UHoverTankMovementComponent
1>          ]
1>          c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(682) : see reference to function template instantiation 'TSubobjectPtrConstructor<UHoverTankMovementComponent>::TSubobjectPtrConstructor(SubobjectType *)' being compiled
1>          with
1>          
1>              SubobjectType=UHoverTankMovementComponent
1>          ]
1>          C:\Users\JamesB\Documents\Unreal Projects\ExtraNodesTest\Source\ExtraNodesTest\HoverTankPawn.cpp(10) : see reference to class template instantiation 'TSubobjectPtrConstructor<UHoverTankMovementComponent>' being compiled
1>c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(569): error C2439: 'TSubobjectPtrConstructor<UHoverTankMovementComponent>::Object' : member could not be initialized
1>          c:\program files\unreal engine\4.2\engine\source\runtime\coreuobject\public\uobject\UObjectGlobals.h(565) : see declaration of 'TSubobjectPtrConstructor<UHoverTankMovementComponent>::Object'
1>  -------- End Detailed Actions Stats -----------------------------------------------------------
1>ERROR : UBT error : Failed to produce item: C:\Users\JamesB\Documents\Unreal Projects\ExtraNodesTest\Binaries\Win64\UE4Editor-ExtraNodesTest.pdb
1>  Cumulative action seconds (12 processors): 0.00 building projects, 0.87 compiling, 0.00 creating app bundles, 0.00 generating debug info, 0.00 linking, 0.00 other
1>  UBT execution time: 9.34 seconds
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets(38,5): error MSB3073: The command ""C:\Program Files\Unreal Engine\4.2\Engine\Build\BatchFiles\Build.bat" ExtraNodesTestEditor Win64 Development "C:\Users\JamesB\Documents\Unreal Projects\ExtraNodesTest\ExtraNodesTest.uproject" -rocket" exited with code -1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Answer in laymans terms if you will please :slight_smile: I’m still new to programming.

You need to include HoverTankMovementComponent.h in HoverTankPawn.cpp.

The error says it cannot convert from ‘UHoverTankMovementComponent *’ to ‘UObject *’. This is because the compiler doesn’t know they’re related types since you’re not including the header.

Thanks, you were right. I actually had to include it in the Game.h file instead… how strange.

Works a charm now :smiley: