Efficiently Storing Hunderds of Animations

Let’s say i have a app in which I allow a user to view different animations on a character one at a time from a list.

If i have hundreds of these animation, what would be an efficient way of storing them for quick access and also allowing decent searching speeds.

The first thing that comes to my mind is a fixed array of animations but searching would be O(n) time althought perhaps thats not so bad.

Perhaps it would be better to keep the animations in the project panel and somehow access them at runtime… im just not sure…

The other tips are solid, but i <3 efficiency myself as ultra huge projects require this kind of thinking - where memory actually becomes an issue and is a larger issue than any other most of the time. (I am developed an (virtual) infinite world in CryEngine for example that requires this kind of efficiency) So for fun: C# Dictionary uses hashes, it’s O(1) lookup speed. SortedList (C#) is O(log(n)). However, arrays are also 0(1) look-up and are also memory aligned (well in C++, i dunno about C# but i’d be surprised if they weren’t). If you use an enum and know all the animations ahead of time, set your array with these enums as keys (faster than strings, tho for this application likely negligible difference). I’m not sure how you are envisioning look-up times… It’s only O(n) if you have to look through everything and don’t have some other data associated with it. So say you have a bunch of animation pictures only (maybe as GUI buttons or whatever)… if a user clicks on one, it should have the animation’s enum in the array/dictionary, and therefore O(1) lookup.

If you want/need to get fancy, be sure to access in power of 2s and get ‘pages’. With hundreds of animations, it’s unlikely a user can view them all on screen at once. So say you display 16 at a time, and can scroll say 8 at a time. Start by initially loading say 64 animations (or some number that represents the max animations you are allocating) and you have a scroll function that will move 8 off the screen, and bring in a fresh 8. You won’t worry about loading until later. So you scroll down and now it shows animations 8-24 (before it was 1-16). At a certain point in scrolling, say at 32-48, it will load in the next 8, and offload the first 8 (throw it in a coroutine for smoother loading/unloading)…if you stop and scroll back, it will load the first 8 and offload the 8 you just loaded a moment ago. If the user is scrolling fast and something hasn’t loaded yet, no big deal, display a loading box or something until it is loaded. However, if you do are loading/unloading insertion into your data structure may be an issue and, you won’t want an array, statics you can’t, dynamic ones will have to make entire copies of itself at some points which you don’t want… In this case I would use a Dictionary, be sure to unload before you load.

Another way for super memory saving, is to just load pictures, when a user clicks on one it will load the animation on a model at that time and play… so you just grab it when you need it, small hiccup likely tho, but if space is very very very like running off a $9.99 SD card low (2013 cards and prices just to note), this is what i’d do.

Side note: using coroutines has more overhead (usually) because caching won’t be as efficient (you load in one animation, then do other things that will likely disrupt the cache, then load another animation)… when you load all at once usually similar data ure caching is more efficient (especially when memory aligned, which is not the case with dictionarys but it will likely be on the same page table most of the time). That being said, it’s very very tiny note in comparison to keeping a smooth framerate.

all that being said, this is for fun - i wouldn’t worry about loading in large chunks unless your doing a hundred other things at once.