Actors seem to be influencing other actors of same type.

I have a platform I place in my level and is supposed to pulse the emissive glow in a material during Tick(). This all works 100% fine, except for when I have more than one placed in the level at a time. Once I add another platform to my level the pulse on both of them is very erratic. I know I can do this in the material itself which I will probably end up doing. But what is it about my code that the actors are influencing each other? I’d rather understand my issue than just apply a bandaid and run into the problem again later.

.h



UPROPERTY(EditAnywhere, Category = "PlatformSettings")
FLinearColor GlowColor = FLinearColor(1.f, 0.f, 0.f, 1.f);

UPROPERTY(EditAnywhere, Category = "PlatformSettings", meta = (ClampMin = "0.0", ClampMax = "1000.0", UIMin = "0.0", UIMax = "1000.0"))
float GlowIntensity = 225.f;


.cpp



AMainPlatform::AMainPlatform()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    USceneComponent* RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
    RootComponent = RootComp;

    PlatformMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlatformMesh"));
    PlatformMesh->SetupAttachment(RootComponent);
    static ConstructorHelpers::FObjectFinder<UStaticMesh> PlatformVisualAsset(TEXT("/Game/Discrimmage/StaticMesh/SM_MainPlatform.SM_MainPlatform"));
    if (PlatformVisualAsset.Succeeded())
    {
        PlatformMesh->SetStaticMesh(PlatformVisualAsset.Object);
    }

}

void AMainPlatform::OnConstruction(const FTransform& Transform)
{
    if (PlatformGlowMatInstance == nullptr)
    {
        PlatformGlowMatInstance = PlatformMesh->CreateDynamicMaterialInstance(1, PlatformMesh->GetMaterial(1), NAME_None);
    }
    PlatformGlowMatInstance->SetVectorParameterValue(FName("Color"), GlowColor);
    PlatformGlowMatInstance->SetScalarParameterValue(FName("Intensity"), GlowIntensity);
}

// Called every frame
void AMainPlatform::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    float GlowPulse = 0.f;
    static float PulseDirection;
    if (PulseDirection == 0.f)
    {
        PulseDirection = 1.f;
    }

    float CurrentIntensity;
    PlatformGlowMatInstance->GetScalarParameterValue(FName("Intensity"), CurrentIntensity);

    if (CurrentIntensity >= GlowIntensity * 1.5f || CurrentIntensity <= GlowIntensity * 0.5f)
    {
        PulseDirection *= -1;
    }

    GlowPulse += DeltaTime * GlowIntensity * PulseDirection;

    PlatformGlowMatInstance->SetScalarParameterValue(FName("Intensity"), CurrentIntensity + GlowPulse);
}




Quick video to show the behavior.



static float PulseDirection;


is what is causing the issue.

Static variables are like globals. Imagine you made a global variable called



float g_AMainPlatform_Tick_PulseDirection;


But that global can only be accessed inside the Tick function.

No matter what instance accesses it the value same value is passed to every instance.

https://www.cprogramming.com/tutoria…ickeyword.html

Also worth noting that a simple pulsing effect can be handled very easily by the material itself. There’s no need to tick an actor purely to pulse a material.

@TheJamsh

I appreciate the input, you can see from my original post I knew I could do that. This was mostly just, “why does my code behave this way?” type question. Which I also knew how static variables behaved, I just didn’t put two and two together for some reason. I guess I was scatter brained from all the frustration of MAKING GAMES! lol

I also realize it’s almost a year later, but meh, I’ve got a FTJ and 3 kiddos. It takes me a while to get back to things sometimes, haha.