Quote: "
Actually, this is a property of all binary data types on the computer. Since the timer returns a 32-bit integer extending it to 64-bits will break it, but truncating it to a 16-bit or 8-bit value will still give the correct roll-over behaviour, although the maximum delay times you can use will be smaller."
Yes it is. I was specifically talking about the code you posted. Of course other data types roll over, but just not at that same value. So using that same value with another data type would change the effect. I think we're in agreement.
Quote: "You only need to store the time the event is to be fired. However, whenever you would compare two times to see which comes first:
if timeA < timeB
...
endif
Instead, you would do this:
currentTime = timer()
if (timeA - currentTime) < (timeB - currentTime)
...
endif
This ensures that the wrap-around position is always as far away as possible from the current time. Normally one of the times you are comparing would already be the current time, in which case it's as simple as this:
if (timeA - currentTime) < 0
...
endif
The important part is that both sides of the '<' operator are time differences rather than absolute times."
Sure. That will work too.
Quote: "There's no need to sort the array - as long as you insert elements at the correct positions the array will always be in the correct order. Finding the correct position is O(log n) or O(n) if done the easy way. Resizing the array is also O(n). Using a linked list instead would provide no performance gain at the cost of memory and code complexity, since although resizing is now amortized constant time, finding the correct position will now be at least O(n) so the overall complexity of an insertion is still O(n)."
Yeah, and worst case is running O(n) or O(logn) inserts on all n entries, like if they were repeating. The so called 'perfect storm' worst cast scenario. And I should add that they didn't mention any kind of quick searching capability. That sort of evolved in the discussion afterwards.
Quote: "
However you can do better: arranging the events in a binary min-heap inside the array will allow you to get O(log n) time for both insertion and removal of events, and constant time checking for events each frame. Plus, a binary heap is very easy to implement using an array."
Well there you go you can squeeze more performance out of there. Still I wasn't looking to to build the absolute perfect system, seems I've been sucked into this none the less.
This approach to Sorting Events, is a lot better than the first one that was proposed. The fact that you can Binary sort and search in O(logn) time means it's fast. It's a bit more in depth then I was thinking of going with this, with mindset of a simple approach. Relatively speaking, it's the most complicated of the suggestions, but I think it's the most rewarding.
Worst case... I'm starting to wrack my brain here... still has you running all the events... every frame... so that's n, insertion is O(logn)... insertion on every event looping to the end... O(nlogn) I'm thinking.
It's a bit rough working with worst case scenarios, I think that as a matter of practicality your method would work really well. In some special case situations it would be worse.
Now I don't think my suggestion is the absolute mathematical best solution that exists. I thought of it more as a lazy man approach, simple but effective. But hell, I'm in this deep might as well provoke you further, in all good fun. O(n) is still better than O(nlogn).
On a side note:
I suggested a few other things like making things periodic instead of 'per frame'. I think that still stands. If you were simply running everything every frame before, then adding the best event system isn't going to help if you're still running all those events per frame. That would be the same workload plus overhead.
Also the suggestion of capping the event processing time per frame, to something like 1 or 2ms and picking up where you left off on the next frame. I think that could also be applied to any suggestion here. This suggestion in most cases ends the debate. Realistically speaking for any typical game. You pretty much cap any slow down, even if you are reasonably inefficient. Don't be like that be efficient too, at least a little.