Tuesday 21 May 2013

Runes: Songs of the Gods - Version 1.0-1.2

Version 1.0 - 1.2 - Fixing the Match-Making

I'm going to try and make this post less wordy. Now that we can at max select three runes I worked on the next major bugs.

In this build I fix:
  • Matchmaking not working corretly when runes are missmatched.
  • Matchmaking not working correctly when runes are matched.
These bugs happened quite often and as very noticable so I went about fixing it. It's worth noting that most of this stuff I write down in my little notebook. So I am hoping all these posts don't sound to chaotic.

When a fail state was reached it would still count a score point if the following was true:

Rune 1 = Val_x
Rune 2 = Val_y
Rune 3 = Val_x

This was due to the code I had at the time, which I was super proud of because I thought it was fancy. Obviously I was being too fancy for my own good!

    void CheckArray(){
        string tempStr;
        tempStr = RuneSet[0];

        for(int i = 0; i < 3; i++){
            Debug.Log("RuneSet[i] is: " + RuneSet[i]);
            if(tempStr == RuneSet[i]){
                isMatch = true;
            }
            else{
                isMatch = false;
                MakeGodLight = false;
            }
            CheckArrayToggle = false;
        }

Because RuneSet[0] would always be equal to tempStr it would always be a match (isMatch = true). The next rune Rune[1] could be true or false so here isMatch could be set to either true or false. After this Rune[2] could be true or false which meant that here isMatch would be set to true or false depending on if it matched Rune[1]. This meant that the little algorithm above never really cared for Rune[1]. It didn’t matter if it matched or didn’t. It only took Rune[2] into account as that was the last value reached in the loop.

My fix was much simpler. Occam’s razor I guess.

        string tempStr;
        tempStr = RuneSet[0];

        if(RuneSet[0] == tempStr & RuneSet[1] == tempStr & RuneSet[2] == tempStr){
            isMatch = true;
        }
        else{
            isMatch = false;
            MakeGodLight = false;
            BonusScore = 0;
        }

This was fixed and I rejoiced and felt cautiously optimistic that I fixed my final major bug. Even though, at times, when matching three of the same rune the IsMatch would not toggle to true. I was hoping that this was “somehow” caused by the above code and that I fixed it. But obviously that’s not the way code works and hoping is not the same as thinking things through.

While testing I found that even though the miss-matched rune bug was now fixed, the three matched runes bug wasn’t. I started debugging by hitting different combinations of runes and using crazy amounts of console output.

The bug was tied to the game objects (the runes) name and the how the Instantiate function works. The game object was being created as I mentioned in a previous post. But it wasn’t making a new game object from scratch – it was cloning the previous one. I didn’t know this until I started looking into the (clone) functionality and what Instantiate actually did.

This meant that I could at times have something like the following:

Rune1 = Stone1
Rune2 = Stone1(clone)(clone)
Rune3 = Stone1

Because I was using the game objects name (the runes name) the “(clone)” was added to the array. And obviously this meant that Array[0] != Array[1]. So I had to find a way to get rid of those pesky (clone)s!


As a sidenote, my girlfriend actualy mentioned that the (clone) name might be causing issues when she first checked out my little game in very early builds. Guess she can say "Told you so" now :D

I searched the interwebs and the best solution to my problem seemed to be renaming the game object. By now it was late and I was not thinking straight so I did dumb things like:

case 2:
RuneType = 2;
    Instantiate(Rune2, transform.position, transform.rotation);
    gameObject.name = "Stone222";
    Destroy(gameObject);
    Debug.Log("gameObject.name: " + gameObject.name);
    break;

This didn’t work at all and I wasn’t thinking through my problem at that time. What I had to do was rename the object on creation. Before it was added to the array. That meant not naming the rune in its “parent” rune as that one was destroyed. I had to add the rename code to the clone runes Start() method!

Fixing it:

I knew the only part of the Runes name I had to keep was the number as all runes were called Stone#, with the # being a number 1 to 9. So I got the number part of the rune I needed and used it in the runes new name.

Converting from char to string:

string RuneType;
RuneType = gameObject.name[5].ToString();

Put in own method:

void SetRuneName(){
    RuneType = gameObject.name[5].ToString();
    gameObject.name = "Stone" + RuneType;
    Debug.Log("gameObject.name: " + gameObject.name);
}

BAM! This fixed it and all runes appeared as Stone# in the array. Bug squashed :D





No comments:

Post a Comment