Download

Member variables not maintaining set values

I’m having a problem with getting member variables to maintain the values that I set them to. I have a data asset that contains all the data for my weapons, this includes a weapon component class that tracks the status of the trigger and fires when appropriate. At run time a pawn class then loads the appropriate entry from the weapon assets and creates a new weapon compenent pointer from the one stored in the weapon asset, it then uses that pointer to update the trigger status. The update functions work as they should updating the status of trigger in the weapon component class, but as soon as the function goes out of scope the trigger status reverts back to false meaning that the trigger is never true and never fires.

Weapon Asset Class


USTRUCT(BlueprintType)
struct FWeaponBlueprint
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, Category=WeaponMeshes)
    UStaticMesh* WeaponBody = nullptr;
    UPROPERTY(EditAnywhere, Category=WeaponMeshes)
    UStaticMesh* WeaponBase = nullptr;
    UPROPERTY(EditAnywhere, Category=WeaponMeshes)
    UStaticMesh* WeaponGun = nullptr;
    UPROPERTY(EditAnywhere, Category=WeaponMeshes)
    UStaticMesh* WeaponBarrel = nullptr;

    UPROPERTY(EditAnywhere, Category=WeaponData)
    FVector EndOfBarrel;
    UPROPERTY(EditAnywhere, Category=WeaponData)
    TSubclassOf<UHonkWeaponComponent> FiringMechanism;

    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float RPM;
    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float TurnRate;
    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float ProjectileSpeed;
    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float Damage;
    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float Range;
    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float ChargeSpeed;
    UPROPERTY(EditAnywhere, Category=WeaponStats)
    float ExplosionRange;

};

UCLASS(BlueprintType)
class HONK_API UHonkWeaponAsset : public UDataAsset
{
    GENERATED_BODY()

public:
    UPROPERTY(EditAnywhere)
    TMap<FName, FWeaponBlueprint> Weapons;
};

Pawn Class
Weapon Component from the data asset and pointer to current instance


UPROPERTY(VisibleAnywhere)
TSubclassOf<UHonkWeaponComponent> WeaponComp = nullptr;
UPROPERTY()
UHonkWeaponComponent* WeaponInstance = nullptr;

Accessing and creating the new instance of Weapon Component


WeaponComp = WeaponAsset->Weapons[weapon].FiringMechanism;
WeaponInstance = NewObject<UHonkWeaponComponent>(*this*, WeaponComp);


Weapon Component Trigger set and Tick function


void UHonkWeaponComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    *if* (Firing && LastFired <= 0.0f)
    {
        UE_LOG(LogTemp, Warning, TEXT("This function needs overriding"));
        LastFired = FireRate;
    }

    *if*(LastFired > 0.0f)
    {
        LastFired -= DeltaTime;
    }
}

void UHonkWeaponComponent::SetTriggerStatus(bool status)
{
    *this*->Firing = status;
}

Any help is appreciated.
Thanks

It’s difficult to read the code because it’s all colourised for some reason.

The first thing to point out though is that you need to call RegisterComponent() once it has been created, if you don’t it won’t initialize properly. It won’t tick, it won’t replicate, it won’t create render/physics states etc etc.



WeaponInstance = NewObject<UHonkWeaponComponent>(this, WeaponComp);
check(WeaponInstance);

WeaponInstance->RegisterComponent();


Hi,

I didn’t realise the colouring was not default. I copied directly from my VS Code file so maybe something about how VS Code encodes/displays it’s text.
As for the fix, this worked perfectly.

Many Thanks