UProperty() UObject derived class creation

I created a few UObject() derived class, they should belong to my ACharacter derived class.

If I create them in the constructor however they are replaced by nullptr somehow.

I create them like this:
SoADefault = NewObject(this, TEXT(“SoADefault”));

They are created successfully, but ingame my character has nullptr references.

Edit:
I also tried with CreateDefaultSubObject<>().
Then it isn’t replaced but somehow still false: after creation I use the “SoADefault->SetOwner(this);” call to set a pointer to the Character. However ingame the UObject has a pointer to a wrong instance of my character (address is different + it doesn’t have variables set I set in BeginPlay).

Is there a way to solve this from constructor?

Hey ,

To get a better understanding of your goal:

  • What is the reason behind using UObject, instead of another class?
  • What sort of data are you holding in the UObject and why is it important to be separate from the Character that you want it to belong to?

I ask because UObjects are not very flexible. If your goal is to have some flexibility when it comes to attaching components or Actors to your Character class, there are other options.

Thanks!

Hey, thanks for your reply.

I created a class hierarchy for character states - a base class with about 15-20 child at the moment. Those are the UObjects.

Some state specific value and function is stored in the state classes, it would be cool if they could be reachable directly from blueprint.

The character class has an instance from all state classes as UPROPERTY() variables.
I also have a pointer to the active one.
(This way I can handle inputs via delegating the function call to the active state.)

Edit: I was thinking about using ActorComponent instead of UObject as a parent, but maybe that would be a bit overkill? For all the 20 child states…

I moved the NewObject() calls to PreInitializeComponents, and it seems to work at the moment.

Would be cool to understand what happens with the objects if they are in the constructor.
Is there a template instance for the character class, and the one in the game is created from an auto-generated copy constructor using that?

Also if I used NewObject(this, TEXT("…")) then the properties won’t be garbage collected until the character itself is alive, right?

Thanks

So I tried a few new things to get UObjects to show up in the editor in a way where you can create them from the Blueprint menu.

For example, your character state class name is DeadObject for me.l

[.h]

#pragma once

#include "Object.h"
#include "DeadObject.generated.h"

/**
 * 
 */
UCLASS(BlueprintType, EditInlineNew)
class DEAD_API UDeadObject : public UObject
{
	GENERATED_BODY()
	
public:
	UDeadObject( );

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="DeadObject")
	int TestValOne;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="DeadObject")
	float TestValTwo;
	
};

[.cpp]

#include "Dead.h"
#include "DeadObject.h"

UDeadObject::UDeadObject( )
{
	TestValOne = 3;
	TestValTwo = 123.4432f;
}

[DeadCharacter.h]

public:

	// Gives access to C++ public and allows Blueprint to set / get  - (default is null)
	UPROPERTY( BlueprintReadWrite, Category="Dead" )
	class UDeadObject* ActiveState;

	// Instanced states that can be created in editor via character menu ("Dead")
	UPROPERTY( Instanced, EditAnywhere, BlueprintReadWrite, Category="Dead" )
	TArray<class UDeadObject*> AllStates;

This gives you the ability to added to the AllStates array from the editor and give each state its own values. Such as:

This will let you setup a bunch of states and then give you the option to set a ActiveState through either C++ or Blueprint as long as you have a Character reference.

Wow, thanks! Actually it’s much more than I hoped for - I thought I won’t be able to make it work in the editor panel - now I can setup the default values easier.
(I didn’t know Instanced and EditInlineNew… I guess I should check the specifiers after new releases :stuck_out_tongue: ).

(Btw. first I missed EditInlineNew, and couldn’t set the values from the list in BP (there was only a “None” option in the drop down list).

Can you explain to me what you mean by, “I can’t modify those values for the actors placed in the level.”?

Thanks.

Hi again!

I built a small class hierarchy from UObjects bases on your advice. I have an actor component, it has an array of pointers from those.

I use the property “Instanced”, and it works fine for class defaults - I can set the array elements in the child blueprint.

However, I can’t modify those values for the actors placed in the level.
Is there a way to make this work that way, or do something similar somehow? It would be really essential for me :frowning:

Sorry, my bad. It seems to work fine with actor, but if the array is the member of an actor component, if I select an instance in the level I can press anything (bool checkbox, array add element button, etc.), but nothing happens. I can’t modify any value related to the whole “Instanced” Uproperty data structure.

In blueprint class defaults it works with component as well.

Something like this:

I added this KinematicActorComponent to an actor blueprint. The key is the component somehow, if I do the same without the component (array is in an actor directly) it works fine.

Have you created a component in C++ that has these arrays of UObjects as instanced?

Can you post a screenshot as an example of the component and the UObjects?

Sorry for bothering you again, but is there any update on this?

Hey ,

Sorry for the delay. I am looking into it now and will update you when I can.

Hey ,

Thanks for the information. I was able to reproduce the issue of not being able to create instanced objects on a Actor in the world and have logged the issue. You can track its progress here: https://issues.unrealengine.com/issue/UE-34445

The issue is now marked as a duplicate. I could find no link to the one it duplicates.
What is the status of the problem? Where can I check it?

I think the duplicate is: Unreal Engine Issues and Bug Tracker (UE-38871) found by accident. Looking to solve the same issue…