Hi there! I am currently working on a survival game where at the end of each day a random event occurs based on certain variables. My goal is to have a script that will check for variables, if the variables are true then x-events will be added in, the script will then roll a random number to determine what event will occur, once an event is picked then the event will happen on screen and the player will receive a note, similar to games like Crusader Kings.
I am asking around to see if there an easy function to do this or if my base idea is correct.
Thank you for your time
You can use List(T) which is a type found in System.Collections.Generic, paired with Actions.
List<Action> Events = new List<Action>();
A 1 is like an array, except you can continue to add items to it and it will automatically resize itself.
An Action is a object which represents a void(void) method (e.g. void SomeFunction()
).
You can clear Lists by calling Clear()
like so:
Events.Clear();
And you can add items to them by calling Add(item)
like so:
Events.Add(someEventFunction);
Later, you can randomly select a number ranging from 0 to Count
:
int randomEventIndex = Random.Range(0, Events.Count);
Finally, select and call the event you’ve randomly selection:
Events[randomEventIndex]();
Thank you so much for this answer I appreciate it!