The Following is a simaple and flexible gun system made in Unreal Engine using C++. I wanted to start a long term project that would be played withing the FPS medium. First approaching this project i wanted to create one fleshed out portion at a time so I would be able to demo complete work on my blog. After working on this gun system i found that this approach is not ideal. In order to not have wasted time I have decided to put what I have completed on my blog and continue with development. Going forward I want to work on simple prototype components and build them up together as I progress through my development.

When first making the gun system I used a basic object oriented approach with a parent gun class and an assortment of child gun classes based on varied functionality. Progressing through development I would realize this approach would become difficult with overlapping functionality in some weapons. If I want to add and automatic weapon I would create a child class but then If I wanted to distinguish between a hitscan weapon and projectile weapons there would be two different automatic weapon classes. I began to realize how undesirable a chain of object oriented classes would become with its increasing difficult to manage and add changes. The object oriented approach was scrapped and replaced with something more flexible.

I needed to have a variety of weapons with different functionality but each separate gun functionality is decoupled from each other. Decided to use a data driven design approach with components for the different gun functionality which would be attached to a single gun base class. Each gun would be an entry in a data table which would hold different gun stats and the components used to create the gun.

Gun Components:

The reciever would handle the gun firing behavior whenever a trigger is pulled or released. The weapon firing behavior would be a single method stored in the base class and passed as a delegate to the reciever.

GunReciever->TriggerPulled(GunFireDelegate)
      

The projectile component would be invoked from within the gun fire method of the base class.

GunProjectile->FireProjectile()
      

The Gun magazine component would handle reloading along with other checks needed to be done throughout the gun base class. The main need for different magazine components was to deal with single bullet fed weapons and action based weapons.

When doing the creation process from the gun data stored in the data table I found it difficult to manager references to the table in c++. After some search I learned about how I could replace the data table with Data Assets and utilize the asset manager to solve my problems. Had one Data Asset to hold all relevant animation related data for a weapon then a main Data Asset for each weapon to be made into an actor.

This approach of using the gun Data Assets allows me to store the unchanging data relating to a specific gun inside its gun Data Asset. After switch to the using the Data Asset throughout the c++ file I was then able to remove duplicate local variable inside the gun actors header. The utilization of the gun data was analogous to the flyweight pattern and would help reduce the size of the gun actors espcially if duplicate gun actors exist in the scene.

In order to construct guns at runtime I created a gun factory class using an unreal subsystem class that was attached to the life time of the game instance. With this I would be able to decouple all pre-initialization operations, such as setting the gun Data Asset before actor BeginPlay, into one class.

        AGunBase* NewWeapon = GunFactory->SpawnGun(GunDataAsset);