Problem using one variable as the value of another

I’m trying to implement an ammo and reload method onto my weapon class but when using variables to assign the starting ammo for test purposes they always have a value of 0 in the debugger, the break point is in the fire method that preforms an ammo check.

Everything else works and if I assign them a value manually it works.
The variables I set in blueprints are correct in the debugger.

.h file ---------------------------------------------

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="WeaponSettings")
	int32 MagSize;
	int32 CurrentAmmo = 0;

	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="WeaponSettings")
	int32 MaxMags;
	int32 MagAmmo = 0;
	int32 MaxAmmo;

.cpp file --------------------------------------------

    MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
    RootComponent = MeshComp;

    MuzzleSocketName = "MuzzleFlashSocket";
    TracerTargetName = "Target";

    MaxAmmo = MagSize * MaxMags;

    CurrentAmmo += MaxAmmo;
    MagAmmo += MagSize;

Hey Calaban.

In your constructor, your variables are not initialized yet. so they are all still 0.
So: MaxAmmo = MagSize * MaxMags;
is: MaxAmmo = 0 * 0;

Somewhere you need to give them a value before using them.

Perhaps doing those calculation in the BeginPlay(), and not in the ctor. That way, both the child and the parent ctors will have already been executed and the values read.

Hey , Thanks for answering. The values were already declared in blueprints, I should have been more clear.

I created a method in SWeapon, SetAmmo and put all the calculations in there and had it called from Character class where I spawn the gun.
(Thanks )

Yes but a constructor is always run before the blueprint can set the variables.
To be sure all the variables are set, you can run your code in BeginPlay().

If you want to test it you can add this code to your constructor.

UE_LOG(LogTemp, Warning, TEXT("ASWeapon: MagSize: %d"), MagSize);

This is because the Constructor is part of how c++ works. Unreal Engine has no control over it.

When you instantiate a new variable in c++ It will run the constructor right away. (Even if you yourself did not make one.) And it will always to this.

Thanks, I didn’t know Blueprints set the variables after the constructor. This basically answer’s my question.