Static Types

Chris Didier
4 min readApr 4, 2021

--

Statics use the keyword static. Static classes, variables, and static methods. Static information can be stuck in memory. Static information is going to be there for the life of the program., so it is important to know how much available space is there.

An important thing to know is the difference between instance members and static members. Static members live for the life of the program where instance members live for the instance of the item. When we create a custom item class it is an instance of the item.

A good example of this is if you had a multiplayer game and were monitoring how many players were connected at one time.

Same thing for how many items are currently exist that you have.

Now I am going to go over a practical use case of using static types. So I am going to be creating a spawn manager. A great of example of when to use static types when you have data that is going to be shared across your game and will live on for the life of the program. In this situation you will always be counting how many enemy players you have in the game at one time so it makes sense to track that using a static.

Next I am going to talk about static methods and static classes. Utility classes are helper methods used to streamline the development process. It is important to know that you cannot inherit from anything as it is a non-mono class, and everything in this class must be static for it able to work. In the example below I created a primitive object by pressing the space key and moved an already created object by pressing the E key.

Next I will be creating a helper class that would let me turn any object I pass into it a color of my choice. The player must use the space key to trigger this event and the color will be randomized.

Finally, I will be working with static members that have static constructors. In the example below when you have a static method it is called 1st before a public method.

Working with statics is something that you will not want to rely on using too much as you do not want to use up too much memory of your game, but it has it’s uses on game components that will live on for the life cycle of the program.

--

--

Chris Didier
Chris Didier

No responses yet