0

Is there any limit on how many functions I can declare in the Phaser game update loop & does the performance decrease if there are a lot of functions in the update loop?

caesar getit
  • 215
  • 6
  • 16
  • it depends on the hardware were you run it. if more functionality must be done per update loop, then more hardware you will need. – lcjury Apr 16 '15 at 15:15
  • pretty much yes. the more code you have, the longer it'll take to run. that's just basic common sense. – Marc B Apr 16 '15 at 15:16

1 Answers1

2

Declaring and Calling Functions

There's a difference between declaring a function

function foo(n) {
    return n + 1;
}

and calling a function:

var bar = foo(3);

If you really mean declare, you can indeed declare functions within update, since JavaScript supports nesting and closures:

function update() {
    function updateSomeThings() {
        ...
    }
    function updateSomeOtherThings() {
        ....
    }
}

This has negligible performance impact, since this snippet doesn't actually call any of these functions. If however later in update you called them:

        updateSomeThings();
        updateSomeOtherThings();

then yes there is a cost.

Note: You don't have to declare functions within update itself to call them! You can call functions declared elsewhere, as long as they're in scope. It's worth looking at a JavaScript guide if this is too confusing.

The Cost of Function Calls

Every function you call takes time to execute. The time it takes depends on how complex the function is (how much work it does), and the it may call other functions which also take time to execute. This may be obvious, but the function's total execution time is the sum total of the execution time of all the code within that function, including the time taken by any functions it calls (and that they call, and so on).

Frame Rate

Phaser by default will aim to run at 60 frames per second, which is pretty standard for games. This means it will try to update and draw your game 60 times every second. Phaser does other things apart from calling your update function each time, not least of which is drawing your game, but it also has other housekeeping to do. Depending on the game, the bulk of your frame time may end up being taken up by either updates or drawing.

You certainly want to take less than 1/60th of a second (approx. 16 milliseconds) to complete your update, and that's assuming the game is incredibly quick for Phaser to draw.

Some things you do in Phaser are slower than others. Some developers have been doing this long enough to estimate what is "too slow" to work, but many 2D games will be just fine without taking too much care over optimization (making things run more efficiently in terms of memory used or time taken).

Good and Bad Ideas

Some bad ideas: if you have 50,000 sprites onscreen (though some machines are very powerful especially when Phaser is set to use WebGL), they will often times take far too long to draw even if you never update them. If you have 10,000 sprites bouncing and colliding with each other, collision detection will often times take far too long to update, even though some machines may be able to draw them just fine.

The best advice is to do everything you have to, but nothing you don't. Try to keep your design as simple as possible when getting started. Add complexity via interesting game mechanics, rather than by computationally expensive logic.

If all else fails, sometimes you can split work across multiple updates, or there may be some things you can do every other update or every n updates (which works best if there's different work you can do on the other updates, so you don't just have some updates slower than others).

Community
  • 1
  • 1
El Zorko
  • 3,349
  • 2
  • 26
  • 34
  • typically i am calling about 12 functions in my update loop, all these functions are physics collision detection related. – caesar getit Apr 23 '15 at 13:55