Minigame Monday: Delegating

 Delegating:

  • The concept:

There's a system in C# called delegates, allowing you to define a variable that can be called as a function, which can call a range of methods that are assigned to it. I hadn't used delegates in any Unity projects before, so I figured I'd make a minigame to demonstrate the system. As it happened, I took it far too literally. I figured I'd set the player a series of tasks, which they could either attempt themselves, or delegate to an AI, which would handle the tasks using (you guessed it) a delegate. 

This meant that my minigame for this week wasn't exactly one minigame, but instead six simple minigames. This meant a bit more coding and set up than I expected; in the end I wrote eleven separate scripts for this project over two scenes, and suprisingly little of it involved delegates!

  • The challenge:

Despite my intentions, the biggest challenge here wasn't using delegates, but instead creating six separate minigames to use them on. I decided to go with games that the player could play with one button, and in the end settled on the following games: Press when the random numbers match, mash to inflate, press to keep draining water (really just a cuboid) at the right level, press when the blocks are level, press to make the sum correct and press to defuse the bomb when it's about to blow up. Most of these games weren't too complex to program once I'd worked out what I wanted. A little more complicated than that was creating the menu system to choose which games should be delegated or not. This involved a bit of scene management to make sure the correct values from the menu were transfered to the gameplay scene, but again (after making sure there was no case that duplicated my persistant gameobject).

On to the delegate. Once the flags to show which games should be played by the AI, I added functions to the delegate in the following manner:

delegate void ToDo();
ToDo todo;

void Start()
{
    random = FindObjectOfType<RandomNumberController>();
    inflate = FindObjectOfType<Inflate>();
    delegating = FindObjectOfType<ButtonManager>().delegating;
    if (delegating[0] == true)
    {
        todo += DoRandom;
        random.pointsForWin = random.pointsForWin / 2;
    }
    if (delegating[1] == true)
    {
        todo += DoInflate;
        inflate.pointsForWin = inflate.pointsForWin / 2;
    }
}

private void FixedUpdate()
{
    if (todo != null)
        todo();
}
 
void DoRandom()
{
    random.AI();
}
void DoInflate()
{
    inflate.AI();
}
 

This is a cut down snip of the setup class, but it shows how the delegate works. The functions to control the AI are added to the delegate (called todo) if they are needed. Importantly, the delegate is checked to see if it is empty before it is called. This is important, as calling an undefined delegate will cause an error, and if the player wanted to do everything that situation could occur.

  • The result:


 Although as this game is a minigame it's not the prettiest, I'm happy with how the idea worked out. It's not really possible to beat all of the six tasks yourself, so you do have to prioritize and delegate which is what I was hoping for when designing it. If you want to give it a go yourself, you can play it at https://reddragonmakesgames.itch.io/delegation, and the code is available on my github.

Comments

Popular posts from this blog

Armageddump (Boss Rush Jam 2023)

Introducing: Minigame Monday! (3D Minesweeper)

Minigame Monday: Covert Behaviour