Let’s say that I’d like to store all the bullets, that anyone is shooting in my game to calculate new position every frame etc.
If there are 10 players and everyone’s shot rate is 10 shots per second we would possibly need to track 1000 objects after only 10 seconds.
We do know, that iteration over array is very efficient.
should I add new bullets like this?
// "bullets" is an array
bullets.push({
x_position: 5, // x position from which bullet was shot
y_position: 10, // same as above but for y
x_speed: 2, // count of pixels that bullet is travelling on x axis per frame
y_speed: 10 // as above but for y
});
should I remove bullets, that hit the bound, or another player like this?
delete bullets[i] // i -> currently processed bullet index
Cause if i try to take out the element from bullets array, it’s not very efficient with long arrays.
Honestly i haven’t had any better idea to solve bullets problem. Iteration through this kind of array can be painful after couple of minutes because if we delete old bullets, the array length just stays the same and we end up with iterating over milions of records, from which 99% are just empty.
I believe that you want to implement a linked list instead of using a JavaScript array.
The truth about JS arrays
First, you may have a misconception about arrays. When we think of JavaScript arrays, we’re really talking about hashmaps where the keys just happen to be integers. That’s why arrays can have non-numeric indices:
Iteration is fast for arrays (in the C/C++ sense, at least), but iteration through a hashmap is rather poor.
In your case, some browsers might implement your array a real array if certain constraints are met. But I’m fairly certain you don’t want a real array either.
Array performance
Even a real array isn’t particularly amenable to what you want to do (as you pointed out, your array just keeps filling up with
undefinedelements, even as you delete bullets!)And imagine if you did want to delete bullets from a real array and remove the
undefinedelements: the most efficient algorithm I can think of involves creating a new array after a full sweep of bullets, copying all the bullets which haven’t been deleted into this new array. This is fine, but we can do better.Linked Lists in JavaScript!
Based on your problem, I think you want the following:
A simple data structure which provides constant time creation, iteration, and deletion is a linked list. (That said, linked lists don’t allow you to get random bullets quickly. If that is important to you, use a tree instead!)
So how do you implement a linked list? My favorite way is to give each object a
nextreference, so that each bullet points or “links” to the next bullet in the list.Initializing
Here’s how you might start the linked list off:
Appending
To add a bullet to the end of the list, you’ll want to set the
nextreference of the oldlast_bullet, then movelast_bullet:Iterating
To iterate through the linked list:
Deleting
Now for deletion. Here,
brepresents the bullet being deleted, andoldrepresents the bullet just before it in the linked list — soold.next_bulletis equivalent tob.Notice that I didn’t delete the bullet using
delete b. That’s becausedeletedoesn’t do what you think it does.