Convert List to index to be used in an array

So I am trying to find a way to take a List<T> and get each of its Items as an array index so I pass that to a function that I already have written. (Trying to keep this genaric as to fully understand everything would take dissecting the game Im working on lol.)
33 Replies
Jimmacle
Jimmacle5d ago
what index are you trying to get exactly? the index of each item is just going to be a sequence from 0 to list.Count - 1
The Negative One
The Negative OneOP5d ago
Immidiate index out of bounds exception
Jimmacle
Jimmacle5d ago
no those will all be valid indicies within list
The Negative One
The Negative OneOP5d ago
Tried that last night
Jimmacle
Jimmacle5d ago
i'm not sure how you're making that happen without seeing code unless the list is entirely empty
Angius
Angius5d ago
Unless the list is empty, .Count - 1 will not be out of bounds
The Negative One
The Negative OneOP5d ago
/* while (RealLastSectionIndex < 0)
{

text.AppendLine("Only Chuck Norris Can Divide By Zero\n");
}

if (RealLastSectionIndex >= 0) {

text.AppendFormat("- Section Index: {0}\n", enhancedSectionStatsPrv.SectionIndex);
text.AppendFormat("- Next Section Index: {0}\n", enhancedSectionStatsPrv.nextSectionIndex);
text.AppendFormat("- Total Notes Hit in Section: {0}/{1}\n", enhancedSectionStatsPrv.TotalSectionHitStats.AllNoteCount, enhancedSectionStatsPrv.TotalSectionNotesStats.AllNoteCount);
text.AppendFormat("- Total Notes Missed In Section: {0}/{1}\n", enhancedSectionStatsPrv.TotalSectionMissedStats.AllNoteCount, enhancedSectionStatsPrv.TotalSectionNotesStats.AllNoteCount);


}
*/
/* foreach (var section in engine.EngineStats.SectionStatsTracker.SectionStatsArray)
{
text.AppendLine("\n Current Section Stats:\n");
text.AppendFormat("- Section Name: {0}\n", enhancedSectionStatsCurrent.sectionName);
text.AppendFormat("- Section Index: {0}\n", enhancedSectionStatsCurrent.SectionIndex);
text.AppendFormat("- Next Section Index: {0}\n", enhancedSectionStatsCurrent.nextSectionIndex);
text.AppendFormat("- Total Notes Hit in Section: {0}/{1}\n", enhancedSectionStatsCurrent.TotalSectionHitStats.AllNoteCount, enhancedSectionStatsCurrent.TotalSectionNotesStats.AllNoteCount);
text.AppendFormat("- Total Notes Missed In Section: {0}/{1}\n", enhancedSectionStatsCurrent.TotalSectionMissedStats.AllNoteCount, enhancedSectionStatsCurrent.TotalSectionNotesStats.AllNoteCount);

}
*/
/* while (RealLastSectionIndex < 0)
{

text.AppendLine("Only Chuck Norris Can Divide By Zero\n");
}

if (RealLastSectionIndex >= 0) {

text.AppendFormat("- Section Index: {0}\n", enhancedSectionStatsPrv.SectionIndex);
text.AppendFormat("- Next Section Index: {0}\n", enhancedSectionStatsPrv.nextSectionIndex);
text.AppendFormat("- Total Notes Hit in Section: {0}/{1}\n", enhancedSectionStatsPrv.TotalSectionHitStats.AllNoteCount, enhancedSectionStatsPrv.TotalSectionNotesStats.AllNoteCount);
text.AppendFormat("- Total Notes Missed In Section: {0}/{1}\n", enhancedSectionStatsPrv.TotalSectionMissedStats.AllNoteCount, enhancedSectionStatsPrv.TotalSectionNotesStats.AllNoteCount);


}
*/
/* foreach (var section in engine.EngineStats.SectionStatsTracker.SectionStatsArray)
{
text.AppendLine("\n Current Section Stats:\n");
text.AppendFormat("- Section Name: {0}\n", enhancedSectionStatsCurrent.sectionName);
text.AppendFormat("- Section Index: {0}\n", enhancedSectionStatsCurrent.SectionIndex);
text.AppendFormat("- Next Section Index: {0}\n", enhancedSectionStatsCurrent.nextSectionIndex);
text.AppendFormat("- Total Notes Hit in Section: {0}/{1}\n", enhancedSectionStatsCurrent.TotalSectionHitStats.AllNoteCount, enhancedSectionStatsCurrent.TotalSectionNotesStats.AllNoteCount);
text.AppendFormat("- Total Notes Missed In Section: {0}/{1}\n", enhancedSectionStatsCurrent.TotalSectionMissedStats.AllNoteCount, enhancedSectionStatsCurrent.TotalSectionNotesStats.AllNoteCount);

}
*/
Jimmacle
Jimmacle5d ago
there are 2 commented sections here, what am i supposed to be looking at
The Negative One
The Negative OneOP5d ago
Theres a story here just give me a secd
Angius
Angius5d ago
There's no list in this code either
The Negative One
The Negative OneOP5d ago
So in this code the List is a variable
chart.sections
chart.sections
Also Im only focusing on one part of this massive beast at this point lol So code you see is Immediate JIT Gui Code for the Unity Debugger our group cobbled together so all you see there will be seen on the Gameplay side of things immediately. (Witch is how I know that that answer was invalid as I already got that crash like four times now). The two things im shooting for is... 1. When looking at the actual vars Im trying to bound it on either side
int RealCurrentSectionIndex = Chart.Sections.GetIndexOfPrevious(_songRunner.SongTime);
int RealNextSectionIndex = Chart.Sections.GetIndexOfNext(_songRunner.SongTime);
int RealLastSectionIndex = RealCurrentSectionIndex - 1;
int RealCurrentSectionIndex = Chart.Sections.GetIndexOfPrevious(_songRunner.SongTime);
int RealNextSectionIndex = Chart.Sections.GetIndexOfNext(_songRunner.SongTime);
int RealLastSectionIndex = RealCurrentSectionIndex - 1;
these are the new index variable these work but wil throw IOB when compiled because as my partner pointed out the first index is 0 so 0-1 = -1 (IOB) and its the same for the last index +1 -1 equals list (IOB) so im trying to just get the indexs store thing in a bounded var and use them for this function
public SectionStats[] SectionStatsArray;

public FiveFretSectionTracker(List<Section> sections, InstrumentDifficulty<GuitarNote> difficulty)
{

SectionStatsArray = new SectionStats[sections.Count];

for (int i = 0; i < sections.Count; i++)
{
var section = sections[i];
var sectionStats = new SectionStats();

foreach (var note in difficulty.Notes)
{
if (note.Tick >= section.Tick && note.Tick < section.TickEnd)
{
sectionStats.TotalSectionNotesStats.CountNotesInSong(note);

}
}
//sectionStats.sectionName = section.Name;
//sectionStats.SectionIndex = i++;
SectionStatsArray[i] = sectionStats;

}
}
}
public SectionStats[] SectionStatsArray;

public FiveFretSectionTracker(List<Section> sections, InstrumentDifficulty<GuitarNote> difficulty)
{

SectionStatsArray = new SectionStats[sections.Count];

for (int i = 0; i < sections.Count; i++)
{
var section = sections[i];
var sectionStats = new SectionStats();

foreach (var note in difficulty.Notes)
{
if (note.Tick >= section.Tick && note.Tick < section.TickEnd)
{
sectionStats.TotalSectionNotesStats.CountNotesInSong(note);

}
}
//sectionStats.sectionName = section.Name;
//sectionStats.SectionIndex = i++;
SectionStatsArray[i] = sectionStats;

}
}
}
Jimmacle
Jimmacle5d ago
i don't know what those functions returning indexes do, so i can't tell you if they're correct or not
Angius
Angius5d ago
If your variable starts at 0... just don't subtract 1 from it? And it will be a perfectly valid index
Jimmacle
Jimmacle5d ago
yeah, if you're at the first item there is no previous item your code needs to handle that special case
The Negative One
The Negative OneOP5d ago
Im going to just close this one as I had a brain fart. Noting that because I have all of the code and info its easy for me to refrance this but nigh inposible for me to get thrid pary help on. Thanks for the attmpets though
Jimmacle
Jimmacle5d ago
i mean, tbf your question was pretty all over the place :LUL: i have no idea what Immediate JIT Gui Code for the Unity Debugger is or how that's relevant to debugging regular C# code
The Negative One
The Negative OneOP5d ago
Because I have all of the gaps and I dont know whats needed or not to get to my answer so were at a catch 22 Also all code I have given you is valid and will comple it only dies on how its called thats another cog in this machine its not c# thats having the issue it the game engine
Jimmacle
Jimmacle5d ago
throwing an index out of bounds exception in your own logic doesn't sound like the game engine's problem tbh
The Negative One
The Negative OneOP5d ago
I can run the game just fine with that code up there the only issue comes is when I try to activate the Debug Menu and the debugger kills the program and says Index Out of Bounds so Im just trying to learn to bound a list that is dynamicaly created Also The end goal is to have a set of values stored in the previous index so Im trying to find a way to hold index 0 in memory while having the game work on index 1 etc
Jimmacle
Jimmacle5d ago
all the elements of a list are already always in memory
The Negative One
The Negative OneOP5d ago
So i need to subtract one at one point (pun intended lol) That I know its when the game loads it throws the error because its loading both the current index 0 and the last index 0-1 aka -1 at the same time
Jimmacle
Jimmacle5d ago
so you need a bounds check before trying to index into the list
The Negative One
The Negative OneOP5d ago
Yes I just dont know how to do that yet hense why am here lol
Jimmacle
Jimmacle5d ago
if (index < 0)
The Negative One
The Negative OneOP5d ago
You know I thought about that but thew it out because Would that work under a foreach loop headder if it doese im going to pull my hair out lol
Jimmacle
Jimmacle5d ago
why wouldn't it? if you have a number and you want to check if it's less than 0, that's exactly how you check for it
The Negative One
The Negative OneOP5d ago
I have been working on every solution under the sun since 430 this morning never once did I see this as a valid answer lol god you should never code while asleep lol
The Negative One
The Negative OneOP5d ago
Without full context this foreach loop should work right
Jimmacle
Jimmacle5d ago
i can't currently download attachments, can you use $paste
MODiX
MODiX5d ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Jimmacle
Jimmacle5d ago
also, $tias
The Negative One
The Negative OneOP5d ago
lol

Did you find this page helpful?