Hello, I’m trying to make an enum on Segment information script, which would show possible directions to spawn for another segment (Think of game level generation using segments). Something like, if Segment #1, has right side port open, Segment #2 can get attached to first Segment’s 1st port with it’s left port etc. This is possible with bools, but it would just be too meesy, I’d like to try using enums, but just not sure how to use them, tried to look online.
Would be great if you could at least put me on the right track, a way how to choose random possible enum would be awesome too, thanks!
I don’t think you think enums do what i think they do.
enum is just a way of giving labels to values
to answer your topic, you pick a random enum with
(myEnum) Random.Range (0, numberOfMyEnumOptions)
Well, you can give a value to each entry of your enum, after that, it’s just getting the number of entries of your enum, and doing a random.
If you are always making those enums in such ways that it starts with the same number (either always 0, 1, 2, 150, whatever, but always the same), and increment them by 1 each time, you can find what to supply to the random pretty easily.
The “min” should be your starting number, the max is one of either functions that can get you the amount of entries of an enum, which is a .Length on either a enum.getNames or enum.getValues.
//Just your random enum
public enum Stuff{
Nothing = 0,
Something = 1,
Second = 2,
My_Choice = 3
}
//Randomizing the entry to get:
int min =0; //the number that starts the enums
Random rand = new Random(); //use an existing one, or a seed works too
int entries = Enum.GetValues(typeof(Stuff)).Length; //Can use the Enum.getNames too.
//And now, the stuff you actually want:
int randomEntry = rand.next(min, entries);
Stuff randomEntry = (Stuff)randomEntry;
//Note: no need to make the intermediate int, it is just for teaching purpose.
As you can probably tell by my ways of doing it, I am not used to what Unity adds (like the Random.Range), so I prefer to use things I have a good basis with when answerring.