Been working on an RPG for a while, and am now looking at the item upgrade mechanics. My original design was that Item A-I can be promoted to Item A-II by using accessory B. Which is fine as a simple mechanic. In pseudo-code
(The _upgrades fields is populated with item types (they’re enums) in the inspector.) But I do want a bit more depth in there.
So I have a container class, and although some weapons / accessories can be upgraded with a container, I also need to have the test for what the container contains. Clearly a container type for every possible content is silly. And I can do the check simply in code. But is there a better - more data-driven - way to check not just the type of the item, but some attribute of the item as well? It sounds like invoking a ton of reflection, or writing a meta-language to describe these things and I wonder if it’s worth the effort?
That’s exactly what I have. I have an enum that describes each type of object in the game (yuk, I know this is generally bad practice, but…).
So there is an array of upgrade items and the level they become applicable at. They transform the item (in this case a piece of rail gun ammo) into another item. But notice that the Storage Bottle isn’t enough in itself. The result of the upgrade will depend on what’s in the bottle. Not all upgrades are like this though. Some just require an object on its own. In my code I do this sort of thing:
protected override void applyUpgradeInner(Item upgradeItem)
{
if (upgradeItem.SpecificType == InstanceType.StorageBottle) {
var bottle = (StorageBottle)upgradeItem;
if (bottle.Contents == ParticleType.Neutron) {
// The only way to get neutronium is to have neutrons.
Payload = RailGunPayload.Neutronium;
}
}
}
but I’d ideally like this to be in the ScriptableObject inspector interface. So I’m looking for a relatively simple, elegant way of representing the upgrade system that minimises the amount of bespoke code I need to write. Does that make sense?