[4.7.6][CPP + BP]Placed actors don't update after adding cpp component

Placed actors don’t update after adding cpp component.

Steps to reproduce:

  1. Create some basic Actor class in cpp.
  2. Create BP class in editor which derives from created cpp class.
  3. Put BP class into the scene/map.
  4. Create custom basic Component class in cpp.
  5. Add your created component in previously created actor class constructor. (OI.CreateDefaultSubobject)
  6. Recompile with editor open or closed.
  7. Check your previously placed BP Actor class. The component is visible, but is null in cpp code where you are trying to access it from another class. (Example: pickups)

Spawning actor dynamically works ok.
Placing the actor again in the map after recompiling solves the issue.

Hey ThePassenger,

For Step 7, can you clarify how you’re checking that the cpp code is null? Thanks!

I’m picking up actors from the scene and adding them to the character inventory and checking if the component is null. (no actor destroying, only hiding)

Thanks, but I meant the code you’re using to accomplish this. Or are you doing it in Blueprints?

I’m doing trace for nearby items in character blueprint and in cpp im just adding found Actor to the Array.

Whole hot reload feels like it was degraded, it can crash the engine if you change some function params or add a new function.

Hey ThePassenger-

Following the steps you provided this is what I did:

Create Actor subclass (MyActor)

Create BP based on MyActor (MyActorBP) and added an instance of the BP to the level

Create StaticMeshComponent subclass (MySubMesh)

In MyActor.h I included the header for MySubMesh and created a pointer variable (UMySubMesh* newMesh)

In MyActor.cpp’s constructor I added the function call:
newMesh = CreateDefaultSubobject(TEXT(“newMesh”));

After compiling, the instance of the BP showed the newMesh component in the Details panel. No mesh was set since I didn’t define a default mesh in the code. Is this the same as what you’re experiencing or is there something that I’ve done differently?

Cheers

Doug Wilson

Yes you did ok, the component is visible in the details panel, but now try to access placed Actor in Character cpp code and check if “UMySubMesh* newMesh” pointer is null.

I have the same problem but I’m not using blueprints. I placed all the objects in the editor and they were perfectly working. I added an ATextRenderComponent via code and all the instances loaded properly in editor, but when I play the game they are null. I load all the objects into an array and the array is null. I tried to clean the project and rebuild it but nothing has changed. If I crate a new instance is recognised and it is placed properly into the array.

Hey ThePassenger-

This was tested inside of a FirstPerson Template project. Using the following code inside the OnFire() function I was able to successfully “see” the newMesh component when I pressed the LMB in game:

TArray<AActor*> TestCubeActorArray;
	UGameplayStatics::GetAllActorsOfClass(this->GetWorld(), AMyActor::StaticClass(), TestCubeActorArray);
	if (TestCubeActorArray.Num() > 0)
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Black, TEXT("We got something!"));
		}
	}
	else
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("We failed to get anything."));
		}
	}

	AMyActor* TheActor = Cast<AMyActor>(TestCubeActorArray[0]);

	if (TheActor->newMesh)
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Cyan, TEXT("I can see the Actor's newMesh"));
		}
	}
	else
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Actor mesh? What actor mesh?"));
		}
	}

What this code does is get all actors of type MyActor and adds them to an actor array). After creating the array it takes all of the actors in the array (there’s only one that I’ve added to the level) and casts the first array element to AMyActor class. It then checks if it can see newMesh of that actor and prints out a message accordingly. Let me know if this helps at all or if you’re still having trouble accessing actor components from another class.

Cheers

Doug Wilson

Strange, i tried to reproduce this bug in new first person template cpp project and everything works ok.
My original project is created from Third Person BP template and then converted to cpp. Could this be the case?

I created a Third Person BP template project and used the same steps above to create an actor with a static mesh component. I then created a new class based on character with the code I posted in its tick function and made a blueprint based on this class. After removing the character that is placed in the level when it is created and changing the default pawn class to the blueprint I created I saw the same messages showing that it was able to recognize the static mesh in the actor.

I’ll mark this as solved for now and will post again if something similar happens.