Creating a class wtihout FPostConstructInitializeProperties& PCIP?

I have this class that I want to create within my AI



void ABaseAIController::moveToTarget()
{
	SetFocus(target);
	MoveToActor(target);
	Bot->currentAction =  new UMoveAction();
}




Here are the other classes:



UCLASS()
class UMoveAction : public UAction
{
	GENERATED_UCLASS_BODY()
	
	
};

UCLASS()
class AEntity : public ACharacter
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, Category = EntityType)
	TEnumAsByte<EntityEnums::EntityType> CurrentEntityType;
	UAction* currentAction;
	
};



This issue is that the parameters require FPostConstructInitializeProperties& PCIP, which I have no idea what it is or why I need one.

Is it possible to not have this in the parameters or if I do need it where do I get PCIP from?

new isn’t going to work for UE4 classes, have you tried replacing new UMoveAction(); with GetWorld()->SpawnActor(…)?

edit: if it’s an UObject you should use something similar to TestObject = NewObject<UPWNTestObject>(this, UPWNTestObject::StaticClass()); instead

edit2: The answer hub has a bunch of good answers to lots of very specific problems. More info here.

Im not an expert of UE4 yet, but i think you can use this Bot->currentAction = UMoveAction::StaticClass(); ??

So I now have this:



Bot->CurrentAction = NewObject<UMoveAction>(this, UMoveAction::StaticClass());


but how would I pass in parameters then?

This required line for UObject declarations…


GENERATED_UCLASS_BODY()

… effectively creates some glue that makes your class work with the UObject sub-system. Part of that glue is the declaration of a class constructor, and it is required that you implement it in your module. If your class has a lot of methods, you probably already have a separate .cpp file just for that class, and you can add the constructor definition to that file. If your class doesn’t have any code, or all code is inlined, then you can just throw it into any other .cpp file. Typically I use the MyModule.cpp for that, or if I have many classes with no code I add MyModuleClasses.cpp and put the constructors in there (for example, take a look at SettingsClasses.cpp).

The format for the constructor definition is as follows:



UMyCoolClass::UMyCoolClass( const class FPostConstructInitializeProperties& PCIP )
	: Super(PCIP)
{
    /* do object initialization in here, if needed, otherwise leave empty */
}


For the creation of UObjects see: UObject Instance Creation
For the creation of AActors see: Spawning Actors

Note that UObjects (and therefore AActors as well) have their properties initialized from the INI file, or set to 0/nullptr if no settings are specified. You cannot pass any additional arguments into the constructors but will have to defer all run-time initialization to subsequent method calls or property assignments after creating the object. It seems like a limitation at first, but it opens up some interesting possibilities. For example, all UObjects and actors have the same constructor API, which means that they can be easily created automatically by other means, i.e. from the Blueprint system or when they’re replicated over the network. The only thing you have to provide is a class name.

If custom constructor arguments were allowed then we would have to provide a mechanism to generate constructor calls and populate them with valid arguments automatically at run-time. Probably the only elegant way to do this would be a dependency injection framework, which is quite difficult to create and use in C++. In other similar languages, such as C# this may be a little bit easier, but note that even there most frameworks do not allow custom constructor arguments for serializable objects.

Is is possible to make a normal c++ class instead, I only need something that extends base object anyway?

You can just create your own plain old C++ classes in Visual Studio.


class FMyCoolClass { .. };

It’s not possible yet to create C++ classes from the Editor yet.

Thanks, I’ll give it ago :smiley:

I’ve added this to the wiki a while back:
https://wiki.unrealengine.com/UE3_to_UE4_Transition_Guide#Coding

Do I still need a .h and .cpp for normal c++ classes?

I’ve got this atm as my .h:



namespace ActionEnums
{
	enum ActionType
	{
		Movement,
		Combat,
		Other
	};
}


class Action 
{
	ActionEnums::ActionType CurrentActionType;	
};



and this as my .cpp:



#include "The_Battle_of_Maldon.h"
#include "Action.h"

Action::Action()
{

}


doesn’t like me doing Action::Action()?

What happens if you put the constructor declaration in the header?
Action();

Ah ha, I forgot about constructors, didn’t realize that it went in here:



class Action 
{
         Action()
         {

         }
	ActionEnums::ActionType CurrentActionType;	
};


You’ve fixed my problem, thanks :smiley:

New issue I have this within my Bots:

Action* CurrentAction;

And I try and assign it within my AIController like so:

Bot->CurrentAction = new MoveAction();

Here is MoveAction



#pragma once

#include "Action.h"

class MoveAction : public Action
{
public:
	MoveAction()
	{
		CurrentActionType = ActionEnums::Movement;
	}
};


It’s giving me this as an error:

error C2440: ‘=’ : cannot convert from ‘MoveAction *’ to ‘Action *’

Any Ideas?

Your Action() constructor is private, the default access type for a class. Make it public.

I wish I had a second pair of eyes around, almost all problems are stupid mistakes, Thanks so much :smiley:

Complier says no, looks like theres lot’s wrong within Action:




namespace ActionEnums
{
	enum ActionType
	{
		Movement,
		Combat,
		Other
	};
}

class Action
{
public:
	Action()
	{

	}
	ActionEnums::ActionType CurrentActionType;
};


Getting these erorrs:

‘Action’ : ‘class’ type redefinition
‘ActionEnums::ActionType’ : ‘enum’ type redefinition
‘CurrentActionType’ : undeclared identifier
‘Action’ : base class undefined

The header file is probably getting included more than once. You want this at the top of every .h:


#pragma once

The file layout of C++ is a little tricky, and UE4 projects can make that more complicated with generated headers. You may want to experiment a bit with a simple Win32 Console Application before writing any serious code.