Creating a weapon system

If you know all the weapons before hand and don’t need to animate their position you can add them as separate sprites to the character game object and enable/disable as they are used. Store stats for each weapon in a table, like:

self.weapon_stats = { pistol = { damage = 10, range = 30, firerate = 3 },
                      shotgun = { damage = 50, range = 15, firerate = 1 },
                      ... }

If you have lots and lots of weapons and they eat lots of resources you might want to load them dynamically, but the above should work for quite a few sprite weapons.

Also note that if you need weapons placed separately (as pickups) or have other users of these weapons (like enemies) you may want to store the weapon stats elsewhere. One way could be to have a “weapon” game object with all the possible sprites, stats and a script that switches between the used type.

2 Likes