How to replace Default Static mesh to another mesh? C++

Hi, I have a ship mesh and it has two steering meshes as external meshes.

The first steering is level one and it has low control on the ship while driving.
The second steering is level two and it has high control on the ship while driving.

I attach the first level mesh by default when the ship is spawned in the level, and I want to replace this default steering when the user picked up the second level steering.
How to replace it?

my struggle to achieve this:

if (DefaultSteeringActor != nullptr)
	{
		SteeringMesh->SetStaticMesh(OutRow.DefaultSteering);
	}
	else
	{
		if (const UDataTable* DT_ShipResources{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/Tables/ShipResources")) })
		{
			if (const FST_ShipResources * OutRow{ DT_ShipResources->FindRow<FST_ShipResources>(FName(DefaultSteeringActor->ID), "")})
			{
				if (ID != FName(TEXT("2")))
				{
					SteeringMesh->SetStaticMesh(OutRow->SteeringLevel_1);
				}
				else
				{
					SteeringMesh->SetStaticMesh(OutRow->SteeringLevel_2);
				}
			}
			
		}
	}

the problem is when I tried to replace the mesh, the engine crashed with error array not found

please explain why you are setting the mesh for !null steeringmesh ?

if (DefaultSteeringActor != nullptr) // ??

you should always set the mesh to an actor if the actor is empty and it has place to take something.

Thank You for reply Sir, I am doing this because I am checking for nullptr to avoid any crash

you will not get any crash in anycase if the actor is empty, this is not the data coming from a class or from a struct which needs to be checked against any null array…

try to change it to check for perma nullptr.

if (DefaultSteeringActor == nullptr) // this should be always null if you want to set or replace new item. 
	{
		SteeringMesh->SetStaticMesh(OutRow.DefaultSteering);
	}

try this and let me know if it helps you, cheers!

2 Likes

yes it was the exact issue, the items are now replaced and the editor is not crashing , Thank You very very much Sir for help :slight_smile:

1 Like