The making of our Weapon Manager and how important object oriented programming is

5

23 februari, 2017 av eric01422

When I first wrote my weapon manager I did not think about the importance of programming in a object oriented mindset. This lead to a limited script that made it hard to implement new weapons with different stats and behavior. This was something I later needed to rewrite and in this blog post I will talk about how I perceived this encounter.

At the start, the weapon manager contained the stats for a general weapon for example fire-rate, spread and recoil. It also used a Transform reference that would have the location and rotation of where the bullets world spawn. So what I did was place the spawn point in front of the default weapon sprite and spawned different projectiles depending of witch weapon you used.

This was a big problem because now I could only use sprites that would be the same size and located at the same location as the default sprite. So my solution was to make all weapons into their own objects containing their own information. Now I could create a stats script that would contain all values and behaviors that the weapons need to function that previously existed in the weapon manager script. The only thing left in the weapon manager script now is the function to change between weapons by disabling and enabling the game objects in an array.

The result was an easy to use WeaponManager with a clean interface that grants the ability to add new weapons with low effort and to create weapons with their own specific behavior and stats in the new stat script. One extra things that becomes much easier is to modify the weapons in game for example, if you want have a weapon level or buff ability you can simply just change the stats in the weapon object you want to edit.

What I have learnt from this experience is the importance to sort and structure code into an object orientated mindset, so that I can reuse and easier access elements in a specific object.

EXTRA

One interesting behavior that is contained in the stat script is the sprite recoil animation function. What is does is that when you fire a projectile it moves the weapon sprite back in relation to the player and the slowly return to its original spot.

Here is a picture of the pattern

paramiter

Credits go to Erik Säll for the idea

5 tankar om “The making of our Weapon Manager and how important object oriented programming is

  1. programmerik skriver:

    Hi Eric! Very interesting post, glad to see some refactoring.

    You do a good job of introducing the problem by first explaining the old system and its limitations. You then explain the new system and its benefits, which seems very dynamic. The recycling of scripts for different weapons and the general recycling way of thinking could be inspirational for programmers struggling to keep track of their structure in Unity. The benefits are clearly communicated, and even potential unplanned features which could be implemented with this system are touched upon. I like that you reflect on your personal growth as well, so we can see your progression.

    For next time maybe you could expand more on how your new system works, and maybe provide some explanatory images; although it’s I understand the reasoning on keeping the blog post to the point and not go into unnecessary detail.

    Well posted and good luck!

    Gilla

    • Hi Erik. It is I, the Designer of time.

      I can see I taught you well, and your comment is realy positive and to the point. You can almost see Erics spirits lift up. I could not be much prouder of you. But remember, there is always more that I, the Designer of time, can teach you.

      Do not forget that you can always reach me on my g-mail, thedesigneroftime@gmail.com.

      / Yours truly, the Designer of time.

      Gillad av 1 person

  2. baloobeckman skriver:

    Hi Erik!

    This was a helpful blog post as it introduces a solution to a problem I’m having and are just about to try myself especially creating a weapon manager and more distinctive weapons..
    The problem you had was clearly explained and you can see how you couldn’t make more different weapons using using that structure.
    For your solution it’s very interesting and just something that I want to implement myself.
    I like how it could be used to easily put in more weapons in the game and it clearly showed in your presentation how different all your weapons were.

    In your stats script you had all weapons defined and not in separate scripts for each weapon object?

    Has that been helpful? I can see how some variables might be used for multiple weapons instead of having the same or similar variables in different scripts.

    The weapon manager is also something that is well explained and really useful for me specifically. So I wanted to say thanks for the ideas.

    Baloo S. Beckman

    Gilla

  3. Patrik Selleby skriver:

    Hey Eric,

    I too have been struggling with proper object-oriented programming conventions in unity so it is refreshing to see that I’m not alone in that regard. Your post has been very informative and your post is written very concisely and to the point, with a clear ”red thread”. And while it is technical, I still believe anyone can understand your underlying problem and how you solved it.

    The only thing I think is missing is perhaps some pictures. Maybe you could have visually demonstrated your problem. I get that since this is a pretty technical blogpost that it might be hard to demonstrate through pictures the idea of your problem, so it is not the easiest task.

    But overall I’d say this is a very thorough post.

    Looking forward to playing your final game this thursday, and seeing how it has progressed.

    Good luck in the final coming days!

    Gilla

  4. Kyle Alley skriver:

    Hello Eric,

    This was a very nice read, object-oriented programming is still something that I struggle with throughout my coding in Unity. The flow of this post was very nice to follow along with and it had so much helpful information even for those who are not very familiar with the topic. Your main problem was easily identifiable throughout your blog post and it was nice seeing your thought behind solving it. It was nice to see how you had spawned the bullet with the properties of the gun in front of the gun it was coming from.

    Showing some snippets of code might make understanding of what you did easier, consider adding some next time. Clarification is hard for us programmers, so I have found it’s easy to help the reader understand if we add some code.

    Your game is pretty nice to play, can’t wait to see the final.

    Good luck
    -Kyle

    Gilla

Lämna en kommentar