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
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
Immidiate index out of bounds exception
no those will all be valid indicies within
list
Tried that last night
i'm not sure how you're making that happen without seeing code
unless the list is entirely empty
Unless the list is empty,
.Count - 1
will not be out of boundsthere are 2 commented sections here, what am i supposed to be looking at
Theres a story here just give me a secd
There's no list in this code either
So in this code the List is a variable
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
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
i don't know what those functions returning indexes do, so i can't tell you if they're correct or not
If your variable starts at
0
... just don't subtract 1
from it?
And it will be a perfectly valid indexyeah, if you're at the first item there is no previous item
your code needs to handle that special case
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
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
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
throwing an index out of bounds exception in your own logic doesn't sound like the game engine's problem tbh
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
all the elements of a list are already always in memory
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
so you need a bounds check before trying to index into the list
Yes I just dont know how to do that yet hense why am here lol
if (index < 0)
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
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
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
Without full context this foreach loop should work right
i can't currently download attachments, can you use $paste
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!
also, $tias
lol