Construct Event for UObjects?

I’m learning blueprints and I have a quick question. I created a UObject blueprint. Is there a way to initialize a value when the object is instantiated? Do I need to create an actor blueprint for that or am I just missing something.

2 Likes

ahh ■■■■. i got the same problem. im not happy that there is no answer here.
im thinking about it, that when im constructing the object, i instantly call a custom event on it. sounds bad, but have no other idea how to manage it better

It follows the same pattern as everything else - expose the variables:

unreal constructors don’t take arguments, so, yes, if you must pass values to it, you’ll need to do that after it is constructed.

If you want to hide that under a layer, you could use a factory function that takes in parameters, creates the thing, then sets them on it, and returns the new thing

I have a text variable, i ticked these two flags, but it wont show up in the construct node.

by factory function you mean a function which lives in the Blueprint Function library?

well, a function somewhere. needs to be accessible to where it’s needed. could be in the same object, or in a function library, or some other object you can get at .

Choose the pertinent class from the dropdown first, then right click the node - Refresh Nodes.


It should work as is, there is onClassPinChanged() or whatshername but sometimes manually refreshing nodes is the way.

1 Like

I also use UObject as data store, and this results in a sufficiently deeply nested hierarchy of objects which is hard to handle when editing it via exposed UPROPERTIES in the Details window. In this fashion I am also unable to reutilized resources between parts of the hierarchy.

You can leverage the PostInitProperties. What I did was create my UObject at the C++ level then overrode the method and added my own BP method that could be overridden for initialization that is called in PostInitProperties so it essentially acts as a constructor.

//Header
	virtual void PostInitProperties() override;

	// Blueprint-native function to allow Blueprint overrides
	UFUNCTION(BlueprintNativeEvent, Category = "Initialization")
	void OnInitialized();

	// Default C++ implementation of the BlueprintNativeEvent
	virtual void OnInitialized_Implementation();

//CPP
void MyObject::PostInitProperties()
{
    Super::PostInitProperties();
    OnInitialized();
}

void MyObject::OnInitialized_Implementation()
{
}