Revisiting my first ‚big’ software project

When I was about 4 years old I lived in polish countryside. My house was surrounded with wooden fence. It was similar to one shown on photo bellow.

drewniane_ploty_621x0_rozmiar-niestandardowy

(Photo source: https://www.wymarzonyogrod.pl/ogrodzenia-nawierzchnie/ogrodzenia-ogrodu/ogrodzenia-z-drewna-drewniane-poty-zdjecia,36_502.html)

Anyway, one day one of the gateways broke down – one of the bands corroded and someone had to replace it. And just before my father got his toolbox, I took my small hammer (you know, that one used to break shells of peanuts) and said something like „Hold on there, I will do it!”. And I did!

Well it maybe was not the best fix that could be applied. It probably took something about an hour, and I used dozen of nails (and lost another dozen), and my parents are joking about that even today, but the gate was legitimately fixed.

I was cute ‚lil boy back then.

But that post is not about my engineering skills back in the 1998. It is about my engineering skills back in the 2015.

If you check out my previous posts, you will find that making video games is my ‚thing’. They are reason that ultimately leaded me to computer science course at university. I tried music, art and writing (and wasn’t great at them) but programming got me really hooked.

I was fascinated with idea, that you can make computer ‚do things’. Like finding prime numbers, changing random words on website to funnier ones or displaying a huge tank battle in ‚Command & Conquer: Red Alert’. Obviously, the last one was most fascinating for me.

So I learned hard. First there was C/C++ then bits of C# and some basic concepts of OOP. By the end of first university year thanks to my passion for programming i had no problem with passing all programming related classes (other story with math – but that’s for different time). Still, I didn’t knew why my teacher got mad every time I’ve put my whole program in main() function.

Most importantly – had completely no idea how to bite the video game. Back then my most complex creation ever was a „Rock, paper scissors” game (that was written completely in main() function by the way). Then I heard about that hot video game engine called Unity. It used C# for scripting language – I knew some C# so i decided to watch some tutorials, and then create something on my own. And that was exactly what I did.

Now, dear reader when you are aware of context, let’s dive into code of that thing, shall we?

First of all – you are welcome to play it here.

This is the original „release” of the game. The only thing I did was compiling it to HTML5/JS. I want you to experience it it’s initial state.

Okay, back to business. Let’s look at class diagram generated by Visual Studio.

classDiagram

It will be a point of reference during our little journey. Let’s start from GameController, precisely with Start method.


	void Start () {
		if(isGameOn){
			BallsUsed=0;
			Paddle.SetActive(true);
			Paddle.transform.position=new Vector2(7.72f,-7.04f);
		PaCon.ShieldsStatus=100;
		StartCoroutine("SpawnNewBall");
		}
	}

 

For those not familiar with Unity – Start and Update are special methods that are executed automatically. Start is called on start of object existence, as sort of setup, and Update is called on every frame of game.  So… why is there condition statement checking state of isGameOn? Maybe, if we look at isGameOn declaration it will clarify some things…


public bool isGameOn=false;

Well, there it is. Initialized with default value. So Start method does indeed nothing on start (I know, it was hard to see that, because of shitty code formatting).

But there must be some way to set isGameOn to true, right? Oh, look – there it is!


public void NewGame(){

isGameOn=true;
PC.Start ();
AudioSource.PlayClipAtPoint(startUpSound,transform.position);
newGameButton.SetActive(false);
gameOver.gameOverScreen.SetActive(false);
//HiScoresTable.SetActive(false);
Start ();
}

So, as you see it sets isGameOn to true does some changes to UI and then… calls Start. Not only it’s own start. Also in PC which stands for PointsCounter. Let’s see what’s in there.


// Use this for initialization
public void Start () {
BallsUsed =0;

}

(That comment appears in script files generated by Unity)

Oh, so it just sets BallsUsed to zero?

Okay, let’s calm down. I will try to explain how the game handles failure state. We are now diving into GameOver class


void OnTriggerEnter2D(Collider2D col){

if(col.tag=="Ball"){
//Game will be displaying some "funny" messages
//RandomNumber=Random.Range(0,4);
//gameOverTexts[RandomNumber].SetActive(true);
//gameOverTexts[RandomNumber].transform.position= new Vector2(col.transform.position.x,col.transform.position.y);
Destroy (col.gameObject);
Debug.Log("Collision!");

GC.BallsUsed++;

AudioSource.PlayClipAtPoint(gameOverSound,transform.position);
if(GC.isGameOn)GC.StartCoroutine("SpawnNewBall");

}
}

OnTriggerEnter2D is another special method in Unity API. It is called whenever trigger is, well, triggered. It also takes Collider of object that came in contact as parameter. Wait… trigger? In GameOver class? Yeah, since GameOver script was attached to Walls that purpose was to destroy balls coming into contact with them. So that basically changes some variables in GameController class, that to be honest could be wrapped in one method.

But we still didn’t found what we were looking for. So maybe FixedUpdate will give us the answers?


void FixedUpdate(){
if(PC.ShieldsStatus<=0){
if(GC.isGameOn){

Paddle.SetActive(false);
gameOverScreen.SetActive(true);
GC.isGameOn=false;
GC.newGameButton.SetActive(true);
//HiScoresTable.SetActive(true);
AudioSource.PlayClipAtPoint(gameOverSound,transform.position);
}

}
}

FixedUpdate is Update equivalent used when dealing with game physics. So… Not in case shown above. And also it checks variables in GameController in EVERY frame. That (and some other cases in this codebase) is probably reason why this thing works so slow (an throws OutOfMemory exception on Firefox).

I also found interesting the way how MusicScript works


void Start () {
MusicButon.text="Music: On";
IsMusicOn = true;
GC.Music[0].SetActive(true);
GC.Music[1].SetActive(false);

}

// Update is called once per frame
void Update () {
if(IsMusicOn){
if(GC.isGameOn){
GC.Music[1].SetActive(true);
GC.Music[0].SetActive(false);
}else{
GC.Music[0].SetActive(true);
GC.Music[1].SetActive(false);
}
MusicButon.text="Music: on";
}else{
GC.Music[0].SetActive(false);
GC.Music[1].SetActive(false);
MusicButon.text="Music: off";
}

}
public void SetMusic(){
IsMusicOn = !IsMusicOn;

}
}

I think, that words „not the most efficient” are now in your head.

So I probably could go on and on for pages what is wrong, and for some more how to fix it (fixing this may be hard… but isn’t that great idea for blog post series?). Anyway revisiting my first big software project was fun experience. The game also was fun! It had some unique mechanics, and I personally like it’s arcade cabinet style (despite it’s shitty UI). I probably  wouldn’t be where I am right now as a developer without making that game.

So let’s end this little code review with ExitGame class.


public class ExitGame : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
public void Exiting(){
Debug.Log("Exiting");
Application.Quit();
}
}

Revisiting my first ‚big’ software project

Project goals for this month

Hello there!

It’s been a while since I wrote something about Simulation Pills. But to reward your patience I’ve decided to give you peek into whatever I am planning for this month:

  1. Implementation of item system
  2. Automated need based crafting
  3. Non-telepathic communication between pills

So there you have it. Hopefully I will be able to obtain goals listed above, and back up project with some more commits as the time passes. Also I wold like to write more „quality” posts, like that one.

That’s all for now!

Project goals for this month

Types of games, that do not need a lot of level design (kind of)

As a programmer who likes to make games I often find myself in situation where I have to make some playable content/levels. Because let’s be honest – even the best programming patterns/practises does not really matter for player. What matters is content. But I (and propably many of you) don’t feel like level design master. But fortunetly there are ways to make games without designing levels, and in this article I decided to aggregate some of them.

System based games

In this approach you are designing game around systems, that are interacting with each one, sometimes even without player’s input. What’s interesting, player is often treaten as just one of entites presented in game’s world.

Good example of that approach is of course – Civilization. Every player, doesn’t matter if human or AI has set of similar ways to rule it’s empire and same goals. Also every outcome from Civ playthrough is valid – from taking over the entire world in future, to being flooded by barbarians in ancient times. And it is still fun!

GandhiMeme

Gandhi with his powerfull and peacefull message

Another example which I enjoyed was a small indie game called Westerado. It is closest thing to Red Dead Redemption on PC that you can have, so deffinetly check that out.

20170328111210_1

„The Ecstasy of Gold” plays in your head

On the beggining of the game your family is murdered and your farm burned down by some mysterious desperado. In his last words your brother gives your some clue about the responsible one. Rest of the game is revenge/detective quest to find this guy and put the bullet between his eyes. The detective part includes talking to other characters in game world, but with one interesting twist – you cant draw your weapon at any moment during conversation (actually outside of them too). That action can end in many ways, one of them being pierced with bullets by other people who may not like your voilent behavior, or even getting the answers from person being at gunpoint.

20170328112456_1

There are of course other excamples of game being in most part deigned around systems. Metal Gear Solid, XCOM, Fallout are just few of them.

RNGs

Random Number Games (or procedural generated games if you like it more profesionally) are games where content is in most cases generated from randomized mix of prefabricated components that in most cases work well with itself in many combinations. Theses components could be for example pre-builded rooms for dungeon crawler, or set of events that player can ecounter during playthrought.

Good example that comes into my mind is FTL – Faster Than Light. In this game you are in control of a spaceship and it’s crew. Yor mission is to deliver crucial data for Federation that can help them win the war over Rebelion. The catch is – whole Rebel fleet is after your one small ship, and most of the time they are just few FTL jumps behind you.

20170328234458_1

Believe or not, but it IS easy to get your ship in presented state

Game is mostly about ship and crew managment. Other important aspect are these random events. After every jump one is taken from event pool, and player has to deal with it’s consequences. Sometimes it’s a space station sending distress signal, friendly ship with resources to help you in your mission, but most times it will be rebel/pirate/rouge ship really happy to blow up your life support system to give your crew horrible death because of oxygen deprivation. Guess why I never completed that game.

Another example are all these endless runners. That concept was propably covered from every possible angle, but I will try to give my opinion anyway. With strong mechanic it could be long lasting, addictive fun (I mean, Flappy Bird, Temple Run, etc.).

And it would not be completed list without all those rougelikes/lites. Games like Enter The Gungeon, Crypt of the NecroDancer or Binding of Issac. As I mentioned before, these are games with prefabricated dungeon modules, that are put together by magic and random number generator.

Simulations

I had hard time putting these on my list, but here it goes. My resoning for that is as follows – if you are creating simulation, it does not require you to build levels around some mechanic, but recreate reality that you understand and have knowledge about. For example, if you are creating let’s say forklift simulation, you have to know it’s „natural enviroment” (wartehouse for example).

In other cases, if you are creating battlefield simulation, all you have to do is take Google Earth’s satellite pictures of area that will fit your game and recreate it in your engine.

When I was young I played a lot of Operation Flashpoint: Cold War Crisis (today it’s called ARMA: Cold War Assault because of licensing problems between Codemasters and Bohema interactive). What shocked me about that game was it’s map design. You see, Everon, Malden and Kolgujev had a lot of eastern european flavor to them. Small towns, landscapes or even small details like roadsigns they reminded me of places where I’ve been. It might be not the best for designing levels, but sure it was more reallistic.

There is vide choice of sims across the market to take inspiration from: flight simulators, car simulators, life simulators and list goes on, and on, and on…

Summary

When you are making piece of entertainment like song, you usually take instrument you are good at playing to accompany it. I think that the same thing goes to videogames. If you are good at programming, and mediocre at art, sound or level design try to take most from your programming skills.

Types of games, that do not need a lot of level design (kind of)

Moving on

So that was pretty lazy weekend for me. I’ve decided to have some kind of reboot. I was watching „Under the dome” and „Vinyl” (amazing shows, totally recommended), reading newest Jaroslaw Grzedowicz book called „HEL3” (with cover reminding me the last DOOM) and hanging out with friends. I desperatly needed that.

So having that out of boat, here is update on the project:

It is not much – if pill does not know the location of need fullfiller it starts moving in random directon, until fullfiller is found. It looks kinda rad to be totally honest. Just like nomads, vagabonds (call them what you will \m/) searching for new place to live.

Still it would be fun, if they searched area in organised way with friends. I will propably focus on that in the next update (or on something completly different), so stay tuned!

Moving on

Making game in five or so hours

Yesterday I accomplished something I wanted to do in long time – made (almost) full game in short time.

The game is endless runner, where player controls team of soldiers charging towards enemy.

It’s on GitHub , here.

Game was made in GameMaker.

Now, the question that propably is in your head – Why?

That’s the game design strategy of one of my favourite game designers – Jonatan Söderström (aka. guy who was co-creator of Hotline Miami (Hotline Miami is one of my favorite games FYI)) .

And it kinda works. Putting myself on strict time limit made me focus more on game mechanics. I had only few hours to make something that is engaging. Of course, it’s still a prototype, but prototype that shows basics of a game.

I will work more on that in future.

Making game in five or so hours

The solution

Recently I had very sad post, about certain things in my project not working too well. Today I found and implemented solution to that issues.

As I said before, I was thinking about holding default need values in XML file. That’s exactly what I did. There is an XML file with defaults in project, so you can check it out (and set your own values if you like).

Few words about the implementation. It is not rocket science, but some of you might find it interesting.

So that’s how XML looks like:

<?xml version="1.0" encoding="UTF-8"?>
<needs>
  <need>
    <need_type>Food</need_type>
    <value>50</value>
  </need>
  <need>
    <need_type>Water</need_type>
    <value>50</value>
  </need>
</needs>

I think it speaks for itself. It basically holds list of objects, where each one of them has need_type and value property. Before I’ve made parsing code I needed a data structure.I used Xml2CSharp.com, these are classes generated from it (after some changes in datatypes):

    [XmlRoot(ElementName = "need")]
    public class Need
    {
        [XmlElement(ElementName = "need_type")]
        public NeedType NeedType { get; set; }
        [XmlElement(ElementName = "value")]
        public double Value { get; set; }
    }

    [XmlRoot(ElementName = "needs")]
    public class Needs
    {
        [XmlElement(ElementName = "need")]
        public List<Need> Need { get; set; }
    }

I could propably do it myself, but saddly I am lazy person. Anyway, this solution works just fine, so it stays that way for now.

And finally – InitializeNeeds method

 public static void InitializeNeeds(PillAi pillAI)
        {
            var path = "Assets\\Resources\\Needs.xml";
            var reader = new XmlTextReader(path);
            var serializer = new XmlSerializer(typeof(Needs));
            var defaults = (Needs)serializer.Deserialize(reader);
            foreach (var need in defaults.Need)
            {
                pillAI.Pill.Needs[need.NeedType] = need.Value;
            }
        }

This solution still needs some work, but it does what it should. Default need values are no longer hardcoded.

But still, it is far from the solution I imagined. Yes, you can edit the XML but only before you build the game. After that, all asset files are packed into*.asset files. I want them to be accesible in builded version, so that is unacceptable. In the next post I will find the way of doing that.

But for now  – happy saturday! It’s the best day of the week, IMO 🙂

PS: I’ve updated „About me” page and it is in english now.

The solution

The struggle

Today something about  why this happened:

Przechwytywanie

Maybe something about operation execution order in Unity too.

First of all, let’s look at piece of code

public static void InitializeNeeds(PillAi pill)
{
    var gc = GameObject.FindObjectOfType<GameController>();
    if (pill.GetNeedsDictionary() == null)
    {
        pill.SetNeedsDictionary(new Dictionary<NeedType, double>());
    }
    foreach (var key in gc.DefaultNeedVaules.Keys)
    {
        pill.AppendNewNeed(new KeyValuePair<NeedType, double>(key, gc.DefaultNeedVaules[key]));
    }
}

What you can see is static method that defines needs for every object on scene. It should do the job. But what I get after runing the game is…

error

Not the greatest result, isn’t it? But thankfully, Visual Studio has some cool debugging support for Unity, so let’s find out what went wrong.

And because it will be important later, this is GameController’s Start method

        private void Start()
        {
            DefaultNeedVaules = SetDefaultNeedValues();
            Pills = FindObjectsOfType<PillAi>().ToList();
        }

Again – DefaultNeedValues should be initialized first in order for InitializeNeeds method to work. What actually happens is InitializeNeeds being executed first. That means, my approach being wrong.

But that is actually okay. Default need values were one of ugliest examples of hardcoding I have made, and believe me –  I have made some.

I am thinking of keeping them in some kind of XML. So my next post will touch the subject of XML parsing in Unity.



			
The struggle

A pill that speaks

In world that I am creating every pill needs to fill its needs. As you may remeber from previous post, there are two basic needs implemented right now – food and water.

These needs will be possible to refill at „Need refill stations” as I call them now. I am not 100% sure how the refilling process will look at the end, but that is not today’s post subject. Today I will tell word or two about implementing communication between pills.

So firstly – why?

Well, communication is one of basic skills of intelligent lifeforms.

Secondly – how it works?

To be totally honest, o took the cheap and easy way of giving them telepathy. Every agent stores two lists of objects. First one is list of „Friends” – these are object that pill already met. Second one is list of known need fulfillers –  objects that pill will go towards when coresponding need fullfillment value is bellow the refill trigger value.

Now whenever two pills met, two big things are happening.

  1. Pills are chcecking their friends list, to check if the other pill is there. If not, pill is added to list and becomes friend.
  2. What follows friendship status check is information exchange. Both pills are exchanging informations about known need fullfillers, and update them if there is need to do so.

Information exhange occurs also when one of pills meets new need fulfiller.

That is basically it. It may not seem as much, but it looks like good starting point.

I have to admit, the project is a bit of clusterfuck right now. Coding without plan is can be at times a crazy fun, but this time I want my project to serve some purpose (even if that purpose is weird and uncommon). So I have a question to all reading – what kind of planning method do you use in your personnal projects? And how do you set goals in these projects? Are these goals clear or fuzzy?

Oh, all code is already on GitHub.

PS. The question stated at the end of post was also tweeted. If you are into tweeting, feel free to respond me there!

 

A pill that speaks

I’ve just drawn some ugly looking UML diagram.

Well, that was one hell of a clickbait. But let’s get into details, shall we?

 

I’ve decided, that this time, since goal of the project is to serve people of the internet who want to build their own world of wonders inside of a computer, I need to document whatever I am doing with it.

And yes, I think that means UML too. But since target of this project are developers I need to provide some kind of instruction. It will be in some kind of GitHub wiki I guess.

This week, while developing some code for this project I tested two UML tools.

One of them was StarUML. You can see output of my work bellow.

skrenuml

It is not impressive (never meant to be!). All I really wanted was simple class diagram, to not get lost, and well… It delivers, sort of.  But it requires time and skill, and I am not software analysis specialist.

So I needed something simpler. Luckily though, Visual Studio has cure for my needs.

umlvs

It can be generated, and updates automatically as I edit the code. So it’s good for what I need right now.

How to make your own? Simply, really:

howtoclass

When it comes to implementation… Well there was some progress, indeed.

There is some basic intelligence implemented already. I’ve made up the following scene:

scene

We have some kind of 3D box, our Pill and lots of grass. Our AI agent has hardcoded two basic needs – food and water. When they decrease bellow some level, he seeks for fulfiller of that need and goes there. That’s kinda it.

I am not going to get into details of the implementation, because you can check them out on my repository.

As the final note – question. How do you come to topic of documenting your code? Do you use UML, or just make notes?

I’ve just drawn some ugly looking UML diagram.

Get Noticed 2017 – Let’s get this started!

Here I go again. Maciej Aniserowicz is making his big Get Noticed competition again. I am of course down for it, so I decided to reactive my blog. If you are here the first time – welcome, please enjoy your stay. I will try to post valuable content as often as I can. You may find that most of the content is written in Polish, and sadly it will stay that way for some time.

For people who already know me – i hope you will like new face of my blog.

Okay, if we have greetings out of the board, let’s discuss my project!

Simulation Pills

What is that? Well I am glad you asked.

Computers bring us big power. Power to create living, breathing worlds… Well, sort of. We are still quite limited. But with enugh ammount of skill and hard work it should be possible to create simple world and put there simple „organisms”. It is not going to be perfect mock (like Matrix), but something smaller and simpler (like terrarium).

But why Simullation Pills?

Well…

pills

Look at these capsules. What do you think about idea of these capsules being living organisms which are going to be simulated?

Well it actually does not matter, because decission was made. Also that title sounds so cool.

If it comes to my tech stack, I am going to do it with Unity3D, and Visual Studio.

So I think there is really nothing else I can do, than post link to my GitHub repo with source code.

https://github.com/damoch/simulation-pills

If you want more info about me, there is state-of-art GitHub page answering your questions.

Bye!

PS. If my broken english hurts your feelings in some way, well everybody started somewhere.

Get Noticed 2017 – Let’s get this started!