# Working with lists' elements distances

Hello everyone, can you find an efficent method to select fast the nearest element of a list?

2 Likes

Hey @Alessio_Bonini what do you mean by nearest? As in the closest / smallest number?
Because traditionally in programming a â€śheapâ€ť is used for that. It is a sorting method that will never exceed checking more than half the list, and usually has to check way less than that. Also it sorts when adding item rather than taking out (though it also recalculates when removing an item), so that the lowest value is always at index [0].
In the past I made a visual mockup with coBlocks (with added time delays and visuals), for short lists it does add processing time, but the longer the list the faster it gets in comparison to checking each member individually.

2 Likes

Nice @Luc! I was playing around with a couple of different sorts here a few years ago:

3 Likes

For near i meant the object of a list with the less distance from another object, I found some methods but they canâ€™t work nice with a lot of elements in the list (I need to compare them two at a time).

2 Likes

This is the object i was workin on, i need that the foxes only look at the nearest food.

Also if you want help me to solve some problems that makes the project buggy, I create this project to try some evolution experiments (you can see a lot of them usually in Unity on YouTube). I think the algorithm will be not so difficult.

2 Likes

I think a system where each element has 2 coordinate (x, y) and you subtract the foxesâ€™ coordinate with them (settting this value to a property or a variable), but to find the nearest element to the fox of course you need to comper all of them and it doesnâ€™t seem efficent.

2 Likes

Cool project @Alessio_Bonini, Iâ€™ve watched some of those videos before, so itâ€™s super cool that you are making one yourself, very curious to see how it turns out!

So I havenâ€™t actually changed anything about the sorting or finding the shortest distance, I think this is lower priority for now; To put it like this Speed < Functionality, and implementing a list sort early on can be quite confusing for bugfixing; Also Iâ€™m not sure what the best approach for this would be, since the distances will be continuesly changing for each fox individually, so Iâ€™m not sure there is a faster way actually (I mean there always is, but one that is feasonable in coBlocks).

I made a revamp on the main walking/eating system, now it works quite quick and for many actors at once; To increase the food / fox count just duplicate them, and increase itâ€™s corrosponding variable at the top of the â€śwhen play clickedâ€ť code.
It adds the items to the list based on their name: Fox + â€śintâ€ť looping the int until it reaches the aformentioned variable. (This does mean if there is a gap, like you have fox 1,2 and 4 but no 3, it will spawn a red box saying it cant find fox3).

I havenâ€™t touched the NewBorn, or the life and speed things. I think using a similar system I made for the â€śClosestFoodListâ€ť whereby the index of each fox in the â€śFoxListâ€ť corrosponds with the data on the same index in the â€śClosestFoodListâ€ť. You could even combine those two data points using a vector (like speed on the x axis and life on the y axis); But that might just make it more confusing to read while gaining little preformance; So opting for two seperate lists might be better.

Neither have I added comments into the code yet, if need be I could do to clear things up so do let me know. Other than that feel free to ask any questions; I know my coding method can be a bit unclear at times. Hopefully this helps you along, would love to see more updates on this!!

2 Likes

Thank you very much I am going to see your project, i started this cospaces yesterday so itâ€™s only started and of course there are a lot of things to add and change.

1 Like

Your system works pretty well, but i need to add the reproduction system using the system that i already put for create a property for each fox in base of its name.

1 Like