I’m relatively new to gamedev, Unity, and ScriptableObjects, and I’m trying to figure out if my current inventory implementation, using ScriptableObjects, is the “correct” approach, or if I’m missing something and should be doing it a better way.
TLDR version: I have two concerns, one is that I want some items to have randomized attributes and I’m not sure that creating a bunch of SO assets at runtime, during gameplay, is ok. Second concern is more nuanced, but I have some SO assets with a List, and objects are added and removed from this list during gameplay - will I have issues saving/serializing the state of this List?
My game really only has two types of equipment or items that I’m concerned about here: weapons and “mods” that can be equipped to slots in the weapons. They are each currently implemented as ScriptableObjects.
For example, my Weapon SO class (some irrelevant code removed to keep it shorter):
[CreateAssetMenu]
public class Weapon : ScriptableObject
{
public Sprite weaponSprite;
public GameObject projectile;
public string weaponName;
[Space]
public float damage;
public float range;
public int energyCost;
public float accuracy;
public float projectileForce;
public float firingCooldown;
public float numberOfProjectiles;
[Space]
public List<WeaponMod> equippedMods;
public void Equip(WeaponInventoryManager wim)
{
// code to equip weapon
// code to ensure any mods of this weapon are also re-equipped from the List of equippedMods
}
}
And here is the code for my weapon mod SO class (some irrelevant code removed to keep it shorter):
[CreateAssetMenu]
public class WeaponMod : ScriptableObject
{
public Sprite modSprite;
public string modName;
public float damageModifier;
public float rangeModifier;
public float energyCostModifier;
public float accuracyModifier;
public float forceModifier;
public float cooldownModifier;
public int numProjectilesModifier;
public void Equip(ModInventoryManager mim)
{
// tell the equipped Weapon that this mod is equipped
mim.playerStats.equippedWeapon.equippedMods.Add(this);
// update the player's modifier stats based on this mod's values
}
// this version of Equip is for when a weapon with mods is re-equipped
public void EquipWithWeapon(WeaponInventoryManager wim)
{
// update the player's modifier stats based on this mod's values
}
public void UnEquip(ModInventoryManager mim)
{
// tell the equipped Weapon that this mod is unequipped
mim.playerStats.equippedWeapon.equippedMods.Remove(this);
// update the player's modifier stats based on this mod's values
}
// this version of UnEquip is called when the weapon is unequipped
public void UnEquipWithWeapon(WeaponInventoryManager wim)
{
// update the player's modifier stats based on this mod's values
}
}
There will only ever be 5 or 6 weapons in the game. The only thing about the weapons that changes is the List of equippedMods - the player can add or remove mods to each weapon during gameplay.
There will be a few unique weapon mods, but many of them will be randomly generated during gameplay to have different attribute values.
My code is working with the two weapons and six mods i’ve created manually, but I don’t want to run into issues going forward with this approach, so regarding my two concerns:
-
the weapon mods will be kinda like randomized loot. Is there any issue with the SO implementation in this situation? Specifically, I would anticipate needing to create and destroy a bunch of SO assets as random mods are generated and as the player deletes them while they play. It seems a little odd to be creating and destroying assets during gameplay - is this an issue and/or is there a better approach?
-
when testing my game, I add and remove mods from the weapons I have, and each weapon needs its list of mods so that it keeps track of its own mods when that weapon is swapped out for another one. This way, when the weapon is re-equipped, it knows which mods it has on it. Am I going to have any issues with persistence or serialization (I haven’t implemented a save game feature yet) with respect to that List? Between different runs of my game while testing, this list persists in the SO asset for the weapon (expected), but I’m not sure if this will also be the case when it comes to serialization and saving the game (obviously, when the player loads back into their game their weapons should have the mods on them still).
If either of those concerns doesn’t make any sense, hopefully I can clarify (or that means it isn’t something I should be concerned about!). I appreciate any help I can get!