Before i go on and post a tutorial on inventory. (custom inventory) Not using the ut inventory.
How many people actually require this?
This would be an RPG style inventory.
Tutorial.
First we start with the inventory manager which will extend Actor.\
This class will hold all o four stored items as well as make checks to see if you are full of that certain item.
U_InventoryManager.uc
Next we need to create the base abstract class that will be our inventory inside of the hud. You can extend this and create as many of these classes as you want.Code://============================================================================= // U_InventoryManager // // Manages all of the inventory in the game. performs checks it its full and // handles removing and adding inventory //============================================================================= class U_InventoryManager extends Actor; //Array of items in the inventory var array<U_Items> UItems; // Used to spawn and add item into inventory var U_Items AddItem; var int Gold; var int MaxGold; var UHUD HUD; simulated event PostBeginPlay() { super.PostBeginPlay(); `Log("Custom Inventory up"); //find the playercontroller and reference it StartingInventory(); } function AddGold(int GoldAmount) { if(Gold + GoldAmount >= MaxGold) { Gold = MaxGold; } else { Gold += GoldAmount; } } //@params ItemToCheck item specified in the U_WorldItemPickup class //@params AmountWantingToAdd the int amount that is specified in the U_WorldItemPickUp class // //returns false if there is space in the inventory returns true if there is not enough space. function bool CheckInventorySize(class<U_Items> ItemToCheck, int AmountWantingToAdd) { local int ItemAmountInInventory; local int i; for (i=0;i<UItems.Length;i++) { //When the iterator reaches a class that macthes the one that you want add it to itemamountininventory if (UItems[i].Class == ItemToCheck) { ItemAmountInInventory ++; `log(""@ItemToCheck$" Has"@ItemAmountInInventory$"Items"); } } if((ItemAmountInInventory + AmountWantingToAdd) >= 50) { ItemAmountInInventory = 0; `log("no more Space for items"); return true; } else { ItemAmountInInventory = 0; `log("has Space for items"); return false; } } //default stuff in the beggining of the game (you always have a herb on game start incase the player saves the game with low low health.) function StartingInventory() { AddInventory(class'U_HerbItem', 1); } //Add items to the current inventory function AddInventory(class<U_Items> ItemType, int Amount ) { local int i; for ( i=0; i<Amount; i++ ) { //Spawn the abstract item when the physical object is picked up and store it AddItem = Spawn(ItemType); UItems.AddItem(AddItem); } `log("There are" @ UItems.length @ "Items"); } //Remove items from the current inventory either when used or dropped. function RemoveInventory(class<U_Items> ItemToRemove, int Amount) { local int i; local U_Items Item; for (i=0;i<UItems.Length;i++) { //When the iterator reaches a class that macthes the one that you want to use or remove. // Remove it [i] and then use it. if (UItems[i].Class == ItemToRemove) { UItems.Remove(i,Amount); break; } } `log("Now there are only" @ UItems.length @ "Items!"); //Display the items left just for debugging. foreach UItems(Item, i) { `log("Index=" $ i @ "Value=" $ Item); } } function NotifyHUDMessage(string Message, optional int Amount, optional string ItemName) { foreach AllActors(class'UHUD', HUD) { HUD.Message = Message; HUD.TAmount = Amount; HUD.ItemName = ItemName; HUD.bItemPickedUp = true; } } defaultproperties { Gold = 500 MaxGold = 99999 }
Inside these classes can be functions that will heal the player or do something else that fits your needs. Right now they are empty classes.
U_Items.uc
This is a test extension of UItemsCode://============================================================================= // U_Items // // Parent Class of all Iventory items to be added in the InventoryManager // // This class in intereacted with in the Inventory manager as well as the MenuScene GUI // When the player selects the item in the GUI the HUD notifies the inventory manager and tells // the item in the array that is is bieng focused on. Making it read to be used and call remove inventory at any time // // Copyright 2011-2012 Jonathan Vazquez All Rights Reserved. //============================================================================= //class that holds the functions and uses of each item //These classes extending U_Items will be added once a physical pickup is picked up class U_Items extends Actor abstract;
Code:class U_HerbItem extends U_Items placeable;
This is a quick HUD to display our messages
UHUD.uc
In order to make use of the inventory manager you must add a few things to your pawnCode:class UHUD extends UDKHUD; //message pickup properties var string Message; var int TAmount; var string ItemName; var float Alpha; var bool bItemPickedUp; var Font PlayerFont; event PostRender() { HandlePickUpMessage(); Super.PostRender(); } //Shows the picked up item as well as handle fading function HandlePickUpMessage() { local float RenderTime; local float X,Y,UL,VL; Canvas.Font = PlayerFont; Canvas.TextSize(""@Message$" "@TAmount$" "@ItemName$"(s)",UL,VL); X = (SizeX / 2) - (UL/2); Y = (200) - (VL/2); Canvas.SetPos( X, Y); // Set the cursor color Canvas.SetDrawColor(255, 255, 255, Alpha); Canvas.DrawText(""@Message$" "@TAmount$" "@ItemName$"(s)"); if(bItemPickedUp == true) { if(RenderTime == 0) RenderTime = WorldInfo.TimeSeconds; } if(`TimeSince(RenderTime) > 1.0) { Alpha = Lerp(Alpha, 0 , 0.05); RenderTime -= 0.1; } else { RenderTime = 0; Alpha = 255; bItemPickedUp = false; } }
Code:var class<U_InventoryManager> UInventory; var repnotify U_InventoryManager UManager; simulated event PostBeginPlay() { super.PostBeginPlay(); `Log("Custom Pawn up"); //Set up custom inventory manager if (UInventory != None) { UManager = Spawn(UInventory, Self); if ( UManager == None ) `log("Warning! Couldn't spawn InventoryManager" @ UInventory @ "for" @ Self @ GetHumanReadableName() ); } } defaulproperties { bCanPickUpInventory = true UInventory = U_InventoryManager }
Now we need to create the actual pickup that adds the abstract classes into the inventory. These classes can play sounds on pick up display a message etc.
You simply choose what item you want to add from a dropdown menu so it saves you from creating a million pickup classes.
U_WorldItemPickup.uc
Note : Make sure you replace UHeroPawn with your pawn. Or any other un - matching classes with yours.
Code:class U_WorldItemPickup extends Actor placeable; var() editconst const CylinderComponent CylinderComponent; var() const editconst DynamicLightEnvironmentComponent LightEnvironment; //the physical pickup that is seen in the world as a "Bag" which will contain any kind of inventory and a certain amount. var U_InventoryManager InvManager; var() string ItemName; var() string PickupMessage; // Human readable description when picked up. var() SoundCue PickupSound; var() class<U_Items> ItemType; var() int ItemAmount_ADD; var UHeroPawn UHP; event Touch(Actor Other, PrimitiveComponent OtherComp, Vector HitLocation, Vector HitNormal) { super.Touch(Other, OtherComp, HitLocation, HitNormal); UHP = UHeroPawn(Other); if (UHP != none) { //If the inventory check returns false (has space) add the item. if(!(UHP.UManager.CheckInventorySize(ItemType, ItemAmount_ADD))) { //Add the picked up item GiveTo(UHP); PlaySound(PickUpSound); UHP.UManager. NotifyHUDMessage(PickUpMessage, ItemAmount_ADD, ItemName); } } } //Add to pawns inventory once picked up //If you still have space add it to the inventory function GiveTo( UHeroPawn Other ) { if ( Other != None && Other.UManager != None ) { Other.UManager.AddInventory(ItemType, ItemAmount_ADD ); Destroy(); } } defaultproperties { Begin Object Class=DynamicLightEnvironmentComponent Name=MyLightEnvironment bEnabled=TRUE End Object LightEnvironment=MyLightEnvironment Components.Add(MyLightEnvironment) Begin Object class=StaticMeshComponent Name=BaseMesh StaticMesh=None //StaticMesh'Items.Items.ItemBag' Add your own staticmesh Scale = 3.5 LightEnvironment=MyLightEnvironment End Object Components.Add(BaseMesh) Begin Object Class=CylinderComponent NAME=CollisionCylinder CollideActors=true CollisionRadius=+0040.000000 CollisionHeight=+0040.000000 bAlwaysRenderIfSelected=true End Object CollisionComponent=CollisionCylinder CylinderComponent=CollisionCylinder Components.Add(CollisionCylinder) bHidden=false bCollideActors=true }
That should be it. It i missed anything let me know!






Reply With Quote





Bookmarks