[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 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. 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 actor again in map after recompiling solves issue.

Hey ,

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

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

Thanks, but I meant 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 Array.

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

Hey -

Following steps you provided this is what I did:

Create Actor subclass (MyActor)

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

Create StaticMeshComponent subclass (MySubMesh)

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

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

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

Cheers

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

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

Hey -

This was tested inside of a FirstPerson Template project. Using following code inside OnFire() function I was able to successfully “see” newMesh component when I pressed 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::, 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 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 array it takes all of actors in array (there’s only one that I’ve added to level) and casts 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

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 case?

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

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