Hi, I’ve been using ue4 for a while and finally decided to learn C++, I want a Hitman-Game like inventory where you have a static inventory, button 1 is a gun, button 2 a tranq gun etc. Just a standard non-dynamic inventory.
Can someone push me the right way or send me a tutorial series that kinda covers it?
Thank you for your time!
Since you want to have specific slots on your character for each type of item, you should take the following steps:
-
Create a project using the UE wizard. Select game and then select the Third Person template. Set your project type to C++.
-
Using the Generate C++ Class option (File->Generate C++ Class…), create an AActor class for your inventory items. To keep things simple, let’s call it AItem.Using the Generate C++ Class option (File->Generate C++ Class…), create an AActor class for your inventory items. To keep things simple, let’s call it AItem.
-
Now create subclasses of AItem for each type of item you wish to have a slot for. To keep things simple and in line with what you’re thinking, call your AItem subclasses AGun, ATranquilizer, AMelee, and ABomb.
-
In ProjectNameHereCharacter.h, inside the class declaration, define fields and methods for each item as follows:
UPROPERTY(BlueprintReadWrite, Category = "Inventory")
AGun* Gun;
UPROPERTY(BlueprintReadWrite, Category = "Inventory")
ATranquilizer* Tranquilizer;
UPROPERTY(BlueprintReadWrite, Category = "Inventory")
AMelee* Meelee;
UPROPERTY(BlueprintReadWrite, Category = "Inventory")
ABomb* Bomb;
UPROPERTY(BlueprintReadOnly, Category = "Inventory")
AItem* Item;
UFUNCTION(BlueprintCallable, Category = "Inventory")
void EquipGun();
UFUNCTION(BlueprintCallable, Category = "Inventory")
void EquipTranquilizer();
UFUNCTION(BlueprintCallable, Category = "Inventory")
void EquipMelee();
UFUNCTION(BlueprintCallable, Category = "Inventory")
void EquipBomb();
-
In your Unreal project, use Edit->ProjectSettings… to create inputs as detailed here to bind each of your item types to a particular key.
-
In ProjectNameHereCharacter.cpp, implement the UFUNCTION methods described in step 4. Use the following as a guide:
void ProjectNameHereCharacter::EquipGun()
{
if (Gun != NULL)
{
Item = Gun;
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT(“No gun available!”));
}
}
-
Inside SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent), bind the axes you created in step 5 to the functions that you defined in step 6. Use the existing generated code as a guide.
Once you have done all of the above, create blueprints for each item type, subclassing them appropriately. You can then use Tom Looman’s usable actor tutorial to build a pickup system. You can then try to figure out for yourself how to use item pickups to set your items by category. That’s a challenge for you to try for yourself.