C++ how to set default value for TObjectPtr<UMaterialInstance>

Good day to you all,

My situation is the following. I have a c++ Actor in which I have a material instance variable TObjectPtr. I want it to be visible in the actor’s details panel when it’s selected in the scene.
Everything’s fine up to now, but my variable is empty by default and I’d like to be able to set a default material when I place my actor in the scene (material that I’ve also been able to retrieve with the ConstructorHelpers).

How can I set my Material Instance in the variable? I’m available if it’s not very clear :slight_smile:

Can you show the code for your header file with the TObjectPtr? This sounds like a UPROPERTY issue.

I’ve found my solution. After finding my asset with the constructor helper, all I just need to do is this:

MyEditableVariable = MyDefaultAsset.object;

I forgotten the .object

If it can help some people like me who aren’t very comfortable with C++

“MyEditableVariable” is my variable, visible from the editor in the properties tab and declared in the .h file.

“MyDefaultAsset” is the asset variable loaded with the constructor helper in the .cpp file.

Thank you for your time xenofsnstudios :smile: :+1:

Ah, so the issue was actually with using the value after grabbing it using the constructor helper. For others in the future, note that .object should be actually be capitalized.
Ie. MyEditableVariable = MyDefaultAsset.Object;

As for TObjectPtr, I didn’t know until now, but they are recommending using them in place of raw pointers from here on in. I had no idea, and I use raw pointers everywhere, so I guess I have quite a bit of migrating to do.

I found this video on TObjectPtr helpful:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.