Abstraction in Game Programming – A Guide

In my limited experience in the development world I have run into a huge problem for programmers that many do not always seem to realize. This problem comes with the idea of abstracting code. Or, in other words, making code more flexible and usable in multiple situations. So here I am going to write a guide for myself and for anyone else who is interested on how far you should abstract your code in different situations.

DISCLAIMER! Let it be known that I am in no way an expert or a seasoned veteran of the industry and this guide could be completely wrong so take it as you will, but as it is this comes from my personal experience on the topic and I find it valuable. To each their own and I hope that you do take something away from this post either way.

Anyways! To help understand what I am trying to convey here is a key to some of the terms I will be using.

Levels of abstraction key

- No abstraction – Code that must be replaced so it can be maintained without an abundance of issues.
- Low level abstraction – Creating base-line abstraction for objects that needs to be done for the game to succeed.
- High level abstraction – Most objects are generic and/or extremely flexible.

So, let’s start with creating a game!

When you are developing a game you should be using low level abstraction. Why? Because the high level abstraction should have been done while developing the game engine. When developing the game itself you should stick to creating just what is needed for the game to succeed. Not only does this save time but it also keeps you from the pitfall of creating a game engine on top of a game engine. Milliseconds of performance time should not be something you should worry about. Make the code usable and maintainable. And if you have a connection to the internet, make it secure.

A game engine (and this should encompass your physics engine, AI engine, sound, graphics, etc.) should be high level abstraction. There isn’t much of a point of creating a game engine that will be used for a single game. If you are an independent developer and you wish to create a single game you should either use a pre-existing game engine or build on top of a framework (such as DirectX or OpenGL) using low level abstraction that will do only what you need to be done and that’s it. Pretty simple idea, right? I have seen many developers fall into the black hole of attempting to create a whole game engine for a single game with a User Interface and all only to never see the game actually produced.

Creating a prototype. This should be done within a 2-4 week period and it is completely acceptable to hack your way through this for a few simple reasons. This isn’t the final product. You need to have as much functionality done as possible in a small amount of time so the people you are pitching your game to have a better understanding of what your game should eventually look like. Once you secure money (which should directly relate to having more development time) this code should be, for the most part, scrapped. Prototypes should have little to no abstraction to save time. Prototypes are completely for the visuals and to try and convey an idea.

As mentioned before, an AI engine should be high level abstraction because odds are you will be using the same AI again in another project.

Creating a tool that does a very specific job should be low level abstraction.

If you are having a problem determining how abstract your code should be simply ask yourself these questions:

How much time do I have to make this?

Will this code be re-used/is it necessary to make this code re-usable?

Am I creating software for a developer or for a player?

Once you have done that it should be easy enough to decide how abstract you need to make your code.

Thank you for reading and good luck on your next project. :)

Posted in Blog | Tagged , , , , | Comments Off

Unity3D Stealth Engine


Tyler Camp and I discuss different ways to approach building a stealth engine for the AI in a 3D Unity game and prepare for the development process.

Posted in Studies | Tagged , , , | Comments Off

Strategical and Tactical Reasoning with Waypoints

Hey guys!

So I took the weekend off from daily studies and I am back to begin my next big study!

Today I spent my time studying up the concepts of Strategical and Tactical Reasoning with Waypoints and tomorrow I will start applying these concepts in code.

First off, something that should be kept in mind is that A* is also used in 3D games. However, instead of having tiles for each point on the map the nodes are placed in a node-graph over the map and are either generated automatically or placed manually by a designer.

Since these nodes inherently contain information about the relationship between positions in the world, this can be exploited for other uses besides pathfinding. This can become a powerful tool for NPCs through fast map analysis. To make it more realistic it is also important to take into account the idea of visibility. This can help the NPC determine the cost of the node based on how safe it is or if they even know it is there.

This is what I plan on focusing on for this week.

According to AI Game Programming Wisdom an effective technique to store connectivity and visibility information is in a bit string class that consists of a string of bit of arbitrary length with operators for Boolean operations such as and, or, and not. So this is the first thing I will set up to determine what is safe for the NPC and what is dangerous. Afterwards I can start looking in to intelligent attack positioning .

Posted in Studies | Tagged , , , | Comments Off

Generic A* Machine Part 5 and 6

Finally!

Over the last couple days I finally finished up the beginnings of my Generic A* Machine. It works very nicely and as you can see from the above screenshot it only takes a couple of lines of code to successfully run it now!

The Goal class was definitely the most challenging implementation because determining cost of a tile can get very abstract and… well… interesting.

Now that I have finished this (with plenty of add-ons coming in the future!) It’s time to move on to the next big study. I will either be doing swarm AI or tactical group issues and intelligent group movement. Not sure which one I want to do just yet. We will find out tomorrow though!

Posted in Studies | Tagged , , , | Comments Off

Generic A* Machine Part 3 and 4

Yesterday I fell asleep before I could make a post about the work I did so I will be doing a double daily study post today!

Part 3:

Yesterday I spent my time moving the Node class over and filling out the storage and goal classes. Along with that I cleaned up some unnecessary code I had written that I thought might be needed. Afterwards I ran the code and fixed a very large amount of bugs! The bug fixing took most of my time so I stopped after I finished.

Part 4:

Today I did some more clean up and filling out of the machine. I finally got into a comfortable enough position to start filling in the code for the functions. Yay! Things are actually starting to happen. I created the functions for setting the source point, the destination point, and adding nodes to the closed list. I also made sure the code ran smoothly after actually creating an instance of the Machine which was very exciting to see work! Tomorrow I should be able to completely finish implementing the algorithm and then I can work on creating multiple different maps to try and plug in to the machine and see it work it’s magic. Along with the bug testing. There is always a lot of bug testing.

Posted in Studies | Tagged , , , , | Comments Off

Generic A* Machine Part 2

Today I spent most of my time setting up all of the classes to begin the construction of my A* machine.

Right now I have the empty shells of the AStarBase class, the template for the Machine itself, the AStarGoalBase class, and the AStarStorageBase class. Creating a full fledged A* machine takes several months so I will only be making a simplified version of one that I can expand upon as I need on a later date.

I made the beginning of the infinite loop of the AStarBase class that will be used to actually run the algorithm. The majority of the work is lined out at this point (which is half of the battle) and all that is really left to do is fill out all of the functions and make sure they work properly. I still need to re-import the Node class which won’t take long and then I can get working full throttle tomorrow on the storage class and the goal class. The easiest part and the hardest part in one day!

I hope this works out!

Posted in Studies | Tagged , , , | Comments Off

Generic A* Machine Part 1

This post is a bit late, but is basically just a study of the theory. No code written just yet, that will be for later today.

I will be paraphrasing again from AI Programming Wisdom.

So this study will be about taking what I did previously and making it into a template that can be used for many things besides finding a path. For instance, grabbing all the tiles besides the path found, choke-point detection, terrain analysis, weather creation and movement, animal migration routes, and AI wall building.

There are generally four different parts to the machine:

The Storage – A place to hold all of the data collected by the machine.
The Goal – The class that determines what really happens in the A* engine. Holds data such as the unit, the source, the destination, the distance methods, and the costs.
The Map – The search space of the engine.
The Engine – The heart of the operation. Where the majority of the computing happens.

Later today I will be breaking these parts down and implementing them in my code. So stay tuned!

Posted in Studies | Tagged , , , | Comments Off

Basic A* Pathfinding Part 2

Yes, I know. It’s the same day as Part 1. Reasoning, these are supposed to be only 2-3 hour studies per day and I will be too busy tomorrow to do one. So I did two in one day.

After much head scratching I finally got a simple version of the A* algorithm working. It’s set up to work with a hard coded 4×4 grid for simplicity’s sake. And because it is my first time attempting this. Took me a good hour of bug fixing to finally get this bad boy running and I am very happy to see it working correctly!

Now that I have this running correctly I can abstract the algorithm and make it work on any sized grid-based map and implement a more complicated cost system with grid nodes that are impassable and etc. etc.

Up top is a picture of the working console application.

Sunday I will start my study on Generic A* Machines!

Posted in Studies | Tagged , , , , | Comments Off

Basic A* Pathfinding Part 1

Today’s study consisted of learning the concept behind A* pathfinding and beginning a simplified, hard coded version of it to test my knowledge on the subject.

(I will be paraphrasing from the book AI Game Programming Wisdom)

The A* Algorithm is a grid-based pathfinding tool that can be used in 2D games. It is based on four different terms. Nodes, distance, cost, and map.

A map is the space that the algorithm is using to find a path between two points.

Nodes are the structures that represent positions on the map.

Distance is used to determine the suitability of the node being explored.

Cost is the abstract variable that differs depending on the application that A* is being used in. For instance, cost can be time, energy, and money depending on the situation.

Three important variables that nodes hold are the fitness, goal, and heuristic values.

Goal is the cost to get from the starting node to this node.

Heuristic is the estimated cost to get from this node to the goal.

Fitness is the sum of goal and heuristic. Fitness is the best guess for the cost of this path going through this node. The lower the value the better we think this path is.

The last important variables of A* are the Closed list and Open list.

The Open List consists of nodes that have not been explored.

The Closed List consists of all of the nodes that have been explored. A node is considered explored if the algorithm has looked at every node that is connected to this one, calculated their fitness, goal, and heuristic values, and placed them on the Open list to be explored in the future.

Today I worked on setting up my first test program for the A* algorithm by setting up the Node class, a Point class, the Map, the Open and Closed lists, all of the nodes that correspond with the map, the function to find the starting position, and find the children of each node.

Tomorrow I will finish up the application by finishing the function that calculates each nodes fitness, goal, and heuristic values, adding nodes to the open list and closed lists, as well as adding nodes to the path and updating the path.

Posted in Studies | Tagged , , , , | Comments Off

Video Games ARE Art: Arguing against Roger Ebert

I am privileged to be surrounded by people who are like-minded with me when it comes to video games. Video games being an art form goes without saying to us. In fact, it is pretty hard to find places on the internet that will argue against video games being an art form as well. When I google “Video Games are not art” the first page only has one link to an article that is actually arguing for this. (There are a few near the bottom that have the title “Video games are not art”, but upon further inspection these were either April fool’s jokes on gaming forums or articles claiming that video games are “more than just art”.) This one article happens to be written by a highly respected film critic named Roger Ebert. After reading it I became a little frustrated and many of the comments on it only served to show that there are far more people out there then I first expected who have very little respect for video games.

So, today I would like to take the time to post my argument against Roger Ebert’s article on why “Video games can never be art” (Yes, I plan on arguing with a dead guy) and perhaps a larger glimpse into why I, and many others, believe they are.

If you want to read Roger’s article to gain some basis of what I am going to be discussing, you can find it here.

The first point Roger makes is that “No one in or out of the field has ever been able to cite a game worthy of comparison with the great filmmakers, novelists and poets” and he continues on to add “painters, composers, and so on”. This is a tricky statement. In what exact way are we comparing them? One art form can’t be compared to another because they are inherently different. Thus the reason they are their own forms. One thing all art does do is tell a story. If we are talking about comparing the stories told by the different art forms and whether or not it is reminiscent of “the greats” (which is done often) then I’d say yes, video games can do this and have done this. The Bioshock series by 2K games is often compared to the works of Ayn Rand and Aldous Huxley. The Last of Us by Naughty Dog has been compared to The Road by Cormac McCarthy. Heavy Rain by Quantic Dream is easily comparable to the Sherlock Holmes novels. And it is important to also keep in mind how young of an art form video games are. They are literally only 41 years old. The next youngest art form is film which has been around for over a hundred years. I’d say they’ve actually come quite a long way in the time they’ve been around and will only get better.

bioshock_rapture-wide

His next point is “one obvious difference between art and games is that you can win a game. It has rules, points, objectives, and an outcome”.

This is a hard topic to tackle because traditionally all other art forms do not have rules, points, or objectives for the viewer. Traditional art does not interact with the viewer at all. Video games are so inherently different from all other art forms that you can viably take either position on this subject. However, I am a strong believer that the definition of “what is art” is a forever changing thing. Perhaps video games do not fit the requirements for a traditional art form but neither did film when it first came to be. Change in art is what keeps art alive. So what is the true definition of art? This isn’t an easy question to answer and I am sure there are a bunch of books out there written about it but I am going to do my best to at least give you my quick definition of art.

Art will trigger emotions. Think of the ending of Titanic or Beethoven’s Ode to Joy. They swell you up with emotion. The best pieces of art will make you feel multiple emotions all in a matter of moments. Not all art pieces have as much impact as something like Titanic but all art will trigger some sort of emotion in you.

titanic_the_final_moment-wide

Art is man-made. Nature is beautiful and wondrous. It can trigger emotions, tell a story, and it has made us curious since the dawn of man. But it is not art. Instead I would call it one of the inspirations for art. Art must be crafted by someone because it gives a deeper meaning to the piece. We can better relate to art because as humans we all understand each other to some degree. The difference between looking at a beautiful landscape and looking at someone’s painting of one is that the first is simply beautiful but the second is saying “I want to share this beauty with you”.

DCP_4292

Art tells a story. Storytelling is an important part of life. We learn from stories and we grow closer to others by sharing them. Every painting or song is telling the story of the artist that created it. It’s just in a different context. Sometimes you can’t use words to describe a story you want to share with others. It’s an attempt to not just tell your audience what you saw or how you felt but also to let them experience it for themselves. It’s storytelling in very great detail.

Art creates curiosity. A good piece of art will raise questions. Sometimes it will give you the answers and other times it will leave you to wonder. The question can be as simple as “What is going to happen next?” or “Why did the artist make what they did?”. No matter what the piece is there is always a question to be asked.

By these defining points I think it is safe to say video games are art. If you doubt me try playing some of the following on your own.

The Bioshock Series

The Half-Life Series

The Last of Us

Dishonored

The Portal Series

Heavy Rain

or, if you want a quick flash game

The Pretentious Game

Ebert goes on to bash the games Waco Resurrection, Braid, and Flower. I’ve never played Waco Resurrection so I don’t plan on defending it. In his argument against Braid he chooses to begin by relating it to chess, saying the ability to go back in time and correct your mistakes in chess “is known as taking back a move, and negates the whole discipline of the game.” This seems to be an attempt at first critically analyzing Braid in the form of a game and, because of one of it’s core mechanics, it must not be a very good game because it has no discipline. He continues to say “nor am I persuaded that I can learn about my own past by taking back my mistakes in a video game.” It is fairly obvious Ebert never took the time to actually try and play Braid. As for the game Flower he simply choses to tell us that it is not, in fact, a game at all.

flower-game-screenshot-2

This last bit of bashing sounds a lot more like a stubborn old man who refuses to give video games a chance rather then a truly informed critic who has taken the time to study these games. The fact that he didn’t even take the time to play them and come to a conclusion from what he actually experienced tells me this part of his argument is lacking a good foundation if any. So I am not going to waste my time responding to it any further.

He closes with asking “Why are gamers so intensely concerned, anyway, that games be defined as art?” Its not just gamers but the developers that are concerned and the reason why is because we know we are making/experiencing art and we want it to be respected as such. Yes, there are mind-numbing games that are not art but that is the case for all other art forms as well. For instance, I don’t consider Jersey Shore art but it is still the product of film. There are game developers pouring their hearts and souls into the games they make trying to tell their stories and making people laugh with joy and cry deeply over their creations. These developers deserve to be called artists and their products art.

Posted in Blog | Tagged , , | Comments Off