Jump to content

(C#) How to get objects in a List between certain indexes?

RileyTheFox

Hey. I need help with getting a list of objects in a List between one index and another (e.g 0-100).

 

For extra context. I am modding a game to add some new features and need to figure out how to get all "Notes" before a certain position in the game.

 

For example, there are 50 notes before position 100. How do I get all those 50 notes? How would I get the notes between 100 and 200?

I think I'd need to use either a for or foreach loop.

 

There are 2 lists I need to use

 

I have a list called "eventTimes" that stores the positions I need to get.

Then there's a list called "Notes" which has objects with a position attached to them.

 

At some point, there will be a matching position in the events list and the Notes list, but I want to get all the notes between the two positions.

 

I'm sorry if I'm explaining this wrong, it's really difficult to explain. 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

If I understand your question, you’re saying there’s a List data structure with 100 entries. 

Yoi want to access notes 50 through 100 & you know the entries are at indexes 50 through 100. 

 

(First time trying mobile code tags)


int start = 50;

int end= 100;

 

for (int i = start; i <= end; i++)

{

console.log(notes[i]);

}

 

A simple for loop or while loop with start & end conditions are all you need. This was a simple problem but now you know how to handle large amounts of data in the future. 

Link to comment
Share on other sites

Link to post
Share on other sites

So you want to get items within two numbers in a list?

I don't use C#, but it should be something like the following:

ListName.Slice(StartPosition, EndPosition))

You should just be able to reassign that to another list; however, if you want each item independently, you should be able to use the following:

foreach (i in ListName.Slice(StartingPostion, Ending Position))
        {
            Console.WriteLine(i);
        }
        //https://www.dotnetperls.com/array-slice

 

In Python (On the chance somebody references this for info in the future)

ListName[Start#:End#:Step#]

 

Fan Comparisons          F@H          PCPartPicker         Analysis of Market Trends (Coming soon? Never? Who knows!)

Designing a mITX case. Working on aluminum prototypes.

Open for intern / part-time. Good at maths, CAD and airflow stuff. Dabbled with Python.

Please fill out this form! It helps a ton! https://linustechtips.com/main/topic/841400-the-poll-to-end-all-polls-poll/

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, fpo said:

If I understand your question, you’re saying there’s a List data structure with 100 entries. 

Yoi want to access notes 50 through 100 & you know the entries are at indexes 50 through 100. 

 

(First time trying mobile code tags)

 


int start = 50;

int end= 100;

 

for (int i = start; i <= end; i++)

{

console.log(notes[i]);

}

 

 

There aren't 100 entries. The game is a rhythm game and a song can have many different amount of notes. Some even have up to 10k+

 

What I'm trying to do is set the size of the notes (visually) to specific sizes through places in the song.

I have a List full of events which have positions (to tell the game when to change the sizes)

And I have a list of Notes that are in the current song.

 

So let's say the first even to tell the game is at position 100, but there are 50 notes before this position. I need to get those 50 notes before that position (each note also has a position attached to the object). I can't hard code any of these values because this is going to be customisable and released to the public

 

If there are 50 notes before position 100 (pos 100 is in the events list), how do I get those? And let's say there are 25 notes between 100 and 200 (pos 200 is also in the events list). How would I do this?

 

I know I'm explaining this terribly. 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×