- Home /
Creating a weapon database from prefabs
Currently I've created some weapons (e.g. longsword, spear) as prefabs. These consist of a sprite with a Weapon script attached, specifying attributes about the weapon (weight, cost, damage etc.)
What I want is to allow the player to:
1. Have multiple "local" instances of these weapons, NOT just references to the prefabs.
2. Customise these "local" weapons by changing their properties (e.g. adding fire damage).
But I'm struggling to do this because any changes I make to weapons affect the base prefab, which I don't want.
I know that GameObject.Instantiate creates a copy of the prefab in the scene; ultimately I do this when I want a weapon to actually be equipped, but it seems silly to instantiate all my weapons in the scene and have all but one of them sitting somewhere deactivated. The other thing I was thinking is that I could have a list of the player's current weapons, which consists of deep copies of the prefabs, and operate on that.
Basically if I have a longsword in my inventory, I don't want my customisations to that longsword to apply to ALL longswords (by affecting the base prefab), I only want it to affect that particular longsword. What's the best way of doing this?
The problem is that you are thinking of your visual weapons as the same as the idea of your weapons. You need to separate these ideas and work on them this way. Think of it like this, what the player sees is just a visual representation, so all your weapons should just be data objects with all the information required to build the weapons at runtime, like a reference to the base prefab, damages, attack speeds, associated animations, icons, etc. This way when you want to represent an item you can call to the data object and use it as needed, if its to display in inventory, you'll use all your information to feed into your UI, and if its being equipped, you can used the data object to adjust things about the player, and so forth.
Ok, so you also favour the "deep copies" approach, i.e. have a data structure of the player's current Weapons (attributes and sprite renderer) in the Weapon$$anonymous$$anager, which is basically a list of deep copies of prefabs so that we don't operate on the prefabs, and then instantiate those copies when a weapon is equipped. Is this more or less what you're suggesting?
Answer by Esteem · Apr 03, 2019 at 09:01 PM
well since you have to have an InventoryItem class of some sort for each weapon anyway, why not store the local changed values to that and when the player chooses to equip it, just Instantiate the model for it from the prefab and override the default values by the values you have stored in your InventoryItem ?
This is what I was thinking with the second approach: "have a list of the player's current weapons, which consists of deep copies of the prefabs, and operate on that." Each weapon prefab is a GameObject with a Weapon script storing its attributes, and a sprite renderer for what it shows up as in the scene. So I guess what I'd do in my Weapon$$anonymous$$anager class is store a list of all base prefabs, and then have a separate list of deep copies of them which acts as the player's current weapons. This list would basically store Weapons, i.e. attributes, and whenever a weapon is equipped from this list, I'd go and look up the base prefab, instantiate it, and overwrite that instance's Weapon attributes with the player's current ones. Is (something like) this what you're suggesting?
Depends on what you mean by a deep copy, because it seems redundant to me to have two classes that both hold very similar data.
what I would do is create an abstract WeaponData class and then use it as a base for all my weapons (i.e.: LongswordData : WeaponData
then WeaponData would have protected vars that are the same for all weapons (like damage)
LongswordData would then have longsword specific data
you can then store all the weapons in a List and when a player equips a weapon, Instantiate a prefab of that weapon with a small WeaponBehaviour : $$anonymous$$onoBehaviour class that has a field for WeaponData. (private WeaponData data;)
Where you decide to create the logic for the weapons themselves is up to you, but if you decide to to the logic in WeaponData public virtual void Attack()
You can then do public override void Attack() on Longsword and from WeaponBehaviour's Update() you can do
private void Update() {
data.Attack(); }
That way, everything is nice and structured.
NOTES: if you're serializing the weapons and saving them in a WeaponData list, careful there for Unity does not serialize base reference of inheritors.
PS.: idk how well do you know the concepts so I am sorry if this feels like I am patronizing you or whatnot :/
Thanks for your ideas; I also agree it's redundant to have basically two lists of a class that do exactly the same thing but just get treated differently. I like your idea of having a base WeaponData class and then having instances of that in derived classes; I'll have a play around with it when I get back to my PC.