Learning the hard way on keeping clean code and OOP, Feel like im making references everywhere and im starting to lose track so i’m trying to rework my weapons system. Can you offer any advice on my logic and implementation of the weapons system im designing?
Features
Player changes equipped weapon and updates the HUD with the new information
Player shoots and decreases ammo count and updates on the HUD.
That information should be individual to that weapon. There should be an Individual Weapon script, like “PistolScript” that inherit from a common class “Weapon” with common properties
each weapon should have:
(These should be a Scriptable Object since its just data)
weapon damage(int),
ammo(int),
weaponHUD graphic(sprite),
the prefab of the weapon(GameObject),
the bullet prefab of the weapon(GameObject),
a spawnLocation on the weapon prefab(an empty GameObject attached to the prefab)
each weapon object has a static object pool script for the bullets.
One Weapon Object should be passed to the playercontorller script at a time. Maybe as an index of a list.
the equipped weapon in the playercontroller script should update the PlayerUI/WeaopnHUD
Adding new weapons later in the game. That weapon obj should be added to the list of available player weapons.
What specific issues are you having? Sure we can give broad, general advice but it also may not be applicable to your particular project. So what parts are you having the most issues with? What are your current pain points with your architecture?
The implementation of the system in my code is the problem. It is growing and can be confusing to keep track of what is doing what. It feels deeply interconnected and it should be looser i think.
after implementing object pooling i had a problem switching weapons, after fixing that i had a missing reference, after fixing the reference and removing an unnecessary script i broke the camera control when nothing was connected to it with the weapons.
I’m solving the problems but its happening too much. Hence i want to see if my current logic on how i should implement it makes sense to others and see any other possible feedback
Well this is really the only specific indication of how your code is architectured that you’ve given us:
Which is to say, your PlayerController component probably has way too much responsibility. Move the broad strokes weapon handling to its own component. Keep movement and input in their own separate components.
Keep UI separate too. It should be it’s own component(s) that hook into weapon handling component. Weapon swapping can be communicated via a delegate, allowing the UI to refresh itself accordingly, and pull out the specific UI prefab for each weapon.
Bottom line of a lot of this is to separate responsibility, and to keep dependencies one-way. Consider whether two objects should be referencing one another, and how that might be made one-way, or severed completely.
1 Like