How can i add variables in actor constructor?

Hi.

I want to add variables in actor constructor.

ex) AActor1( const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)

  -> AActor1( const FObjectInitializer& ObjectInitializer, var_type1 var1, var_tyyp2 var2..)

How can i do that?

Subclass the Actor class, and use that in your project.

EDIT: For another option, see here: Passing arguments to constructors in UE4? - C++ - Unreal Engine Forums

Sorry! I can’t understand your answer.

Please describe more detail


1 Like

Sorry - I assumed a level of C++ knowledge.

A constructor is what is called a ‘local’ method, meaning it belongs to the class (or subclass). If you look up a C++ tutorial, you’ll learn all about it.

Once you have subclassed Actor, you can add your own behaviour (and arguments) to the constructor.

Hi! Thanks for your answer,
I would try to create subclass for actor and add argument, but i could’t that


UCLASS()

class AMyActor : public AActor

{

GENERATED_BODY()

AMyActor(const FObjectInitializer& ObjectInitializer, FString wname);

}


Error at AActor!!

You have to define AMyActor::AMyActor() or AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer&)

For starters, the correct syntax in C++ would be

class AMyActor : public AActor

However, the easiest way to subclass something in UE4 is to go through the editor. This will add in all the necessary tags like UCLASS and make sure everything is properly added to the project. In the toolbar go to “File > Add Code to Project
” and select the class you want to extend from.

Unreal requires you to have the ‘AMyActor(const FObjectInitializer& ObjectInitializer)’ constructor using ONLY the FObjectInitializer argument. When you spawn an Actor the engine does the ‘new AMyActor()’ call for you. Just add a method to Initialize the actor with the arguments you need rather then using the constructor for it.

Pseudo code example:

AMyActor* myActor = Spawn(AMyActor); // Spawn an actor using a class, UE4 syntax differs from my pseudocode
myActor->Init(arg1, arg2, arg3);

Then you just need to define and implement the Init method which I used above.