Studio 1: Programming Patterns

In this post I’m going to talk about programming patterns and the various ways I use them in Snofyr and ClutterBug.

Programming patterns are described in wikipedia as “a software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design”. Essentially a problem that people kept solving and has been re-purposed into a commonly used solution.

Patterns used

Singleton

A singleton is a static instance of a class that can only exist once. Because there can only be one instance of the class, it means that it is safe to have global access from other scripts to call and modify functions.

Snofyr used a singleton instance of the GameManager.

singleton.png
Snofyrs singleton. Note how it checks if it has a single instance

State Pattern

Having an object use enum states for different behaviours is a design pattern that is used in Snofyr’s gamemanager that I go into detail here.

Chain-of-responsibility

This pattern is an object oriented design pattern used to segment quantities of data. Each object is responsible for processing a set amount of data, and then passing that data onto another object to processed further in another way.

ClutterBug uses a chain of responsibility when instantiating objects into the game world. The node script handles the drawing of the shape in the game world, the normalised random location in world space, the amount of objects to be spawned, while also being responsible for the data to be sent to the main clutter script.

chain.png
Node.cs sending data to Clutter.cs

The main clutter script then uses this data to modify the where the object spawns, uses raycasts to check if the object is allowed to spawn, spawns the object at that point and parents it to the appropriate game object.

Leave a comment