3

Work with me now, I'm a confused lost little child at this point.

Intro

I have an inventory that allows me to place items into a gear slot, instantiating that item in my players hand/ on body. For example, I have a simple rifle, I put it in my gear slot and it is created. My player can now run around shoot, kill, and unequip it too! BUUUT I can not figure out how to save my modified variables.

Problem Lore

All my items are Scriptable Objects while in the inventory, so I can easily create different items. The Scriptable Object holds; some text data, other things, and the actual prefab of the weapon I want to instantiate. The problem is, when I unequip the item from the gear slot it deletes the prefab, as it should, I don't want to see or use it anymore while in game. I can easily create an upgrade system, but saving those changed variables is a problem. I'm deleting it when I unequip it and instantiating a new copy when I equip it. My game allows the player to pickup the same weapon until the inventory is full too.

Overall Problems

  1. How do I go about saving multiple modified prefabs instantiated from the same scriptable object?

  2. Should I figure out how to create a unique Id that represents the weapon and allows the scriptable object to instantiate this unique Id?

I'm not sure if the second question is possible, but I think you might get the gist of the problem, any solutions are helpful, if I should recreate my inventory, I'd cry for sure, but I really want a weapon upgrade system in my game, so I'LL HECKIN DO IT! Thank you guys.

  • 1
    Hm, I guess in that case you can use strategy pattern, so you will change, for example "sword handle" or "sword blade" without changing the entire entity and creation full new object, instead you will create new property for this variable, will that help you? – Red Star Jul 30 '22 at 16:47
  • @RedStar Interesting, going off what you are saying, I figure it would be better to redesign my inventory. I came up with a system I like now and I owe you one for that. Thank you much! – Brandon Rose Jul 31 '22 at 15:53

1 Answers1

0

Problem

I will have a lot of various classes, which is very similar("simple sword", "diamond sword", "giga sword"...)

Solution

Strategy pattern, instead of creation whole new object, i will change the property of this object

Example

interface IHandle
{
    public int Speed { get; set; }
}
class SimpleHandle : IHandle
{
    public int Speed { get; set; }

    public SimpleHandle()
    {
        Speed = 5;
    }
}
interface IBlade
{
    public int Damage { get; set; }
}
class SimpleBlade : IBlade
{
    public int Damage { get; set; }

    public SimpleBlade()
    {
        Damage = 5;
    }
}
class Sword
{
    private IHandle _handle { get; set; }
    private IBlade _blade { get; set; }

    public void ChangeHandle(IHandle handle)
    {
        _handle = handle;
    }
    public void ChangeBlade(IBlade blade)
    {
        _blade = blade;
    }
}
Red Star
  • 556
  • 3
  • 13