EDIT: half of my question seemed to disappear, but thankfully, it was still in the clipboard.
When I say entities, I mean instantiations of various classes in my engine (example class: Engine.Animation
, or Engine.Scene
). The language in question is Javascript.
The situation:
The current structure of my engine goes something like this:
// The single engine object that only stores constructor functions,
// aka classes (or at least it used to only store those).
Engine = {};
Engine.Scene = function()
{
...
}
Lets take the Scene
class as an example... It basically stores all the information needed to render the visual part of the game state, which is done by the Renderer
class (renderer.render(scene, camera)
).
The problem:
All worked well, but then I realized that the entities sometimes needed to be updated! The best (and, for now, the only) example of this is the scene.AlphaDown()
method, which makes the scene transparent, "shuts it down", in a way.
I implemented this with the use of setInterval
(the interval would call a function that used to lower the scene's alpha depending on how much time passed. This is the kind of "update method" I am talking about: the one that needs to be called iteratively, usually at 60FPS.) But, I was told that it was a bad practice (here and here). It was suggested to me that I use alternative ways of updating entities (without the use of setInterval
).
The possible solution:
I meant to have an array in Engine
, called toUpdate
, in which different entities would push their update methods. Then, the Engine would have a central update()
method, which would calculate the time since last updated, and call the entities' registered update methods, giving that time as an argument. In the previous example with a scene
, AlphaDown()
would register an update method by pushing it into the Engine.toUpdate
array.
When the task of an update method (ie scene.alpha = 0
) was completed, it would be removed from the toUpdate
array.
The problem with the solution:
This method has it's flaws, though. First, it would require the user to call Engine.update()
on each of his game loop iterations. Then, it would make the global Engine
object have methods, but I just want to keep it a namespace of some sort, a collection of useful classes. I don't want to dictate how a loop should be structured!
The question:
Is my solution an acceptable way of updating entities? Are there some other, more standard ways? Should I strive to have my entities not have update methods that need to be iteratively called, and let the user implement them?