Dynamically Constructing UObjects'!

I have problems here with Unreal Engine 5.
The compiler works fine but at runtime, the editor crash with the message: Line: 3542] NewObject with an empty name can’t be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSubobject<> instead.

Does anyone have a working solution for this step in the course what is the code for the for loop in the CPP file and how is the declaration in the header file for that?

Hi Mario,
Can you provide more information, possibly a sample of the code you are trying to do.

The CreateDefaultSubobject needs the name of the item you are creating as well as the type. You use code something along the lines of:

Camera = CreateDefaultSubobject<UCameraComponent>( TEXT( "Camera" ) );
Camera->SetupAttachment( GetRootComponent() );

Where the Camera variable is declared as:

UCameraComponent* Camera;

The code would typically be in your constructor. I’m not sure why a for loop would come into play with this. Is this related to the splines?

Hi beeceedee, I’m not really sure you understand the issue right here.
I will try to give you more details and code. The code comes basically from the lesson “Dynamically Constructing UObjects” inside the Udemy VR course that I am following currently.
First things first the Udemy VR course from Sam Pattuzzi (GameDev.tv) have very good quality and we need more VR open XR C++ Unreal skills instead of blueprint solutions on the market. I’ve integrated the code example from lesson 22 in my UE5 solution and the editor crashes with the issue that I sent before.
I’ve changed the code inside the for loop example now with the CPP file code below and duplicated this for the other controller as well (I want to use that for the second controller as well).
With this change inside the code, the editor doesn’t crash now but I can’t spawn a spherical element at the path points, anyway I can’t see them.

Course Example;

void AVRCharacter::DrawTeleportPath(const TArray<FVector> &Path)
{
	UpdateSpline(Path);

	for (int32 i = 0; i < Path.Num(); ++i)
	{
		if (TeleportPathMeshPool.Num() <= i)
		{
			UStaticMeshComponent* DynamicMesh = NewObject<UStaticMeshComponent>(this);
			DynamicMesh->AttachToComponent(VRRoot, FAttachmentTransformRules::KeepRelativeTransform);
			DynamicMesh->SetStaticMesh(TeleportArchMesh);
			DynamicMesh->SetMaterial(0, TeleportArchMaterial);
			DynamicMesh->RegisterComponent();

			TeleportPathMeshPool.Add(DynamicMesh);
		}

		UStaticMeshComponent* DynamicMesh = TeleportPathMeshPool[i];

		DynamicMesh->SetWorldLocation(Path[i]);
	}

}

my functional code:

void AMy_VR_Character::DrawTeleportPath_R(const TArray<FVector>& Path)
{
	
	UpdateSpline_R(Path);

	for (int32 i = 0; i < Path.Num(); ++i)
	{
		if (TeleportPathMeshPool.Num() <= i)
		{
			DynamicMesh_R = NewObject<UStaticMeshComponent>(this,
				UStaticMeshComponent::StaticClass(), TEXT("DynamicMesh_R"));
			DynamicMesh_R->RegisterComponent();
			DynamicMesh_R->AttachToComponent(VRRoot, FAttachmentTransformRules::KeepRelativeTransform);
			DynamicMesh_R->SetStaticMesh(TeleportArchMesh_R);
			DynamicMesh_R->SetMaterial(0, TeleportArchMaterial);
			

			TeleportPathMeshPool.Add(DynamicMesh_R);
		}

		DynamicMesh_R = TeleportPathMeshPool[i];

		DynamicMesh_R->SetWorldLocation(Path[i]);
		
	}

}

my Header file;

public:
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VRCharacter|TeleportPathMeshPool") 
	UStaticMeshComponent* DynamicMesh_R;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "VRCharacter|TeleportPathMeshPool") 
	UStaticMeshComponent* DynamicMesh_L;

Actually, this is basically the same idea. This code does work in UE5 as I tested it in the prerelease version in January and again once released.

Have you got live coding turned on or off? If it is on, turn it off, exit the editor and delete the binaries folder before opening the project again.

Anyway, here’s the code that I know works - this won’t work yet with what you have as this is covered in the next lecture. As it happens, the code in the course also works. I’ve gone through it a number of times.

void AVRCharacter::RegisterNewObject()
{
  USplineMeshComponent* SplineMesh  = NewObject<USplineMeshComponent>( this );
  SplineMesh->SetMobility( EComponentMobility::Movable );
  SplineMesh->AttachToComponent( TeleportPath, FAttachmentTransformRules::KeepRelativeTransform );
  SplineMesh->SetStaticMesh( TeleportArchMesh );
  SplineMesh->SetMaterial( 0, TeleportArchMaterial );
  SplineMesh->RegisterComponent();
  TeleportPathMeshPool.Push(SplineMesh);
}

Thanks a lot for your fast answer. I want to go back now and use the course code for a single teleporter controller. I want to find out why I couldn’t see the small spawned spheres in the center of debugging trace spheres before and the editor has crashed every time.
You mentioned you have gone through the course example a number of times. have you seen the spawned spheres inside the debugging spheres? I think that’s important for the next step.

As far as I recall, and this is almost a year ago, this is the code I used before the meshes were added.

void AVRCharacter::DrawParabola( FVector outLocation )
{
  FPredictProjectilePathParams inParams;
  FPredictProjectilePathResult pathResult;
  FVector lookVector = RightMotionController->GetForwardVector();
  FVector launchVelocity = RightMotionController->GetComponentLocation() + lookVector * 1000.0f;//  change to parameter on BP
  inParams.DrawDebugType = EDrawDebugTrace::ForOneFrame;
  inParams.LaunchVelocity = launchVelocity;

  UGameplayStatics::PredictProjectilePath( this, inParams, pathResult );
}

Now everything works perfectly, even with a second controller and Laser. Thanks a lot for your help. Both lasers I’ve connected with Vive trigger for teleporting.
Last but not least, have you an idea for the approach to hiding and unhiding the laser through by pushing the menü button on the Vive controller as a toggle solution before I will teleport (header and c++ example, please)?

Privacy & Terms