OnConstruction functionality not persisting after project restart

Issue:

C++ OnConstruction functionality does not work after you restart your project. Unless you cut and paste the function out, recompile the project, and paste it back in and recompile.

Expected:

OnConstruction functionality will still work on project restart.

Reproducibility:

5/5 times.

**Steps to reproduce: (Contains code fragments, will require some extra work on reproducers side) **

  1. Declare editable boolean e.g.:

 	UPROPERTY(EditAnywhere, Category = MyCategory)
	 bool bMyBool;
  1. Create custom C++ Actor and declare/implement OnConstruction function e.g.:

void AMyActor::OnConstruction(const FTransform &Transform)
{
	Super::OnConstruction(Transform);
        if(bMyBool)
            myStaticMesh->SetMaterial(0, myMaterial1);
        else
             myStaticMesh->SetMaterial(0, myMaterial2);
}
  1. Place custom actor in UnrealEd level. Set bMyBool to both true and false. Notice the material of the Static Mesh changes.

  2. Save and restart project. Repeat step 3, notice the material no long changes i.e. construction functionality does not work.

  3. Cut the OnConstruction function out of the custom actor class. Recompile project. Paste the OnConstruction function back in. Recompile project.

Notice it now works as expected.


If you have any queries or this is confirmed to be a bug let me know :slight_smile:

Cheers,

Josh Caratelli

Hey -

Rather than setting up materials I used the following if/else statement to print to the Output Log when OnConstruction is called.

if (bMyBool)
{
	UE_LOG(LogTemp, Log, TEXT("Bool is True"));
}
else
	UE_LOG(LogTemp, Log, TEXT("Bool is False"));

After saving and reopening the project I opened the Output Log before adding a new actor or moving the one already placed and found that they were both updating in the output as I moved them around the level. How do you have the OnConstruction function declared in your header file? Additionally, if you could show how you setup myStaticMesh, myMaterial1, and myMaterial2 I can test with the same setup that you’re using.

Cheers

Hey ,

Thanks for getting back to me so quickly. The last couple of days the issue had stopped, and I was trying to replicate it before I got back to you. Then I realized what was going on. It turns out it’s a bug of the user error (me!) kind.

I hadn’t realized that when you recompiled your code, that any objects that were already present in your level wouldn’t have updated OnConstruction functionality.

So for e.g. I had AMyActor present in my level with no OnConstruction functionality. Then I added it in and recompiled. The object that was already present in the level still has no OnConstruction functionality, until I replaced it.

I’m not sure whether it should be updated or not is a bug, for this issue I’ll mark it as resolved :slight_smile: