C++ equivalent to "expose on spawn"

What is direct C++ equivalent to “expose on spawn” behavior found in blueprints?

In blueprints “expose on spawn” allows passing values into blueprint’s construction script, (as I understand it) technically before object is added into scene.

In my project I have (C++) “init” method (which I call manually) for this kind of thing, but as far as I can tell, that one will be called AFTER object has been added into scene.

There is “OnConstruction” method that can be overrided, but its only parameter is FTransform, so I can’t really pass values through it.

So, what would be an equivalent to “Expose on spawn” behavior found in blueprints.

4 Likes

In C++ you can call NewObject and set your variables or you can try this approach.

This would be a UPROPERTY() META attribute, which looks like this:

UPROPERTY(BlueprintReadWrite, Category = “Default”, Meta(ExposeOnSpawn=true))
bool ExposedVariable;

This would expose the C++ variable in the spawn node within blueprints, but if you need to know a variable value in C++ before you can finish spawning, you’ll need to use deferred spawning.

8 Likes

That ain’t it. I’d like to know how to pass parameters into object during construction in the manner similar to “expose on spawn”. I’m not looking for a way to expose variables to a blueprint. After all, in unreal construcor only sets default values and isn’t used to pass parameters into objects.

1 Like

Use SpawnActorDeferred to initialize the variables before spawning the object with FinishSpawningActor although there isn’t any way to pass in the parameters within the spawn call as far as i can tell.

Edit : BeginDeferredActorSpawnFromClass is the way to go since SpawnActorDeferred has been deprecated.

5 Likes

That worked for me, although mine looked more like this:

UPROPERTY(BlueprintReadWrite, Category = "Default", meta = (ExposeOnSpawn="true"))
7 Likes

Slayemin has a mistake (the “=” sign after “Meta”), it’s supposed to be: Meta = (ExposeOnSpawn=“true”)

4 Likes