- Home /
randomize a function from a array (C#)
i have a A.I script with different functions for the conditions on which the enemy shall walk. I want to make a Invoke Repeating() on a function called RandomFunction(). This function shall do a random.range and select randomly from the array of functions, which are [5].
with invokerepeating repeating every 1 second a new function will be called and it should make the player walk randomly around every 1 second.
How would i go about making an array of functions in c#, and how can i use random.range in this array.
Answer by RabidCabbage · Mar 09, 2015 at 01:18 PM
You can store functions in an array using the Func<>
type, eg:
Define an array of 5 functions (You can't use void as the return type afaik, so bool will have to do)
Func<bool>[] AnimList = new Func<bool> [5];
Create multiple functions that return the desired type (in this case bool)
bool Idle() { Debug.Log("Idle"); return true; }
bool Yawn() { Debug.Log("Yawn"); return true; }
Assign the function's to array indexes
AnimList[0] = Idle;
AnimList[1] = Yawn;
Then you can send the Random.Range int's directly into the array index
AnimList[UnityEngine.Random.Range(0, AnimList.Length)]();
Answer by fafase · Mar 09, 2015 at 01:18 PM
private System.Action myUpdate = ()=>{};
private System.Action [] actions = null;
void Start(){
actions = new System.Action []{MethodA,MethodB, MethodC, MethodD, MethodE };
InvokeRepeating("Randomizer", 1.0f);
}
void Udpate()
{
myUpdate();
}
void Randomizer(){
int rand = Random.Range(0, actions.Length);
myUpdate = actions[rand];
}
void MethodA(){ Debug.Log("A");}
void MethodB(){ Debug.Log("B");}
void MethodC(){ Debug.Log("C");}
void MethodD(){ Debug.Log("D");}
void MethodC(){ Debug.Log("E");}
myUpdate is a delegate, so pretty much a reference to a method. What is done in the declaration is just a fancy way to prevent null reference so a default method is added (you may print something to actually see that something is wrong).
The Update just calls that delegate so indirectly it is calling the method it is referring to. The Randomizer method just gets a random method out of an array of methods.
In case of, Action is just a short way for generic delegate.
i got the same error in this example as when i tried myself to do a array with a certain namespace/type:
Assets/Scripts/Enemy$$anonymous$$ovement1.cs(15,17): error CS0246: The type or namespace name `Action' could not be found. Are you missing a using directive or an assembly reference?
its refering to the line: actions = new Action []{$$anonymous$$ethodA,$$anonymous$$ethodB, $$anonymous$$ethodC, $$anonymous$$ethodD, $$anonymous$$ethodE }; and saying that "Action[]" is invalid.
Action is part of the System namespace, I added the explicit reference in my code or you can add a :
using System;
at the top.
Answer by siaran · Mar 09, 2015 at 01:32 PM
I don't see why you want to put your functions in an array here - it's probably possible to hack something together using delegate variables (not sure, never tried), but I think that's overcomplicating things - instead, why not simply write a switch, something like
void ExecuteFuntionByIndex(int index){
switch(index){
case 0:
FunctionZero();
break;
case 1:
FunctionOne();
break;
case 2:
FunctionTwo();
break;
//..etc. for how many functins you have
default:
//Default to function 0 when you get an invalid index
FunctionZero();
break;
}
}
This works aswell, but then the actions will then follow a specific order. Now im calling all my functions thats specified in the switch statement ins$$anonymous$$d of generating a random function after another.
but again this will probably be my plan B, thanks.
It's implied that you would be randomizing the value of the variable named index, so it works as you deem necessary.
Your answer

Follow this Question
Related Questions
Why is Random.Range not working in array 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Create an array that chooses between four random numbers (C#) 3 Answers