I am following the video tutorial Battery Collector for Unreal Engine. The previous steps can be read here.
Adding Variables and Functions
In Pickup.h, I add the protected variable bIsActive:
protected: /** True when the pickup cab be used, and false when pickup is deactivated */ bool bIsActive;
I set it to true in the constructor In Pickup.c:
// All pickups start active bIsActive = true;
In the public section of Pickup.h, I declare getter and setter functions for this variable:
UFUNCTION(BlueprintPure, Category = "Pickup") bool IsActive(); UFUNCTION(BlueprintCallable, Category = "Pickup") void SetActive(bool NewPickupState);
The difference between BlueprintPure and BlueprintCallable is described in the “Blueprints, Creating C++ Functions as new Blueprint Nodes” wiki and in the Function documentation of Unreal Engine. To summarize, BlueprintPure functions are more efficient, but must not modify the state of the class. They are good for getter functions.
In Pickup.cpp, I add the definitions for these functions:
// Returns active state bool APickup::IsActive() { return bIsActive; } // Changes active state void APickup::SetActive(bool NewPickupState) { bIsActive = NewPickupState; }
Back to the Unreal Engine Editor, I create a new folder named “Blueprints” under Content and I create a Blueprint class based on Pickup by right-clicking on the C++ Pickup object and selecting that option. I name it “Pickup_BP” and put it under Blueprints:
Opening the new blueprint, I can access the data, and open the full blueprint editor:
In the blueprint editor, it is possible to access the functions defined in cpp under the category Pickup:
Next Steps
In the next episode, I will extend the Pickup class.
Leave a Reply