Weird error to do with index out of range
hello, I'm getting this error:
In this area of code:
Does anyone see what might be wrong with what I'm doing with indexes? I'm not sure, I've been staring at this for ages and cannot figure it out
6 Replies
have you considered debugging and verifying _possibleEvents contains the number of elements you think it does
Event.Events
is always populated for the entirety that this is running and I very much doubt that could be causing the issue - my event voting system works fine a couple of times and then randomly will just break with this error, crashing it
It is initialised when the server starts (and the plugin is initialised)
If it works once, the event list is never touched after server startDoes your Rand.Next generate a random number that is out of bounds, ie Event.Events.Count not Event.Events.Count - 1
I was thinking something like that because of how random the error appears, but for example if there are 10 events and they are stored at indexes 0-9 within
Event.Events
, surely Rand.Next(Event.Events.Count)
will generate a number between 0 and 9 inclusive?well you should verify that
Rand.Next(10)
will return a random integer less than 10
so that should be fine
Ohhhh shit wtf i just realised
I need to check the _possibleEvents.Count rather than Events.Count, since the latter is constant and the first one changes after some events are removed from the list
so it was choosing a number that was no longer present after the first pick
thanks for the help nevertheless @Saber