Weapon modification

Hello everyone!

A question a little bit out of the scope of this course.

Does someone tried to create a weapon modification system? I mean like in Diablo 2 where we are able to put some runes in the weapons? Or, in more modern games to add a scope to a rifle?

If some one has some advices or tutorial which are compatible with this course it would be great.

Thank you in advance.

I edited the question a bit for clarity, the word you’re looking for is runes. (English is weird).

I’ll start this off with the rough outline of what we would need to do. First, a quick reminder for those who may not be familiar with how Diablo II’s runes and runewords work (Note: This is from researching, I don’t actually have Diablo II, but I have a good idea of how the basic mechanic works).

Weapons and Equipment have sockets. Each socket can take a rune. The runes, by themselves, have a certain bonus to stats/traits. So if you have a rune with a +2 Attack, then socketing that rune onto your sword will give you +2 to attack.

For now, I’m not going to focus on Runewords, but it’s something that can be explored later.

In it’s simplest form, each item has a certain number of sockets that can take a rune. For now, the best place to put these sockets is in EquipableItem. We’ll only be utilizing the sockets in StatsEquipableItem and WeaponConfig, but since both of these share the common ancestor of EquipableItem, you can ensure that they’re both handled the same.

First, within the namespace declaration in EquipableItem.cs:

public class Socket
{
     public InventoryItem Item;
}

Then within EquipableItem itself:

[SerializeField] private int numberOfSockets=0;

Dictionary<int, Socket> sockets = new Dictionary<int, Socket>();

public int NumberOfSockets =>numberOfSockets;
public Socket GetSocket(int socket)
{
    return sockets.ContainsKey(socket)? sockets[socket]: null;
}

public Socket RemoveSocket(int socket)
{
    Socket result = GetSocket(socket);
    if(result!=null) sockets.Remove(socket);
    return result;
}

        public bool TrySocketItem(int socket, InventoryItem item)
        {
            if (socket < 0 || socket >= numberOfSockets || sockets.ContainsKey(socket)) return false;
            Socket itemSocket = new Socket();
            itemSocket.Item = item;
            sockets[socket] = itemSocket;
            return true;
        }

Finally, we’ll need a way of saving the state of EquipableItem… Because the item could be in our Inventory, which deals with InventoryItems, we’ll need to make a change to InventoryItem

public virtual object CaptureState()
{
    return null;
}
public virtual void RestoreState(object state)
{
}

Now, in EquipableItem, we can add a CaptureState and RestoreState(), but they’ll need to be overrides

        public override object CaptureState()
        {
            Dictionary<int, string> stateDict = new Dictionary<int, string>();
            foreach (KeyValuePair<int,Socket> pair in sockets)
            {
                stateDict[pair.Key] = pair.Value.Item.GetItemID();
            }
            return stateDict;
        }

        public override void RestoreState(object state)
        {
            if (state is Dictionary<int, string> stateDict)
            {
                sockets.Clear();
                foreach (KeyValuePair<int,string> pair in stateDict)
                {
                    TrySocketItem(pair.Key, InventoryItem.GetFromID(pair.Value));
                }
            }
        }

That’s what I have for tonight… tomorrow, I’ll go over the items we’ll be socketing, as well as how we’ll get the stats/traits from those items.

2 Likes

Thank you very much Brian! You are great help as usual. I will try this code.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms