- Home /
Items Changing Player Stats, Backpack increasing Inventory Size
I was just curious how this can be done. In a game like PUBG for example, when you pick up a backpack, the amount of items that one can carry will increase. I am having a bit of trouble wrapping my head around how to implement this concept.
Would you have a function that gets run whenever a specific backpack is picked up, or would you just have the backpack item edit the inventory system itself?
This item would need to act similarly to a power up no?
Answer by Sonky108 · Jun 26, 2018 at 07:45 AM
I guess it can be done in many ways. For example you can start with having base backpack class:
public abstract class Backpack
{
private int maximumWeight;
private List<Item> items;
public Backpack(List<Item> items = null)
{
this.items = new List<Item>();
this.items = items;
}
}
The you can add three class (one per tier):
public class SmallBackpack : Backpack
{
maximumWieght = 10;
}
public class NormalBackpack : Backpack
{
maximumWeight = 15;
}
public class BigBackpack : Backpack
{
maximumWeight = 20;
}
Some class containing information about player should have field and a method:
private Backpack playerBackpack;
public void ChangeBackpack(BackpackType newBackpackType)
{
switch(newBackpackType)
{
case(small):
playerBackpack = new SmallBackpack(playerBackpack.items); //it will move current items to new backpack
break;
case(normal):
playerBackpack = new NormalBackpack(playerBackpack.items);
break;
case(big):
playerBackpack = new BigBackpack(playerBackpack.items);
break;
}
}
After all another class, for example WorldItem can call Player.ChangeBackpack(BackpackType).
I've missed some null checks, correctness and in my opinion WorldItem calling method on player is basically bad idea, but I hope you get the point :)
I would give you more points for this QUALITY of answer, but I only have 6.