C
C#7mo ago
n

Weird error to do with index out of range

hello, I'm getting this error:
[2024-05-27 15:36:55.317 +01:00] [STDOUT] ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
[2024-05-27 15:36:55.317 +01:00] [STDOUT] Parameter name: index
[2024-05-27 15:36:55.317 +01:00] [STDOUT] at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <75633565436c42f0a6426b33f0132ade>:0
[2024-05-27 15:36:55.317 +01:00] [STDOUT] at AutoEvents.Controllers.EventVoteController..ctor () [0x00071] in <fa39906863294009a46013076a8f031d>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] at AutoEvents.Controllers.CooldownController.StartEventVoting () [0x00021] in <fa39906863294009a46013076a8f031d>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] at AutoEvents.Controllers.CooldownController+<AutoEvent>d__12.MoveNext () [0x00026] in <fa39906863294009a46013076a8f031d>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] at MEC.Timing.RunCoroutineInternal (System.Collections.Generic.IEnumerator`1[T] coroutine, MEC.Segment segment, System.Int32 layer, System.Boolean layerHasValue, System.String tag, MEC.CoroutineHandle handle, System.Boolean prewarm) [0x001ce] in <e63b8eeff6444254a9bdd1e0f405d7a6>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.DebugLogHandler:LogException(Exception, Object)
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.Logger:LogException(Exception, Object)
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.Debug:LogException(Exception)
[2024-05-27 15:36:55.319 +01:00] [STDOUT] MEC.Timing:RunCoroutineInternal(IEnumerator`1, Segment, Int32, Boolean, String, CoroutineHandle, Boolean)
[2024-05-27 15:36:55.319 +01:00] [STDOUT] MEC.Timing:RunCoroutine(IEnumerator`1, String)
[2024-05-27 15:36:55.319 +01:00] [STDOUT] AutoEvents.Controllers.CooldownController:OnWaitingForPlayers()
[2024-05-27 15:36:55.319 +01:00] [STDOUT] Exiled.Events.Features.Event:InvokeNormal()
[2024-05-27 15:36:55.319 +01:00] [STDOUT] Exiled.Events.Features.Event:InvokeSafely()
[2024-05-27 15:36:55.319 +01:00] [STDOUT] Exiled.Events.Handlers.Server:OnWaitingForPlayers()
[2024-05-27 15:36:55.320 +01:00] [STDOUT] MEC.<_DelayedCall>d__310:MoveNext()
[2024-05-27 15:36:55.320 +01:00] [STDOUT] MEC.Timing:Update()
[2024-05-27 15:36:55.317 +01:00] [STDOUT] ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
[2024-05-27 15:36:55.317 +01:00] [STDOUT] Parameter name: index
[2024-05-27 15:36:55.317 +01:00] [STDOUT] at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <75633565436c42f0a6426b33f0132ade>:0
[2024-05-27 15:36:55.317 +01:00] [STDOUT] at AutoEvents.Controllers.EventVoteController..ctor () [0x00071] in <fa39906863294009a46013076a8f031d>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] at AutoEvents.Controllers.CooldownController.StartEventVoting () [0x00021] in <fa39906863294009a46013076a8f031d>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] at AutoEvents.Controllers.CooldownController+<AutoEvent>d__12.MoveNext () [0x00026] in <fa39906863294009a46013076a8f031d>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] at MEC.Timing.RunCoroutineInternal (System.Collections.Generic.IEnumerator`1[T] coroutine, MEC.Segment segment, System.Int32 layer, System.Boolean layerHasValue, System.String tag, MEC.CoroutineHandle handle, System.Boolean prewarm) [0x001ce] in <e63b8eeff6444254a9bdd1e0f405d7a6>:0
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.DebugLogHandler:LogException(Exception, Object)
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.Logger:LogException(Exception, Object)
[2024-05-27 15:36:55.318 +01:00] [STDOUT] UnityEngine.Debug:LogException(Exception)
[2024-05-27 15:36:55.319 +01:00] [STDOUT] MEC.Timing:RunCoroutineInternal(IEnumerator`1, Segment, Int32, Boolean, String, CoroutineHandle, Boolean)
[2024-05-27 15:36:55.319 +01:00] [STDOUT] MEC.Timing:RunCoroutine(IEnumerator`1, String)
[2024-05-27 15:36:55.319 +01:00] [STDOUT] AutoEvents.Controllers.CooldownController:OnWaitingForPlayers()
[2024-05-27 15:36:55.319 +01:00] [STDOUT] Exiled.Events.Features.Event:InvokeNormal()
[2024-05-27 15:36:55.319 +01:00] [STDOUT] Exiled.Events.Features.Event:InvokeSafely()
[2024-05-27 15:36:55.319 +01:00] [STDOUT] Exiled.Events.Handlers.Server:OnWaitingForPlayers()
[2024-05-27 15:36:55.320 +01:00] [STDOUT] MEC.<_DelayedCall>d__310:MoveNext()
[2024-05-27 15:36:55.320 +01:00] [STDOUT] MEC.Timing:Update()
In this area of code:
c#
public EventVoteController()
{
AutoEvents.isEventVoteRunning = true;

_killLoops = false;

_possibleEvents = new List<Event>(Event.Events);
_votingEvents = new List<VoteEvent>();
_cancelVotes = 0;

// initialise 3 random events
for (int i = 0; i < amountOfVotingEvents; i++)
{
Event eventPicked = _possibleEvents[Rand.Next(Event.Events.Count)];
_votingEvents.Add(new VoteEvent { Event = eventPicked, Votes = 0 });
_possibleEvents.Remove(eventPicked);
}

Map.ClearBroadcasts();
Round.IsLobbyLocked = true;
_coroutines.Add(Timing.RunCoroutine(ShowEventName(), "Show Event Name"));
_coroutines.Add(Timing.RunCoroutine(WaitToCheckVotes(), "Wait Check Votes"));
}
c#
public EventVoteController()
{
AutoEvents.isEventVoteRunning = true;

_killLoops = false;

_possibleEvents = new List<Event>(Event.Events);
_votingEvents = new List<VoteEvent>();
_cancelVotes = 0;

// initialise 3 random events
for (int i = 0; i < amountOfVotingEvents; i++)
{
Event eventPicked = _possibleEvents[Rand.Next(Event.Events.Count)];
_votingEvents.Add(new VoteEvent { Event = eventPicked, Votes = 0 });
_possibleEvents.Remove(eventPicked);
}

Map.ClearBroadcasts();
Round.IsLobbyLocked = true;
_coroutines.Add(Timing.RunCoroutine(ShowEventName(), "Show Event Name"));
_coroutines.Add(Timing.RunCoroutine(WaitToCheckVotes(), "Wait Check Votes"));
}
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
Saber
Saber7mo ago
have you considered debugging and verifying _possibleEvents contains the number of elements you think it does
n
nOP7mo ago
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 start
Saber
Saber7mo ago
Does your Rand.Next generate a random number that is out of bounds, ie Event.Events.Count not Event.Events.Count - 1
n
nOP7mo ago
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?
Saber
Saber7mo ago
well you should verify that
n
nOP7mo ago
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
Want results from more Discord servers?
Add your server