Artificial Experience: GAM 111 Week 11

 

Once the game was finished a script was written to automatically play the game, a sort of A.I. player.  The A.I script is attached to the players game object and was to designed to send virtual input to replicate a human player.

State Diven ai error

The AI script was created after the game and required little alteration to any other scripts. It can be activated by a toggle that  changes the value of a Boolean, named AiOn in the player script. AiOn is then used to set the AI script to enabled/disabled, see the excerpt from the PlayerController below:

// The Ai Script
 AIPlayerScript myAI;

    void Start()
    {
// fetching the script
        myAI = gameObject.GetComponent<AIPlayerScript>();
    }

    void Update()
    {
// turn AI on/off
        myAI.enabled = AiOn;
}

The rest of thinking was done inside the AI script itself, going back through the code and adding lines to control a virtual player would have been extremely slow going and a waste of time.  

 

In the placement stage the AI works methodically along the board to check for a move. The AI will spawn a monster at random if the slot is empty and move to the next. This is broken down into three stages, Checking for the right conditions (Game State == Place), spawn check, and send spawn info to Game Controller.  

  IEnumerator PlaceMonsters()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (remainingArchers > 0)
            {
                CheckandPlaceEnemy(slots[i]);

                yield return null;
            }
        }

I chose the co-routine and  for loop over a simple for-each loop for the aesthetic result. While it still  completes all its actions in a few seconds the staggered work creates a more human like interaction.  Spawning the monster is done by using a SendMessage function, much like the buttons would; broadcasting the function name and spawnInfo struct as a parameter.

....SendMessage("SpawnMonster",RandomSpawnInfo(spawnslot.gameObject));

In this case the SpawnInfo is a method returning a type:

  SpawnInfo RandomSpawnInfo(GameObject ChosenSlot)
    {
        int rnd = Random.Range(0, 2);
        SpawnInfo RandomMonster;
        RandomMonster.EnemyToSpawn = Monsters[rnd];
        RandomMonster.Slot = ChosenSlot;
        return RandomMonster;
    }

The next button is controlled by a method that switches the ready to continue value for the player to true whenever their next button is usable.

State Diven ai error

The A.i is still a little bumpy, and needs debugging. If the game is set to battle between two A.I. players errors emerge after the first round of combat. At times if there are empty slots the game will still advance to attack again, when by logic it should return to the placement scene. I do believe this problem is caused by an incomplete coroutine and a method being called earlier then it should, I will look at this and mention it in the Post Mortem.

 

So far, this project has been highly successful for me in terms of education and confidence in scripting.  I found myself experimenting off knowledge with success more often, in the past it has been more of a stumbling in the dark experience.  By this time next week, I hope to have sound implemented and the game polished to a submission level.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s