Is there a way to add something to a list, and insure it remains at the last position on that list even if I add more data? in this case I am talking about Vector3 List.
Thanks In advance
Is there a way to add something to a list, and insure it remains at the last position on that list even if I add more data? in this case I am talking about Vector3 List.
Thanks In advance
Sure, write some method that inserts new elements by removing and remembering the last element, then adding the new one, then re-adding the remembered last element. That way you can add elements but always keep the old element as the last one.
Depending on what exactly you need, you could also use a different data structure. A Queue for examle, which means you add items in FIFO order, meaning first in, first out. A Stack would be last in, first out. Or you could write your own data structure to do exactly what you want. If you want a structure where you can add elements with booleans, deciding whether or not they should stay at the end, then you can write just that. You’d need to decide what happens when more than one element is set to stay at the end of the list tho.
Anyways, you have complete freedom for what you want to do, if you can define what that is^^
Sure, just add to the list and then everytime you add something, take the last element, save a reference to it, remove it from the list, add the new thing, then add the saved item back in.
Something like:
Vector3 savedItem = Vector3.zero;
List<Vector3> theList = new List<Vector3>();
void AddNewItem(Vector3 newItem)
{
if(theList.Count > 0)
{
savedItem = theList [theList .Count - 1];
theList.Remove(savedItem);
theList.Add(newItem);
theList.Add(savedItem);
}
else
{
theList.Add(newItem);
}
}
Or something similar. Note that was written freehand so probably wont compile, its just to illustrate a point.
You could also move the items instead of removing and adding again. Theres a number of ways you could do this tbh.
Damn you beat me by 2 mins XD
@Yoreki , @MadeFromPolygons_1 , Thank you so much for your replies.
Basically this Script is on all my Units in an RPG game, and the part I am stuck on is making a “Patrol” function. Atm, if the Units are Idle; I can make them Patrol from Current Position (A) to RayHit Position (B). and this makes them go back and forth between these two points. However, the problem is when I add More RayHit Positions to the list (lets say positions; C D E F). Instead of my Units going from A to F, what will happen is, they will Patrol from B to A to C to D, E, F, and this cycle continues. And this Looks really weird. So I want to make sure that Position (A), is always at the end of the List.
Now I think I know how to go around doing it your way, I am just afraid that if over 200 units Have this script on them, and they all go through the process you mentioned, it may start to add some lags. Not to Mention that this will happen with every additional patrol Point that I will be adding. But I am not sure.
Nah adding and removing from lists is super quick. Youll be fine
Golden rule is worry about performance when its an issue, dont pre-emptively worry as that creates more issues. Its only an issue if your noticing performance drop ![]()
Thanks bro, ama give it a shot!
Ofcourse if you do have performance issues with this, either post back here or make a new post regarding the perf issues and we will try and help.
Good luck!
Ok, So I made it work, thanks, but! I droped from 70+ Fps to 35 with 120 units running around with this script.
Nvm, it was a stupid Debug.Log on voidUpdate LOL, it was like on 100,000 count
Inserting and removing in the middle of a long list is actually pretty expensive, because it requires shifting all elements after the insertion/removal point into new positions.
Adding and removing near the end of a list is cheap, but you should really use RemoveAt(index) instead of Remove(item). Remove(item) forces it to search the entire list looking for the thing you want. RemoveAt(list.Count - 1) goes directly to the last item and is extremely fast.
Of course, if the list is really short, then it doesn’t make too much difference.
Right but what I was talking about is that they are not going to really notice that performance impact. Its really unlikely they have a list long enough to make a difference, our current application being used by clients on building sites removes and adds entries to lists that are in the 1000s of length range and we do it on a HoloLens without any hiccups, runs 60fps constant. So yes your correct, but seeing as this is a new user I didnt want to make them run down a rabbit hole for performance issues that arent likely to impact them in a tangible way.
Love you bro, keep your advice coming, I will Make short video soon to show u my progress thx to this forum ![]()