简体   繁体   中英

How do I go about saving multiple modified prefabs instantiated from the same scriptable object C#

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM