Hello,
I have a base class called PowerUp where I use a dynamic material instance to change the base color in derived classes, such as GrowthPowerUp. To do so, in the constructor of the derived classes I simply call a function I defined in the base class, PowerUp::SetBaseColor()
When I compile the code, everything seems to be as expected: in the content browser, I can see that PowerUp and GrowthPowerUp classes have the same shape but different base colors, and also when I include them in the level:
However, as soon as I recompile the C++ project, the different color in GrowthPowerUp does not appear anymore in the editor, although it is still visible in the content browser:
The relevant code in the header and source files is shown below. I don’t know if I am doing something wrong to set the base color in the derived classes, but nevertheless the change in visualization before and after recompiling must be some sort of bug in the level editor. When the GrowthPowerUp instances are spawned during the game with GetWorld()->SpawnActor(), they appear with the expected color again.
I am using unreal engine 4.7.6 in Windows 7 from the Epic Games Launcher, specifically verison 4.7.6-2513093+++depot+UE4-Releases+4.7
PowerUp.h:
#pragma once
#include "GameFramework/Actor.h"
#include "PowerUp.generated.h"
UCLASS()
class SNAKE_API APowerUp : public AActor
{
GENERATED_BODY()
protected:
/* BodyMesh component for the clickable block */
UPROPERTY(Category = Body, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* BodyMesh;
public:
// Sets default values for this actor's properties
APowerUp();
UFUNCTION()
void SetBaseColor(const FLinearColor BaseColor);
};
PowerUp.cpp:
#include "Snake.h"
#include "PowerUp.h"
#include "GrowthPowerUp.h"
// Sets default values
APowerUp::APowerUp()
{
// Structure to hold one-time initialization
struct FConstructorStatics
{
ConstructorHelpers::FObjectFinderOptional<UStaticMesh> PowerUpMesh;
FConstructorStatics()
: PowerUpMesh(TEXT("/Game/Meshes/PowerUp.PowerUp"))
{
}
};
static FConstructorStatics ConstructorStatics;
// Create static mesh component
BodyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PowerUp0"));
BodyMesh->SetStaticMesh(ConstructorStatics.PowerUpMesh.Get());
BodyMesh->SetRelativeLocation(FVector(0.f, 0.f, 0.f));
BodyMesh->SetRelativeScale3D(FVector(1.f, 1.f, 1.f));
BodyMesh->CreateAndSetMaterialInstanceDynamic(0);
BodyMesh->SetCollisionProfileName("PowerUp");
BodyMesh->bGenerateOverlapEvents = true;
RootComponent = BodyMesh;
}
void APowerUp::SetBaseColor(const FLinearColor BaseColor)
{
UMaterialInstanceDynamic* Material = (UMaterialInstanceDynamic*)BodyMesh->GetMaterial(0);
Material->SetVectorParameterValue(FName(TEXT("BaseColor")), BaseColor);
}
GrowthPowerUp.h:
#include "PowerUp.h"
#include "GrowthPowerUp.generated.h"
UCLASS()
class SNAKE_API AGrowthPowerUp : public APowerUp
{
GENERATED_BODY()
public:
AGrowthPowerUp();
//The amount of body parts that this PowerUp will make the snake increase
uint32 DeltaSize;
};
GrowthPowerUp.cpp:
#include "Snake.h"
#include "GrowthPowerUp.h"
#include "SnakeHead.h"
AGrowthPowerUp::AGrowthPowerUp()
: Super()
{
SetBaseColor(FLinearColor(0.9f, 0.1f, 0.1f));
DeltaSize = 5;
}
Material definition: