What the… is a “service locator”!?

Programming and magic… not very different.

Locator.GetService<CharacterManager>()… What is this?! Is it magic?!

This is how I have started programming at 40 years old. By asking what the hell is one of the quite abstract parts of our system, which is the service locator design pattern that we use for managing the game behavior. And I was completely overwhelmed. If you are not a programmer, but you do have some basic idea about what “instructions” are, you think you know stuff… I used to say “I do not know how to program, but I do understand the logic of a script”. It was bullshit. I thought I did, but I had no idea. I had no abstract thinking at that level.

Many years ago, around 1989 or so, I have started to “code” in Basic, on a Z80 Spectrum processor powered Romanian made computer called HC-91. Fortunately, it came with a manual explaining what Basic is an some “basic” examples of code. This is how I have started writing simple things, like drawing lines, sorting numbers, calculating fractions for school, etc. I needed more…

I still have it plus a fully equipped Commodore 64 as well

My mother was a librarian (in my small isolated town in the still communist Romania), which means that I made her dig into the deepest holes of their librarian sources for books on BASIC. I got two of them, one having the ENTIRE CODE FOR SPACE INVADERS WRITTEN IN BASIC. It was my first contact with an actual game. I almost fainted when I realized what that is… Couldn’t understand much because they were PEEK-ing and POKE-ing in the memory a lot, trying to work directly with the assembler, and code commenting was not a thing, really…

Just to make sure you understand, I only found out about the existence of the so called “gaming consoles” around 1998, when a friend of mines showed me an Amiga CD32. I was like, whatever, who cares… I was already playing Red Alert on PC. Consoles have never been a thing in Eastern Europe.

I could only afford an actual PC around 8 years later (my first salary was 30 dollars. My parents were making 100 both. A 386/486SX powered PC was 500 dollars and up) so until then I did not do much. I wrote some BASIC programs to draw graphs for advanced functions, with syntax checking, and I also wrote a chess game, that was played in two which was really, really slow. Using that knowledge 5 years later, I have tried programming in C++ for a few months, but when I tried to understand and code recursive sorting I felt so stupid, that I gave up, for another 15 years…

My mother used to say “You are not good at math, so better stick to Medicine. Leave the Computer Science for the “other” people.”. This is how they were taught and how many of us still are used to think. It is wrong. You get really good at whatever you are spending your time on, if you really like it.

But, back to our service locator pattern, how did I ended up understanding that?! It took me 6 months to get why do we use it and another 3 to get how it actually works. Which is sad, because I realize now that it is not really the best way to build our game, since Unity now has multi-scene editor support and you can store persistent data on Scriptable objects easily. Thus using a masked Singleton like this it is not really a good… WTF am I talking about here?!

I am becoming a programmer and man, I really want to tell you how did I went through this process of learning, because it was hard, frustrating and I felt like giving up almost every day. I need to share my pain, I need to tell you your brain can rewire itself. I also need to hurry because with every day passing, I feel like I can explain less to the myself version from 1 year ago.

I don’t know any more what I didn’t knew back then.


So, to start with how I looked at it… Our programmer friend that I mentioned in the previous article has set up the base of the game and he programmed a few basic features. Let’s have a look at the first one…

Laura explaining to our programmer friend the game idea… He suddenly became very interested.

The Service Locator Design Pattern (Doh…)

Apparently, when you start creating a game in Unity, you do not just open Unity, add a scene, some objects, some premade pieces of text called scripts and after you hit Play, everything will start moving(actually you can do that, but not to a serious extent). First, you need to plan the damn thing. How you plan it? You ask yourself “What do I want to do?”.( I will use our game, Heart.Papers.Border. as an example for everything, by the way).

Unfortunately, some people think that the answer for the above question is “I HAVE THIS AWESOME IDEA!”. Seriously, if you are not serious about learning some skills to make that idea happen, put that in a jar and forget about it. Ideas don’t count for shit without execution. When you say “Maaan, I want to do a game about space, like Eve Online, but with more exploration and less combat. I also want to have aliens. Awesome idea, ha! Let’s do it!”. How do you do it, where do you start?! Try open Unity, see if any of your ideas can be put into actual work… Write the code bellow, quickly:

/* Copyright (C) Important Person Full of Ideas - All Rights
* Reserved
* You may NOT use, distribute and modify this code under the
* terms of some license, which unfortunately won't be
* written for another century.
* Your role is to understand my vision and make it happen! (MOTTO
* OF THE DAY! I heard this as a producer a tad too many times, from
* a tad too many company owners and CEOs...)
* You should have received a copy my words of wisdom, slightly
* above. If not, please write to: someone, or visit : whatever
* ADD AS MUCH INFORMATION HERE TO MAKE THIS SCRIPT LOOK AWESOME!
*/
using UnityEngine;
// This is a simple script that makes a game for you
// Attach this to a Unity game object and it will start an awesome space game like Eve Online
// but with more exploration, less combat and aliens!
public class SpaceGame
{
public EveOnlineCombat m_LessCombatMultiplier = 0.5 // 1/2 less combat;
public EveOnlineExploration m_MoreExplorationMultiplier = 5; // 5 times more exploration
public bool m_Aliens = 1 // use 0 if you wants no aliens
public void Start()
{
Locator.GetService<UnityArtificialInteligence>().CreateSpaceGame(m_LessCombat, m_MoreExploration, m_Aliens);

//Press START now
}
}

Now, looks legit, right? You are telling Unity to make a game for you. Is it working. I hope it does. But even so, there are a few problems with it. Let’s debug!

  1. We have no type classes called EveOnlineCombat, and EveOnlineExploration. But wait a minute, what do I mean with “type classes?! Later, there a lot to explain there, see you in 6 months…
  2. Those numbers after “=” make now sense. It cannot be that easy, right?
  3. For m_Aliens makes perfect sense. I mean, if that “variable” is 0, there will be no aliens, if “1”, there will be aliens. Finally, something clear... or is it?!
  4. What is Start()?! What is that?! What do I start, ha?! The creation of a new space game? When is it done. Do I need an End() too?
  5. The damn ServiceLocator again?! Are you serious?
  6. I have no clue how all this is connected. I feel SO STUPID!

Now, there is a point to this. Imagine the code above would actually make perfect sense, but for you, as a total n00b, it looks like complete gibberish (no worries, this is actually gibberish).The code written by my friend, was working, and I had absolutely no clue how, why and when and… nothing at all!


But, at least, we had some basic requirements... This is probably a good time to split THE IDEA in sections, like Combat, Exploration and Aliens. Starting thinking in terms of game design we should define in your mind, practically, how would a gameplay session look. What are you suppose to do, from starting the game on the Desktop or device to going through menus, selecting shit, adjusting options, click new game, is it a 3D, 2D game, do you even have a character, are you a ship, and if yes, what is your ship about, what does it do, when, where, how is it the space, the environment, sounds, guns, scanners… man, my head is going to explode with so many ideas. Too much! And I am suppose to learn how to do all this?! Why r u so complicated, game?!

Well, if we’re still here, it is time do define some ground rules, the game pillars, those things that WILL NEVER CHANCE FOR THIS GAME, NO MATTER THE CONDITIONS. In our game we have as pillars : OPTIMISM, BALANCE, SENSE OF ACHIEVEMENT, CURIOSITY AND STATEMENT. Beautiful, isn’t it?! Whatever we do in our game, it will not leave this set of boundaries and it will evolve and revolve around them obsessively, including THE code. Once that is done, we can finally think about the rest of the game like The Main Features. Let’s define the Main Features! Yey! We are making a game! How awesome! So easy! Fantastic!

But wait, when do we code?! This is a bit boring, beyond the moment’s enthusiasm! (Story time: and then, a deep, wide and dark trench opened in front of his eyes… A voice from the depths was heard saying — “Later dude. You are not prepared!”). And do not install Visual Studio Express 2015, it will prologue your precoding time significantly. Use Visual Studio Code, it is much cooler, open source on top of that, in the indie spirit! You can use it on a Raspberry Pie 3, easily. It has themes as well.


And that is what I plan to dive into, for the next few articles, on how DID I UNDERSTOOD PROGRAMMING FROM BEGINNING TO NOW. Might be interesting, so hit that like button if you think I should continue!

The glamour of small spaces filled with computers where we can isolate ourselves and dive deeply in our misunderstood abstract thinking. Oh, the great indoors!