TARRAY of actor BP Reference - how to set & spawn them

Hi,

I made a TARRAY define as below:

UPROPERTY(BlueprintReadWrite, Category="Test")
	TArray< TSubclassOf<AMyClass> > RefOfMyClass

In a BP that is launched during my game, I’m adding a BP Ref to the array.

Then in C++ I want to use the ref inside my array to spawn them:

 for ( int8 i = 0; i < RefOfMyClass.Num(); i++ )				{
					if ( RefOfMyClass[i] != NULL )
					{
						FVector ZeroLoc;
						FRotator ZeroRot;
						AMyClass* MyClass = GetWorld()->SpawnActor<AMyClass>(RefOfMyClass[i], ZeroLoc, ZeroRot, SpawnInfo);
									
					}
				}

for any reason: RefOfMyClass[i] != NULL is always false. I mean the loop starts, so it finds that item was added, but the value is null.

I also tried in my BP to loop & spawn just after the Add and it was not working.

Now I need help as I can’t see what’s wrong in my code, and I can’t find any useful post to find a solution.

Thanks in advance,

So you have made a BP of your MyClass and added it to the Array manually?
Now when you start the game, the RefOfMyClass[0] is NULL although you definitly added something?

How do you add the class? Just going into the BP Options and chosing it?
Maybe you need “EditAnywhere” in your UPROPERTY so that the Array can be edited.

Can you show me the part where you add something to the array?

Hi

The bp class is added during an event raised in another bp.

From the event i get the array, link the add node and select from the node aprameter the class I want to add.

I will check the editanywhere once i m back at home.

Hm, ok do that. I’m not 100% sure if this could fix it.
Maybe you can try to check if the Array is empty inside your BP
on some critical points. You could test if the ADD function works in
first place with some print and branch nodes.
I can’t tell you where your error comes from without seeing the actual
BP or code.

#After Begin Play?

Are you trying to use this BP during the construction of your class?

BP values are never valid until at least post init components, preferably you should be safe and use Begin Play!

Put all your code in Begin Play() and let us know if that helps!

void AYourClass::BeginPlay()
{
   Super::BeginPlay();

   //code here

}

For further clarity,I am saying that if you are filling the BP array in Blueprints by selecting blueprints to add in the Editor, these values are not registered in C++ until PostInitComponents(), I prefer BeginPlay() for guaranteed BP initialization

Rama

Hi,

Thanks for your feedback.
EditAnyWhere didn’t change anything.

To simplify the process and find the error, I set other nodes before the add node to “debug” the issue
C++ Definition of the Array
UPROPERTY(EditAnywhere, Category=“Dots”)
TArray< TSubclassOf > DoTs;

The blueprint is fired via a player input event like a key press. When this key is pressed, I want to be able to add references of BP in my array so I can spawn them later on in another part on my code.

To simplify in the test BP, I’m trying to spawn it immediatly after adding it to the array.

In this test, The Count of the array is “1” after adding (expected) but the spawn object is not valid (means no spawn at all, for reminder in C++, the item value of index 0 was null)

I hope my process is now clear for you and you might help me.
Thanks for taking time to help me!

I already check this. I post my BP also if this can help to track down the issue.
Thanks,

Any idea where the issue is? Do you need more information to track the error?

It seems to be a bug in the engine :

I will close the item except if I found this is not the same bug resolution.