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();
}
}

Reklamy
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

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!