BI - Devblog[#8]

posted in BI - devblog for project Barbarian Invasion
Published July 12, 2020
Advertisement

Hey guys! ?

Finally, we have finished semester exams at the university and now we devote more time to the project. During the session, we did not do much, but we also did not sit idly by.

So let me share with you our progress

New FX

Let me introduce a new spawning effect for units

Spawning effect

We also made a new effect for a frost mage

Projectile fx

FSM

I recently did refactoring for AI because, as the code base grew, managing it was getting harder and harder. Initially, I wrote a standard state machine, but I used “if” constructions and, you guessed it, it was extremely ineffective cause my code become more and more something like this

void Update()
{
	if(IsInAttackState)
	{
		if(IsEnemyCloseEnough)
		{
			//Attack code
		}
		else
		{
			//Switch to chase state
			
			//Reset a lot of variables
			...
		}
	}
	else if(IsInChaseState)
	{
		if(IsEnemyCloseEnough)
		{
			//Switch to attack state
			
			//Reset a lot of variables
			...
		}
		else
		{
			//Chase code
		}
	}
	else
	{
		if(target == null)
		{
			//Search enemy
		}
		else
		{
			//Patrol code
		}
	}
}

You got the idea of whats going on. Such code become unexpandable and very hard to read not only for others but even for me

So I decide to do a refactoring and went in search of material on this theme.

After some work and fixing a huge number of bugs I've got this

private Dictionary<STATE_TYPE, FState> availableStates;

public FState currentState { get; set; }

private void Update()
    {
        if(!GameManager.Instance.IsGameEnd)
        {
            if (availableStates != null)
            {
                if (currentState == null)
                {
                    currentState = availableStates.Values.First();
                    currentState.Init();
                }

                var nextState = currentState.Execute();

                if (nextState.GetType() != currentState.GetType())
                {
                    SwitchToNewState(nextState);
                }
            }
        }
    }

Code like this is easier to read and understand, all states are separated now and Its easy for me to modify them

Can be useful:

Here you can download “ready” FSM for your project - https://github.com/dubit/unity-fsm​​

In video below you can find how to make step-by-step your own FSM

Thanks for your attention! ?

Previous Entry BI - Devblog[#7]
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

BI - Devblog[#8]

6066 views

BI - Devblog[#7]

3764 views

BI - Devblog[#6]

3359 views

BI - Devblog[#5]

3326 views

BI - Devblog[#4]

7160 views

BI - Devblog[#3]

6469 views

BI - Devblog[#2]

5770 views

BI - Devblog[#1]

3481 views
Advertisement