Store ActorComponents in TArray

Hi,

new on UE5 learning something new … also a bloody c++ beginner

created an actorcomponent.
it should generate subcomponents of itself until some conditions are met.

CustomComponent.h

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Engine.h"
#include "CustomComponent.generated.h"

UCLASS()
class PROGRAM_API UCustomComponent : public UActorComponent
{
	GENERATED_BODY()

public:

	UCustomComponent();
	UCustomComponent(int arg_index, FVector arg_position, float arg_size);
	
	int index;
	FVector position;
	float size;
	
	TArray<UCustomComponent*> SubObjectArray;
	
	void GenerateSubComponents();

protected:

private:

};

CustomComponent.cpp

#include "CustomComponent.h"

UCustomComponent::UCustomComponent()
{
}

UCustomComponent::UCustomComponent(int arg_index, FVector arg_position, float arg_size)
{
	index = arg_index;
	position = arg_position;
	size = arg_size;
}


void UCustomComponent::GenerateSubComponents()
{

	// some code ...
	// do something with index, position and size ...
	
	if (something_is_true)
	{
		break;
	} else
	{
		SubObjectArray.SetNum(3);
		
		SubObjectArray[0] = UCustomComponent*(0, FVector(0.0f, 0.0f, 0.0f), 1.0f);
		SubObjectArray[1] = UCustomComponent*(1, FVector(1.0f, 1.0f, 1.0f), 2.0f);
		SubObjectArray[2] = UCustomComponent*(2, FVector(2.0f, 2.0f, 2.0f), 3.0f);

		for (UCustomComponent* child : SubObjectArray)
		{
			child->GenerateSubComponents();
		}
	}
}

everything builds fine expect:

		SubObjectArray[0] = UCustomComponent*(0, FVector(0.0f, 0.0f, 0.0f), 1.0f);
		SubObjectArray[1] = UCustomComponent*(1, FVector(1.0f, 1.0f, 1.0f), 2.0f);
		SubObjectArray[2] = UCustomComponent*(2, FVector(2.0f, 2.0f, 2.0f), 3.0f);

VisdualStudio Error:

error C2275: 'UCustomComponent': illegal use of this type as an expression

if stumbled across NewObject but i don´t get how i should pass parameters to it, so the array holds the instanced object with it´s custom parameters…

again this woould be necessary because SubObjectArray is used for stuff in other functions, too, as soon as every objectinstances ares generated and stored …

need some advice since i´m stuck with this for some time now.
Thanks for your time :slight_smile:

Well there are quite a few issues here and I’ll try to help as best I can. The main issue is that you’re calling the constructor of a class inside the class itself. While it is technically possible, there is a bunch of really great reasons as to why you shouldn’t. Also, in Unreal Engine you can’t call constructors at runtime, which is why we have NewObject.

I’m not sure what you’re trying to achieve exactly, but if you want an array of a class inside a class, those two classes need to be different classes. NewObject is used to create objects at runtime, so you’re correct and you need to use that.

Create another UActorComponent if that’s what you want to use. Call it whatever you want, like SubComponent.

in UCustomComponent, replace your constructors with something like
NewObject(this, USubComponent::StaticClass, TEXT(“SomeNameHere”))

To explain what NewObject does.
The <> is a type paramter. This tells NewObject the type of the object to be created. If you want to know more about this, try googling “Generics”.
The first parameter is the outer class. This is the object that will own the new object and control the lifetime of the new object. In this case, the outer is “this”, meaning the object making this call.
The second parameter is the UClass of the object you want to create. We use StaticClass for reasons that are probably a bit much to explain here, but if you’re curious look it up.
The third paramter is just a name for the object.

Now you’re wondering about how to pass your constructor parameters. Make a function in USubComponent, call it Init or something and make that function initialize your properties.

I hope that this helps, and I know this is all quite a lot, and don’t worry if you don’t understand all of it. Take the time you need to get an understanding of constructing objects in Unreal Engine. It’s complicated.

imagine UCustomComponent creating a simple plane
by size, position and index parameter passed from parent

  • GenerateSubComponents checks other variables and
    generates just this plane if ok … or
    generates a set of subplanes-> recalculate position, size, generate new indexes
    and store all this as new indiviual objects

  • in the end there would be an array holding
    the individual objects and their parameters
    so i can simply cycle through this array
    and execute different functions on the stored objects

maybe
SubObjectArray[i].SetColor(color)
SubObjectArray[i].Rotate(rotation)
SubObjectArray[i].Render()

or simply get the neighbours of a particular plane
a.s.o.

  • SubObjectArray is public and used in parent
  • size of SubObjectArray is unknown until generated
public void GenerateComponent(index, size, position)
{

    //calculate condition

    if (condition is true)
    {
        generate one object with values from parameter
        and add to SubObjectArray
    }
    else
    {
        generate new set of objects:
        calculation their sizes and position 
        based on parameter values
        and store them in SubObjectArray[...]
        
        execute GenerateComponent again on each
        element in array
    }
}


public void DoSomethingElse()
{
    for (element in SubObjectArray)
    {
        // code here
    }
}