Hello! I’m currently doing an investigation for my engineering thesis, about Software Design Patterns for videogames, and since I think this is going to be my chosen thesis subject I want to ask this community about something particular:
•If you where about to implement every single programming pattern you can think of (from the well known singleton, so something more weird in gamedev like MVC/MVP), would you use Unity??
I’m developing under Unity, but I don’t know if the component oriented structure in unity will make things easy for me when using architectural patters such as MVC.
In my thesis I will add simple mini-games or demos using each pattern with a PRO/CONS table.
Unity is a tool and you can use any design patterns where they are appropriate.
i.e The State Design Pattern can be used for an AI FSM and there is a plugin for NGUI which uses MVVM. Two simple examples but patterns are meant to address common design problems, no matter the end product. I use the singleton pattern for my GameManager object in all my projects.
At the end of the day if the pattern can help, use it, otherwise don’t.
When you see that it makes sense to use a pattern, there is always a way. In a programming environment you have always restrictions. In Unity you are bound to MonoBehaviour if you want to see something in the inspector. When you are using WinForms or anything else, there are other restrictions.
An interesting thesis would be to compare how high performance, high productivity server-side programming and game programming have diverged in the last 10 years along with the parallels in progress of server hardware and home computers / consoles / phones, and what that means for the future of game engines and game programming. Then, when you’re done with your degree, put together a team write a game engine that leverages what has been learned on the server side and make a few 10 of millions of bucks.
First, while MonoBehaviours in Unity may not use a constructor (directly) - I recommend the Awake and Start events, as those make a close-enough approximation - you can make use of composition.
A gotcha that I discovered early on with Unity’s implementation of C# is, for variables in a subclass to be editable through the editor, the class they are in needs to be decorated with the Serializable attribute, from System.
Example:
using System;
using System.Collections.Generic;
using UnityEngine;
public class Foo : MonoBehavior
{
#region Variables / Properties
public StateMachine Fsm;
#endregion Variables / Properties
#region Engine Hooks
public void Start()
{
List<State> states = new List<State>
{
new State(ConditionA, ActionA),
new State(ConditionB, ActionB)
};
Fsm = new StateMachine(states);
}
#endregion Engine Hooks
#region Methods
// ConditionA and FooA are defined here...
#endregion Methods
}
[Serializable]
public class StateMachine
{
#region Variables / Properties
private List<State> _states;
#endregion Variables / Properties
#region Constructor
public StateMachine(List<State> states)
{
_states = states;
}
#endregion Constructor
#region Methods
// Behaviors that the class engages in. Implement your own!
#endregion Methods
}
Another point of order is that sometimes MonoBehaviours are not the key to implementing some actions; a standard class instance can do the job just as well.
Smooth P., I understand from another topic you’ve at least appeared on the Wave Engine forums. Do you actually use Unity at all? For a programmer like yourself, I would’ve thought you’d known some of this…
No, it sounds like they are asking for how patterns can be applied to game development, not if they can be used.
I’d suggest doing some research and see what patterns have been applied in the past in which situations.