How to pass parameters to pawn constructor?

Hello, i wanted to know if there’s a way to pass other parameters to a pawn constructor besides the PCIP? I’m trying to set a vehicle’s mass in the advance vehicle template by directly assigning the mass to the vehicle movement component, but i’ve found that it only works in the constructor, so is there a way to spawn or create a pawn with different parameters?

This is not possible in 4.5 yet. However, we just implemented support for custom constructors. The functionality is already in the master branch, and it will be available in 4.6.

Jarek will have further details on this.

Oh ok, thanks. I’ve already created a workaround for my specific problem but i’m glad it’ll be supported in 4.6

Do i need to set an answer in this case?

Hi Dylan,

since 4.6 you should be able to declare and define custom constructors in UObjects. In order to do that you have to:

  1. Change GENERATED_UCLASS_BODY() macro to the new GENERATED_BODY() macro in class definition. We will be gradually fading to use new macro, but it may broke old builds, so we didn’t do that yet. This can force you to declare old PCIP constructor in class header as old macro was declaring it, while new isn’t.
  2. Declare your constructor. Note that PCIP or default constructor is still required in every UObject, cause internal systems need it to e.g. load/save to disk.
  3. Use your new constructor with simple new UYourClass(YourConstructorParamList); statement.

Let me know if you encounter trouble with that feature.


Im sorry i didn’t quite understand your answer to use the new system all you need to do is change the GENERATED_UCLASS_BODY() macro to the new GENERATED_BODY()? also how do we create constructors that we can pass parameters to? do we have to include the PCIP parameter in the parameters like so:

AVehicle(const class FPostConstructInitializeProperties& PCIP, FString Path, FString Name);

also how to we pass these parameters when spawning an actor in the world?

Anyone? i still cannot work it out

Hey Kieran, Epic said passing paramters and creating custom constructors should be possible in 4.6 so i think there’s no way to do it right now, my workaround was to save the parameters as in saving the game, then spawn the actor and in the constructor of the actor load these parameters and set them to their respective variables inside the actor class so you can use them in your entire class definition, hope my solution helps but otherwise if your problem does not need inmediate solution like mine did, i would wait until the 4.6 release.

Hi Kieran,
sorry for the late response!

Basically, this feature is UObject feature, not AActor feature. Spawning is Actor-specific and we haven’t changed that.

To use it in UObjects you just need to change the macro and declare your constructor. Then you won’t need FPostConstructInitializeProperties (or FObjectInitializer, cause we’ve renamed it) from the UObject class perspective. Note that you will still need it if your base class needs it. UObject has non-PCIP constructor, but AActor hasn’t.

This is more like low-level feature and is not possible for actors right now. Sorry for the confusion.

So i can’t spawn objects and pass parameters without creating an initialize function and calling it after? If it’s a UObject feature surely it should also work on actors because they drive from UObjects don’t they?

Yes, they do, but it doesn’t mean it should work. Especially that, spawning is a feature of AActors, not UObjects.

Right now spawning is also creating the object. This makes it imposible to use custom constructors.

If you want to do it yourself my best bet would be to delegate the actor creation part (ConstructObject call in UWorld::SpawnActor) to some custom code, but I cannot guarantee it would work.

After updating to 4.7.1 I got this error message “ObjectName is not being constructed with either NewObject”.

Yeah, sorry. We had to disable using new, as it was causing other problems. You have to use NewObject etc. function. I guess you need to initialize your object some other way.