I am continuing the Battery Collector tutorial. This is the 11th video. Click here for the first part and here for the previous one.
Adding Power to the Game
Battery power
In the BatteryPickup.h header file, I am adding a protected section and I am declaring a BatteryPower property. This will define how much power the battery gives to the character:
protected: /** Set the amount of power the battery gives to the character */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true")) float BatteryPower;
It is declared as BlueprintProtected so that only inherited blueprint can access it.
I am also adding the public accessor function:
/** Public way to access the battery's power level */ float GetPower();
Then, in the BatteryPickup.cpp file, I set the default value for this property in the constructor:
// the base power level of the battery BatteryPower = 150.f;
I am also defining the accessor:
// report the power level of the battery float ABatteryPickup::GetPower() { return BatteryPower; }
Back to the editor, I am compiling the project. When I open the Battery_BP blueprint, I can see the property:
I can even modify it and set it to 200 for example.
Character Power
Now let’s define the power the character. In the BatteryCollectorCharacter.h, I am declaring the InitialPower as a protected property:
/** The starting power level of our character */ UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Power") float InitialPower;
I am also declaring the current CharacterPower, but this time as a private property. I set it as VisibleAnywhere to be able to see its current value:
private: /** Current power level of our character */ UPROPERTY(VisibleAnywhere, Category = "Power") float CharacterPower;
In the BatteryCollectorCharacter.cpp file, I am defining the default values at the bottom of the constructor function:
// set a base power level for the character InitialPower = 2000.f; CharacterPower = InitialPower;
In the public section of BatteryCollectorCharacter.h, I am declaring functions to access InitialPower and CurrentPower, as well as a function to modify the currentPower:
/** Accessor function for initial power */ UFUNCTION(BlueprintPure, category = "Power") float GetInitialPower(); /** Accessor function for current power */ UFUNCTION(BlueprintPure, category = "Power") float GetCurrentPower(); /** Function to update the character's power * @param PowerChange This is the amount to change the power by, and it can be positive or negative. */ UFUNCTION(BlueprintCallable, Category = "Power") void UpdatePower(float PowerChange);
The accessor functions are BlueprintPure, since they won’t modify the value. The UpdatePower function is BlueprintCallable because it can modify the state of the class.
In the BatteryCollectorCharacter.cpp file, I am defining these functions:
// reports starting power float ABatteryCollectorCharacter::GetInitialPower() { return InitialPower; } // reports current power float ABatteryCollectorCharacter::GetCurrentPower() { return CharacterPower; } void ABatteryCollectorCharacter::UpdatePower(float PowerChange) { CharacterPower = CharacterPower + PowerChange; }
Back to the editor, I am compiling the project. In the ThirdPersonCharacter blueprint, if I search Power, I can see both values and modify only the Initial Power:
Next Step
In the next episode, we will power up the character.
Leave a Reply