I apologize for the vague wording of this question, but hopefully you can follow along...
In my current project, I am creating a game engine using Javascript. Within this engine, there is a Scene
object that contains various elements, including an array that holds all the items to be drawn.
To access this array, I use:
scene.internals.draw
The issue arises when this array needs to be manipulated within a specific method. Since the name/path of the array may change in the future, I wanted a solution that would allow me to update it easily without modifying multiple references throughout the code. To achieve this, I implemented the following approach:
var location = scene.internals.draw;
By storing the array path in a variable, I can make changes without impacting the overall functionality of the method. This method works well in most cases, such as adding objects to the array using .push(obj)
. However, there is a scenario where I need to divide the array, insert new elements, and then merge it back together, like so:
buff1 = location.slice(0, i); //First section of the array.
buff2 = location.slice(i, location.length); //Second section of the array.
//Inserting something between these sections.
buff1.push(ob);
location = buff1.concat(buff2); //Encountering issues here!
Initially, this logic functioned correctly when directly manipulating scene.internals.draw
as the array path. However, after assigning a new value to the local location
variable, the desired array was not being updated.
Therefore, my question is: How can I assign values to the actual objects themselves rather than just referencing variables like location
?
Although one workaround involves updating the original array path manually at the end of the method:
scene.internals.draw = location.slice();
This approach requires repeating the array path and editing it twice, which is manageable but not ideal. For future scenarios where similar functionality may be required, I am seeking a more efficient solution.