Download

Weapon Switching System

So I have developed a very simple weapon class for a first person game. Each weapon has attributes such as ammo, reload time etc…

When I equip a weapon, I spawn it and give the character a reference to his current weapon. When I switch weapons, I delete this weapon
and spawn a different one. When I now switch back to the previous weapon, I have lost information such as current ammo and other such
attributes.

What would be an effective way of switching weapons without having to delete them and lose instance information?

Hey waltheamMichael-

If you set your weapons up inside a weapons array then you can associate the array with the character. At that point each weapon has it’s information saved in its array element and switching weapons would be a matter of no longer rendering the current element and begin rendering the new element. If the attributes are saved to the weapon themselves then they should persist through switching.

So instead of destroying weapons that are not currently used, I simply hide then from gameplay?

Yes. It’s also better for performance to hide/unhide assets as necessary rather than destroy/recreate them. When you hide them you’ll also want to make sure that you turn off any collision and other features so it won’t interact with the world.

I see, thanks a lot for the help :slight_smile: