Mathematics

I have always had an odd relationship with mathematics. Even though I began programming when I was a young child, I had a difficult time in math. By the time I got to college I was so upset with the math requirements for computer science that I went the route of an English major. Not my best moment. It was a passive and somewhat naive decision, and in my junior year I started focusing on the CS major… but as soon as I finished the BA, boom, I landed a job, and it’s been over five years since formal education.

And how I railed about those math requirements. “Hey, a computer does math for you, why should we have to learn this stuff?” And likewise, I noted, math students in the computer science courses (a requirement at MTSU) had similar gripes. If I was blogging at that time, I would have probably had a whole bunch of arguments against any comparison of the two fields. And to be sure, they are different fields, at least unless you are specifically programming mathematical applications (and really a pro mathematician should be supplying the algorithms if that is the case).

I’m not a kid anymore but the thing about kids (I consider college me a kid) is they may have incorrect conclusions, but there is always something to what kids have to say. I was so challenged by math that once I broke down in tears in High School over geometry. But just today, I (warning, codespeak) used raycasting in the local opposite forward (z) direction and the global y direction to make an object automatically turn corners when leaning into them. Didn’t even break a sweat. So what happened between then and now besides just getting older? And why were those math students so stressed out in CS 101?

I have concluded over the years that there are two routes to problem solving. The step-wise refinement method, and the research method. In schools, we almost always learn the research method. We are asked to memorize formulas, and apply them to numerous situations repetitively.  I’m not saying mathematicians, like those in that class, are just regurgitators of past ideas. They work to both build upon and apply formulas to real world problems by expanding on the research, like scientists and other very important people. And it’s probably helpful for them to do some step-wise refinement. But I don’t think it’s how they normally approach a problem.

And as for me, well, I don’t exactly remember any math teachers telling me WHY numbers work, or WHY geometry functions like it does so that I could problem-solve it with logic or break it down into a procedure. If you want to see just how little the public understands numbers and how much they are taught merely to memorize things, try explaining counting in a different base than ten.  Even though the process is the same, because logic is always the same,  because math is always perfect… it is a curve ball.

I’m not offering any suggestions for the education system on how to fit more to everyone’s mold without losing people.  Maybe that kind of understanding is difficult for the general public, and memorization just gets the grades up for the usual ‘I hate math’ kid (i.e. 90% of them.) Or maybe there really is something wrong with the way it’s introduced. All I can say is that if you’re a young programmer, yes, math matters, but no, you don’t need to memorize every formula on earth. You have Google, you have innovation, you can do it. But please, wriggle through it in college… don’t cast your on-topic degree aside like that.

Tutorial: Runtime Variable Management in Unity

When designing a game, you will often have some runtime variables to be concerned with. Did you talk to that person? Did you unlock that door? What did you name your dog? These are just a few possibilities of what you might need. Not only should such things be easily managed, but they need to be serialized to and from game states so that the player can save and restore their game.

For the sake of self-education, I avoided the Unity asset store and decided to piece it together exactly as I like it. I liked the principal of RPG Maker’s flag tables, but I also love how one of Unity’s best features is being able to view and modify the game as it is running. I wanted all my variables to work the same way. What if I’m testing and I forgot to do something that is required to progress to what I’m interested in? I don’t want to have to restart, just let me set the correct flags and keep moving.

In order to achieve this, I needed a manager that could do the following things:

  • Edit and view my variables on the fly while play testing
  • Save and load the flags to a file stream, so I can easily add them to a save state.
  • Easily access them from the code anywhere I want

My solution: A singleton class holding a list of template objects, where each handles how it draws to the Unity Editor in a different way. This guide will show you how it works. See the link at the bottom for the full source code, free to use in your own project.

 

Extending The Unity Editor

Whenever you extend Unity with a script, you need to remember that during runtime, you will no longer be in the unity editor environment. That means that you will need to #ifdef your code to prevent errors:

using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;

Any code using UnityEditor functionality, including GUILayout calls (which are used to create unity editor interfaces) must be encapsulated in this manner. The only alternative is to stick it in an ‘editor’ folder, but it won’t compile as part of your final game, which is less than desirable.

We will also need a small class which inherits from EditorWindow. Unity lets us add this window to the UI in all sorts of ways, but lets add it to the dropdown ‘Edit’ menu. Look how small this class is:

#if UNITY_EDITOR
// Allows menu based editing. Don't need to do anything, just go to Edit/Flags
public class FlagManagerEditor : EditorWindow {
 public void OnGUI() {
  FlagManager.Instance.OnGUI();
  this.Repaint(); // For some reason this is needed to initially draw the thing.
 }
 
 [MenuItem ("Edit/Flags")]
 public static void ShowFlagEditor() {
  EditorWindow.GetWindow<FlagManagerEditor>();
 }
}
#endif

The ShowFlagEditor function triggers when you click Edit/Flags now, opening this window – fully dockable, too! Of course, this by itself will give you an error since it wants to use a singleton called FlagManager in the OnGUI() function. Well, let’s make that now.

 

The Main Class

I’m going to call the variable management utility FlagManager since it’s good and short. Yes, it holds variable types, not just booleans, so sue me.

FlagManager follows the singleton pattern, but it doesn’t have to be a scriptable object or anything like that. It doesn’t even have to attach to a game object. It is a permanently existing component to the game.

public class FlagManager {
 private List<FlagRecord> records = new List<FlagRecord>(); // The records

 // Singleton pattern 
 static private FlagManager instance = null;
 static public FlagManager Instance {
  get {
   if(instance == null)
    instance = new FlagManager(); 
   return instance;
  }
 }
 private FlagManager() {}
}

There, a very simple singleton. I introduced another class, FlagRecord, which we’ll get to later.

Now for it’s OnGUI call, which uses Unity GUI commands to draw to things your interface. Remember: this only ever gets called and only can be used in the editor, not for a standalone executable. Another #ifdef fixes that:

#if UNITY_EDITOR
 public void OnGUI() {
  foreach (var record in records) {
    record.OnGUI();
  }
 }
#endif

Each FlagRecord in our records list will handle its own drawing, because after all, how do we know what kind of input to allow? A number slider is different than a checkbox or a text entry box.

Nevertheless, we can add ‘new’ buttons directly after the foreach statement. They will go at the bottom of all the flags that way:

 GUILayout.Space(10.0f);
 if(GUILayout.Button("New Boolean"))
   records.Add(CreateRecord("__TEMPBOOL__", typeof(bool)));

And thus we need a CreateRecord function:

public FlagRecord CreateRecord(string key, System.Type type) {
 if(type == typeof(bool))
  return new FlagRecordBoolean(key);

 return null;
}

Notice that in the last two functions I put ‘if’ statements. This is because you can extend both OnGUI and CreateRecord to handle new types in the very same manner. The full source code at the bottom of this tutorial supplies strings, integers, and floats. But for now, let’s focus on booleans.

 

The Flag Records

Now we need a typeless FlagRecord class. You can’t directly use them, so it’s going to be declared abstract.

public abstract class FlagRecord {
 public string Key; // Name of this record
 public FlagRecord(string keyName) { key = keyName; }

 #if UNITY_EDITOR
  public abstract void OnGUI();
 #endif
 
 public abstract void SaveToStream(Stream stream, IFormatter formatter);
 public abstract void LoadFromStream(Stream stream, IFormatter formatter);
 public abstract System.Type GetValueType();
}

It has a key, but no value. Not yet. But now the main class can aggregate our records. Next, we need a template subclass:

public abstract class TemplatedFlagRecord<T> : FlagRecord {
 public T Value;
 protected bool shown; // So that we can collapse and hide flags

 public TemplatedFlagRecord(string key) : base(key) {
   Value = default(T);
 }
 
#if UNITY_EDITOR
 public override void OnGUI() {
  shown = EditorGUILayout.Foldout(shown, Key);
  if(shown) {
   // Now create the editor for the value based on type
   createValueEditor();
  }
 }
 protected abstract void createValueEditor();
#endif

 public override void SaveToStream(Stream stream, IFormatter formatter) {
   formatter.Serialize(stream, Key);
   formatter.Serialize(stream, Value);
 }
 public override void LoadFromStream(Stream stream, IFormatter formatter) {
   key = (string)formatter.Deserialize(stream);
   Value = (T)formatter.Deserialize(stream);
 }

 // Retrieves the type of value held within this flag record. Do not override.
 public override System.Type GetValueType() { return typeof(T); } 
}

NOW we have a value… of any type we may need, too. And thanks to C#’s great type system, we can even initialize it to a default value.

If you’re not familiar with GUI functions, the ‘Foldout’ will display the key name next to an arrow that can be expanded or collapsed. You’re also DONE when it comes to serializing any of these flag records, as long as their type knows how to serialize. For basic types, this is always automatic.

So, finally, here’s the boolean flag class:

public class FlagRecordBoolean : TemplatedFlagRecord<bool> {
 public FlagRecordBoolean(string key) : base(key) {}
 
#if UNITY_EDITOR
 protected override void createValueEditor() {
  Value = EditorGUILayout.Toggle("Value", Value);
 }
#endif
}

That’s all there is to it.

Taking it Further

I did not include the functionality to actually set or fetch variables, rename them, preserve uniqueness in the names, delete them, save all flags to a stream at once… etc. I wanted the tutorial aspect to quickly show how a list of variably typed fields can be managed and displayed in the Unity Editor in real time.

The complete, functioning code is available here.

 

A Letter To Myself (aka new Unity3D users)

thisisnotburgerkingyoucanthaveityourway

Dear me about a year ago,

There’s no mistaking how much easier it is to use an existing engine for game development. Even something like Ogre3D, while greatly simplifying the rendering part, is bare-bones. Unity, Unreal, and other systems give you rendering, controls, and most importantly, scene editors. But there’s two major gotchas you need to know about before going into an existing game engine. I can only verify that they are true in the case of Unity, but I would always go to the table with them in mind.

#1 – You are stepping out of the loop

When I first started my project in Unity, I went in with a few needs. One of those needs was that any two scenes in the game could be loaded at the same time, invisible to one another. All I can say to anyone who is going to attempt that is, you might want to contact me about it. Additively loaded levels always load in at their local geometric space, and moving them with a translation affects cloth. Let me tell you, it was pretty awesome watching all my cloth tear itself free and fly off into space. And using layers had its own list of problems. I solved this, but it took a ton of work – whereas I was able to prototype the two-scene concept in 2D with ruby and Gosu in, oh, about four hours. Wow.

To summarize, by using Unity, you are stepping out of the main loop. By using an SDK, you are sacrificing your god powers as a programmer and relying on the engine to make a consideration for your specific need. This isn’t true with an engine like Ogre3D. With libraries, you are offloading work onto existing code. You still own the program. Before you give up that authority, make absolutely sure the engine you’re using will support your specific needs, particularly if they are unusual.

Why did you think companies still make proprietary engines internally? To burn money?

#2 – Everything you know is wrong

My biggest handicap as a programmer is perfectionism. I took a lot of the ‘proper’ programming practices to heart when I was in college, and I strive to follow them. I could write a whole blog entry on this topic, because I have come to think trying to program great code ends up adding unnecessary length to the process. They’re important guidelines, but sometimes you have to remember that the goal is a functional, maintainable application. Not to perfectly and pedantically compartmentalize everything.

And then you have Unity.

I could never have guessed how different it was to program in a component-based manner. In Unity, you attach scripts to gameobjects. You do not have scripts that are always executing no matter what scene you launch from – you have to get sneaky with singletons to pull that off. All script execution starts and updates from those gameobjects, and in an arbitrary order by default. And forget cutting off access to any of those game objects; you can look up and access the public members of anyone from anywhere.

I discovered that it was much easier to maintain my code by breaking most of the guidelines I cut my teeth on. I gave classes multiple jobs divided into different files using the c# partial keyword. I created singletons. I publicly exposed member variables. It’s just easier.

I think if I had been aware of all this going in, I would have saved some time. So I send this letter out hoping it helps prepare someone going in. Using Unity is NOT a normal workflow if you are trained in programming directly, even if you use an interpreted language. Though it isn’t without its benefits. If you can work within the limitations of the engine, being a part of the program instead of its core developer is much less work. If it starts to feel like more, you’re doing it wrong.

Literally yours,

Joseph

Genetic Algorithms!!

In programming we’re often told not to reinvent the wheel (i.e. if someone’s done it, don’t do it.) But I have always felt that a little reinvention was the best way to truly understand something. The way I see it, if you can discover the big picture from a basic concept, you will be more intimate with the process and therefore prone to do something innovative.

With that in mind, I started a side project with genetic algorithms… and I decided to approach it by not researching the subject one bit. Now, I already knew that it the idea is to mimic evolution, and that it involves generations, mutation and calculated fitness values. So with that in mind, I basically ended up with a design that was pretty close to the standard, albeit my naming convention came out different. What most such algorithms call a genome or chromosome, I referred to as a gene. I refer to my organisms (a solution of genes) as cells, and the world they exist in as my petri dish.

I’ve learned that there are different approaches to the process of the genetic algorithm, but I took a Darwinian approach because I’m familiar with his theory. I don’t know the others (Lamarckian / Baldwinian) but I will do more research sooner or later. Anyway, my cells mate in randomized pairs and pass a mixture of their genes on to offspring with a chance of mutation or newly spawned genes. At each cycle, I kill off all cells that have below or equal to the average fitness value, which is up to 50% of the population. Then I replace those with offspring and start over. Eventually, sure enough, my fitness values end up quite high, especially as more generations die and mate. I refer to my best fitness cell as the ‘hero’ of the  current set.

I break numerous rules of nature, such as having a fixed population for all generations and having no mating rules whatsoever. There are also no catastrophes, lifespans or gestation periods because I don’t see these as valuable to trying to reach a solution. Perhaps I am wrong.

Anyway, I’m really having a blast and considering a GUI-based G.A. library of sorts that could aid me with my experiments. I hope this doesn’t cut in to my game design time too much. :)

Window of Opportunity

As an adventure gamer I’ve heard a lot about the resurrection of the genre through the existence of tablet computing. There are two features that make this possible:

1. Lower standard of graphics means that traditional 2D sells just fine, and that seems to function better than 3D in the genre.

2. The touch screen is ideal for inventory puzzles and other forms of problem solving.

Okay, so the second one is going to always be a thing. But with a heavy heart, I have to say that I lack faith in the staying power of point #1. Tablets are about to get increasingly powerful. Even Google’s cheap-o Nexus 7 has four cores in it. And Microsoft wants them to basically be full computers that just happen to fold up nicely with a touch screen.

While you need other peripherals to really enjoy a third person shooter or massive RPG, I think there is a very small window of opportunity here. Many adventure game legends, through kickstarter, have wisely jumped aboard this ship but I think for many upstarts like myself, it’s going to have sailed before anything can be pulled together. I fear that this does not provide lasting hope to the genre, unless it improves graphically and makes the absolute tightest use of touch-screen interaction. This has yet to be seen.

As It Begins

I have begun the frightening business of designing my own game, so this blog will largely chronicle my process for a while.

This is a big and potentially risky thing to do, so I am beginning small with what I am tentatively calling my Game Pitch Document. This will essentially be a short GDD (Game Design Document) meant to interest those I pitch it to. Another consideration I have made is to rapidly prototype the actual gameplay. For this I will probably use unity since it is getting to be good to know how to use it. This approach also helps show that I am capable of developing as well as designing.

The delightful Lori Cole, a huge inspiration of mine, let me know that there is a book called “Art of the Start” which I will be getting. I have also found numerous internet resources that have been immeasurably helpful. My only concern is what I have learned about the compensation game designers get – another reason to make sure and get an actual salary job programming for the thing. From my understanding, getting 5000$ is considered ‘good’. I would rather kickstart the game myself and at least be in control of an IP that means immeasurably more to me than such a pittance. But we shall just see.

Privacy in the Cloud

I will preface this entry by mentioning that I am not a legal scholar. I am acting purely on my own research.

I am currently beginning work on a very important project. Naturally, I am fearful of my designs slipping out or being plagiarized before I have a chance to make them real. But I wanted to do my work on the cloud – its fast, easy, convenient, and works across all sorts of platforms. This led me to do a lot of learning on the subject and here are my conclusions. I hope any readers out there will append to my knowledge and/or learn from it.

  • Many cloud services want some level of access to your information for the purpose of gathering information. This does not bother me. All it does is help them market products I might very well be interested in. No big.
  • Many cloud services give themselves too many rights in their Terms of Service. This does bother me. As of this writing, Google’s is so vague and so liberally worded that they could probably get away with doing anything they want with your content. Will they? Probably not. I acknowledge that. But it shouldn’t even be a possibility.
  • There are alternatives to anything, and many will attempt to make it an economical advantage to be on the side of privacy advocates.
That last point brings me to two suggestions I would like to make. They have both served me well already.
SpiderOak – The Dropbox/Google Drive alternative. It’s not nearly as straightforward to use as those cloud storage devices, but get this – its so encrypted, the owners of the service cannot even retrieve your password for you. They have a 100% zero-knowledge policy. It is as private as an external hard-drive, without the risk of hardware failure.
Zoho – The Google Docs / Google EVERYTHING alternative. While they don’t plug their product on the basis of security like SpiderOak, they do provide a google-like collection of cloud-based apps with a TOS that does clearly state that they may not lay their fingers on your information except where you tell them to. The basic, free version is pretty neat, but I respected them enough to upgrade. It’s a measly fee to actually pay for the service… how does 3$ a month sound? :)
Again, I hope this helps someone. Cloud computing is the future. It NEEDS to be secure, and we need to take our business to the companies that respect security. I hope more people will start acknowledging that.

You can take the Gamer out of the Adventure, but….

It’s been a rather atmospheric night for me … alone at the house with a translation of the original “Ring” horror novel at my fingertips. I could swear my house is making more noise than usual tonight. This is some solid writing, but I can’t help thinking that this book should have been an adventure game instead. I find myself pausing constantly to think about what I believe the answer is to some clue. I want to solve the mystery – not this Asakawa guy.

And that is why it amazes me that such stories are widely loved, yet interactive fiction is marginalized. I honestly think that if people really understood what games can do for a story, they would want to be a part of it. I completely understand that the desire to mindlessly blow stuff up is going to be a big seller out there, but a deep, compelling story that you can interact with simply tops my list of ‘fun’. Table-top RPGs prove that a lot of people feel that way, I think. So are adventure games just not marketed right? Or is it just that society is unwilling to look at games as an art form?

The urge to work on my design is growing.

 

Crowd-funding

A very cool link has been brought to my attention regarding crowd-funding for Linux games. This sort of connects with my earlier post about it. You can check out a good listing here.

Note that you don’t have to use Linux to enjoy crowd-funding. Kickstarter has really opened the door to a whole new way of doing things that totally cuts out the middle man. Whether this will prove to be a revolutionary new business model remains to be seen, but with a number of recent successes, it would appear that there is a good chance for it.

My personal focus out there has been to resurrect the giants of Adventure gaming – the diaspora of Sierra Online and Lucas Arts. So far, so good. Here are some of the remaining campaigns:

Adventure gaming giants Tim Schafer, Jane Jensen and Al Lowe have all had enormous success already. I truly hope certain others have taken note!