Disclaimer: I am not an expert in code. I am learning as I go along and, therefore, some of the terminology, and even some of the execution, is incorrect. However, I keep this record in the hope that I can, on the one hand, plot my own development as a programmer and, on the other, aid through vicarious learning.

Thursday, 9 August 2012

Cow's about that?

There’ll be a variety of different occupations in the game; some you can dive straight into and others will require training or apprenticeships. One of the jobs you can take part in pretty much from the off is farming.

I want the game to be accessibly complex - i.e. an aesthetically simplistic interface concealing a ridiculously complex backend. The foremost component, and arguably most complicated, of this is the AI. Dynamic economies, weather, day/night cycles, war are all, in their own right, difficult to create, but ultimately they both derive from and - at the same time - directly affect the AI lying within the inhabitants of the game world. This is the same with regards to player and non-player character statistics.

I decided to start with an animal, and - for no particular reason - I decided upon a cow. 

Before committing to any kind of code, graphics, or whatever, I needed to work out the rudiments of what forms a cow. What is the behaviour that defines this creature? Well, I figured that it had a few: it predominantly ate, it can breed - and so this requires it to find a mate, it sleeps, it can attack predators, or flee and it can be born and ultimately die.

Great stuff. So, how could I translate this into code so that I could actually begin to make a cow? Making a new script, inspirationally entitled cowAI, I began to put together a finite state machine.

For those in the know, don’t bother reading the next part, but for the benefit of others who may not have come across this topic before, stay a while and listen.

Finite State Machines

A finite state machine (FSM) is basically a series of ‘states’. Each of these states contains methods that can be called upon to discern a certain action. These states cannot happen simultaneously - they move in and out of each other when criteria have been met. Changing from one state to another is called a transition. 

To paraphrase, it is essentially defining different things that you want - whatever it is - to do. For example, a tomato is planted, it grows and then it is either harvested or dies. So, the ‘states’ for a tomato would be ‘plant’, ‘grow’, ‘harvest’, ‘die’. It’s a simple as that. The FSM itself merely houses these states so that a transition can be made between each of them.

A few worthy notes regarding the FSMs - never call it within your update method as it’ll jam-up (updates are called every frame). I generally begin the coroutine within the start method, as this is only called once and then the FSM intrinsically runs throughout anyway, by virtue that certain conditions and methods are called in order to change it.

Anyway, back to my cow.

So, having thought about what I wanted my cow to do, I began to code it. At this stage I wanted to keep it simple, so the beginnings of my FSM looked thusly:

public enum State {
Search,
Eat,
Return,
Milk,
Flee
}

’Search’ was essentially my default state for the cow. When creating FSM machines some programmers opt to have a ‘setup’ and ‘idle’ state, so that the AI is arranged and then goes into waiting. However, because I want my world to be ‘alive’ the cow should always be doing something, and I call my setup parameters in the awake or start method anyhow.

Having defined the different things that I wanted my cow to do, I now had to go about actually creating those things. I’ll only discuss one method here, so if people are desperate to find out more they can message me, I suppose.

Firstly, I set up the FSM as a private IEnumerator, because it returns a value:

private IEnumerator FSM() {
while(_alive) {
switch(_state) {
case State.Search:
Search ();
break;
case State.Eat:
Eat ();
break;
case State.Return:
Return ();
break;
case State.Milk:
Milk ();
break;
case State.Flee:
Flee();
break;
}
yield return null;
}
}

This is what houses and operates the FSM and it is started a coroutine in the start method. _alive is just a boolean value to ascertain if the cow is alive or dead - if the poor old cow is dead, then fanny adams happens. Otherwise, the initial state is search.

Search: as the default state for my cow, I decided that its primary objective should be finding food; in this particular case, a fresh supply of grass. In terms of game objects this meant the creating the cow, and the grass. The grass would, therefore, become the cow’s target and the cow would walk towards it. Upon touching the grass, the cow would transition into eat, which is a separate method.

In it’s most basic form, the method looked like this:

private void Search() {
target = _grass.transform;
currentSpeed = walkSpeed;
Move();
}

So, what the fuck is going on here?

Well, first off, the method is void because it’s not returning us a value - it’s simply defining behaviour. Within the method the target is set to be _grass.transform. _grass is a public GameObject that is, actually, defined within the start function, but can be manually altered through the inspector because it is public. The .transform indicates that I want the target to be the transform of the grass, which is the element that contains its vector3 reference (it’s x, y, z axis points).

The currentSpeed is a public float that defines how fast the cow should move - in this case it is by its walkSpeed, which is also another float that I have set at a certain number. This is basically the amount that the movement value will be multiplied by to set the movement speed of the cow within real time (delta time).

The next item is actually another method that makes the cow move. When I began coding this game, I decided that I was going to create my own movement system, as I didn’t like the simplicity of the character controller within unity. However, I will discuss this in a separate post as it got pretty ugly pretty quickly.

So, within this method, that is called within the state of search, the cow finds a target and begins to walk towards it. Wow.

When the cow gets to the target I want it to stop and eat the grass. This is done through OnTriggerEnter - the grass that the cow has touched has a sphere collider attached to it that is a trigger. The grass itself is also tagged as ‘grassSupply’:

void OnTriggerEnter(Collider other) {
if(other.CompareTag("grassSupply")) {
_state = State.Eat;
}

So, when the cow touches the GameObject that has the tag “grassSupply” (the target the cow has been moving towards) it provokes a transition into the state of eat. This next method, of course, has a whole heap of other shit that happens.

To summarise - the state of search contains the following actions:

Find target - move towards target - reach target - eat.

And that there is the rudiment of what makes a cow do what it does. After working on it further, naturally, it has become more complex; but it was important for me to make even the simplest creature have as robust an AI as possible. Humans are basically advanced animals who do more things, and so - believe it or not - once the cow has a brain it’s not too much of a jump to make it a person.

I am going to make a cow a person.

Monday, 6 August 2012

The Story So Far...

A robotic-looking farmer admires the view of his static farm.

Okay, so here we have a screenshot.

Here's what's basically been going on the last couple of months... well, years.

The Story So Far...


I started making games when I was very young on a fantastic piece of software called Klik and Play, which later developed into The Games Factory and is now, sadly, no longer available.

These were dead basic bits of software, but at the age of around 10-or-so it was a great way to dive in and get to grips with the basics of game logic. The majority of any complex development within Klik and Play was through algebra and nested 'if' functions - but this complexity could only go so far within the fairly stringent walls of what was, ultimately, an imaginative and engaging piece of software. The mix of a UI and [some semblance of] a backend was perfect for me, as my brain needs almost instantaneous visual results to release endorphins, otherwise it seizes up and feels like it's accomplished nothing at all.

Following this, over the years, I encountered other such 'game making tools'. Software like RPG Maker, in its various iterations, and even going as far as to make functioning games within Microsoft Excel (?).

I think the thing that always put me off tackling code-based development was the fact that it seemed so insurmountable and impenetrable. And, to be perfectly honest, it is. The biggest stumbling block I, and probably many others without any training, face is actually admitting to yourself that what you see before you is actually there, i.e. coding is hard, especially with no training. But that doesn’t mean that you can’t do it.

And so I started to teach myself Java. This went all right, and I began to get the hang of things. Methods, functions, variables - whatever, it all started to fall into place. Learning the definitions and theory is difficult, and time consuming, but it’s not the hard part - it only gets really difficult when you realise that  coding is an applied practice.

You can learn as much general information surrounding the topic as you like, but until you actually get into a script and start trying to do something with it, it doesn’t amount to much.

After learning the basics of Java and giving some basic development a go I started work on my first project - a FPS about a robot who is sent to a dead planet that has no light to take atmospheric readings. I was pleased with the results, but ultimately not the direction of the game, and so I moved on to another. This project was more ambitious, but again, I wasn’t sure about where it was heading.

I had chosen to use Unity3D to develop my games in. Why? Well, for starters it’s free. It’s also, like Klik and Play decades before it, a blend of UI with backend control; the perfect, and most rewarding mix.

After haphazardly putting together these first two games it quickly struck me that I had essentially grabbed the first tool in the box because it was the closest one to me. This didn’t make any sense at all. 

If I wanted to cave in a man’s skull I’d be there for months with sandpaper - better to use a hammer.

And so I grabbed, for me, what was to become the hammer I would cave the proverbial man’s skull in with. And I began to cave his skull in.

With C#.

In total I spent around 45 hours getting to grips with it; learning, making mistakes, learning, looking shit up, and eventually getting it to work.

And I haven’t really looked back.

So, from this point onward I have been coding my latest game project within Unity using C#, and I’ve been thrilled with the results.

For other indie amateur game developers, whom I’m sure can relate to the feeling, the sense of accomplishment is staggering. To spend a whole evening trying to figure out what the funk has gone wrong with a single line of code that is nestled within five hundred others lines, and to instantly know how to solve it is rewarding. Then to click ‘play’ and see those lines come to life is something else... 

Succeeding posts will be exclusively about the game I’m making, but I figured it might be nice to lay some backstory, just to give some insight into how inexperienced I am, and the challenge that I’ve set myself. I hope it’s going to be fun.

First Post!


Introduction

I’m writing this blog to keep people up to date with the progress of my pet project, which is provisionally entitled ‘Sex In Medieval City’.

The blog content will contain general development, technical and screenshot updates, as well as my thoughts about creating a game - from scratch - on my own.

I am looking for feedback, thoughts, suggestions, and any features you would like to see in the game. I’ll try to update this blog as and when I make progress with the game itself, so that it has some semblance of chronology.

I’ll make it look prettier as I go along - I’ve just made the background a wicked pink to separate the weak from the chaff!

Look forward to hearing from you!