Hi guys!
Right now I’m making a Boss for a 2D spaceship shooter game. The Boss after a certain travlled distance appears. I would like to give the Boss three Move function for example. One is the DefaultFunction when he just follow the player and shoot, and two extra functions where he do something else. My question is how can I switch between the functions after a certain time. For example after 10 seconds he change from DefaultFunction to Function1 then after he finishes Function1 he change back to DefaultFunction. I think the two extra function will be choose by a Random number.
I hope you guys understand what I would like to do!
Thank you for your help!
It sounds like you could use Actions here. An Action is basically a piece of code that is set to run in the future at some point.
An example of this could be creating a List (or some collection) of Actions i.e.
private List<Action> m_Actions;
private Random m_Random;
private void Start()
{
m_Random = new Random();
m_Actions = new List<Action>
{
() => AttackMethod1(),
() => AttackMethod2(),
() => AttackMethod3()
}
}
public Action ChooseRandomAttack()
{
var random = m_Random.Next(m_Actions.Count);
return m_Actions[random];
}
private void AttackMethod1()
{
}
private void AttackMethod2()
{
}
private void AttackMethod3()
{
}
So basically, in this example, you would have three attack methods that are added to a list of actions. Then you have a public method to choose a random attack (which could be called from another class).
Obviously, change this to suit your needs. You may not want to choose the attacks at random, instead, you might change them to be chosen specifically at certain times. Maybe you don’t want to choose the attack from elsewhere, and change the method to be private, etc. It’s more just to demonstrate how you could use them to choose different attacks.
P.S. You can also use parameters for actions if you wanted to pass in some damage or time etc, do this the same way you would with a method normally.