Hi all, I’ve been thinking about creating a game with several weapons of different types that all share some properties, but have perhaps one property that only belongs to a certain weapon. I like to structure my projects before committing a lot of time to creating assets and balancing gameplay, etc. I have never taken on a project like this before and I’m struggling to figure out how a system like this should be built from scratch.
I’d like to have an efficient (fast) system that can store weapon statistics based on the name/Id of the weapon (Shortsword, Staff, etc.) that can then display the statistics about a weapon, like on an overlay when the user hovers over a specific weapon in their inventory, in a shop, trade window, or anything.
This is simply a proof of concept of what sort of things I have in mind, treat it as pseudo-code.
Shortsword = {
["Damage"] = 10,
["AttackTime"] = 5,
}
Longsword = {
["Damage"] = 15,
["AttackTime"] = 6.5,
}
Bow = {
["Damage"] = 10,
["AttackTime"] = 5,
["ReloadTime"] = 4
}
Warhammer = {
["Damage"] = 20,
["AttackTime"] = 15,
["Abilities"] = {
["Slow"] = {
-- Slow target by 50% for 5 seconds
["EffectDuration"] = 5,
["SpeedModifier"] = 0.5
}
}
}
As you can see, every single weapon shares a Damage
and an AttackTime
property, but only the Bow weapon has a ReloadTime property. The Warhammer would have a special effect with custom properties in addition to its normal attack, while the other weapons have one general attack.
I have two ideas for this that I honestly haven’t put a whole lot of thought in to (that’s why I’m here!).
- One method seems easiest but not very ‘clean’ or scale-able, and that would be to store each relevant stat in a
BaseValue
likeStringValue WeaponName
,NumberValue AttackTime
,NumberValue Damage
, etc. and only include the stats pertaining to each weapon (the “Sword” would not have aReloadTime
orAbilities
values). My only hesitation with implementing this method is that this seems like a lot of creating and organizing values for each new weapon I want to add to the game, and it won’t scale very well if I ever decide, "hey, let’s add a durability stat to all of the (100+) weapons!'… - Another method I can think of (may not work because I’m not very comfortable with tables/metatables yet) is to attempt to have a ‘Master’ table of all possible weapon stats (Damage, AttackTime, etc) and attempt to
__index
them for every weapon when requested. If the value (stat) doesn’t exist for said weapon (trying to access a Dagger’s ArrowSpeed for example), returnnil
or something? Again, I’m struggling to explain this because I don’t have a lot of (or any) experience with meta-tables. This sort of solution also runs into the same kind of problem as before with adding additional stats, though maybe not as drastic as the first idea.
A third idea that I’m currently against is a combination of both of the following ideas, only making a table for the properties that every weapon has, and using BaseValue
s for individual properties. I don’t like this idea much and don’t expect it to be the leading or sensible solution, so I won’t go more in depth into it.
Any resources, ideas, or feedback would be greatly appreciated here! I’m going to say thank you in advance because I won’t be around to view this post for ~8 hours after posting it. Unfortunately this also means I won’t be able to reply to any questions/confusion for a while. Hopefully this post makes a bit of sense, and thanks again to everyone who can offer advice! Thanks for reading!