How should I use every tick (performance)

Get help using Construct 2

Post » Sun Jun 08, 2014 4:22 am

Hi, ppl. I've got a noobie question about perfomance. :)

What is the difference (on performance) of using (1 event or 3 events):

Every tick{
rotate.sprite
scale.sprite
move.sprite}

and:

Every tick{
rotate.sprite
}
Every tick{
scale.sprite
}
Every tick{
move.sprite
}

Creating more events will slow down the app or do they all "catch the same train"?

I've got some trouble on organizing the groups and now and then I think "hmmm, the code needs another every tick", then I remember that I've got one everytick event running up there and so I search for it and add to it). Is that something I really should worry about?

Thanks in advance
B
33
S
5
G
1
Posts: 163
Reputation: 2,698

Post » Sun Jun 08, 2014 4:35 am

I think c2/JS=Single-threaded
Every tick=laggggg
B
17
S
7
Posts: 360
Reputation: 2,202

Post » Sun Jun 08, 2014 5:40 pm

@fongka2 - that's not true (about the lag - c2 is mostly single threaded though). There's an idea that's getting traction that every tick is performance intensive. It's not. At all. What other actions and conditions you do every tick can be performance intensive, but using every tick, by itself, is utterly negligible. Stuff like moving, rotating or scaling sprites are just fine to do each tick.

Technically though, scofano, as I understand it the first is best, but by such a small amount I doubt you could tell the difference even doing it a bunch of times over a project.
Moderator
B
95
S
34
G
33
Posts: 3,007
Reputation: 27,876

Post » Sun Jun 08, 2014 6:31 pm

Arima wrote:Technically though, scofano, as I understand it the first is best, but by such a small amount I doubt you could tell the difference even doing it a bunch of times over a project.


Thanks, Arima.

Everything that I test on browser (pc) runs fine. My problem is when I test it on my Ipad(new). :?
So I've started to read forums and ppl always blame the everytick (among other things, like number of objects) for performance.

What I think I should do (please tell me if I'm wrong):
I'm coding a top down shooter and there are 4 types of guns. So I coded every gun on their own group (inside each group there is an everytick for each gun) and I activate and de-activate the groups depending on the powerup(gun) I get.

I will be using 1 'everytick' call (go rotate, for example) instead of using them all in a single 'everytick' that is doing things on gun2, 3, and 4 (sprites) but only gun1 is on the scene. Waste of time putting them all on a single event if they are not on the scene, right? (or, is it ignored for the sprite not to be seen?)

Thanks
B
33
S
5
G
1
Posts: 163
Reputation: 2,698

Post » Sun Jun 08, 2014 7:07 pm

Groups that are not active will be ignored at runtime. Using a single "every tick" condition to check the active type of gun with 3 elseif conditions wouldn't make a significant difference.

On mobile, it's mainly collision/overlap checks, loops and physics that will quickly eat up your CPU power. Creating and destroying many objects as well, like bullets for example. If you haven't done so by the way, you will need to pool any type of object that's being created and destroyed often (that is to say create them only once, store them in an array for example and reuse them).

Good luck!
B
79
S
22
G
4
Posts: 311
Reputation: 12,814

Post » Sun Jun 08, 2014 7:33 pm

Valerien wrote:Groups that are not active will be ignored at runtime. Using a single "every tick" condition to check the active type of gun with 3 elseif conditions wouldn't make a significant difference.


Like I thought: deactivated group = 0 processing. That's nice to know. :)

Valerien wrote:On mobile, it's mainly collision/overlap checks, loops and physics that will quickly eat up your CPU power. Creating and destroying many objects as well, like bullets for example. If you haven't done so by the way, you will need to pool any type of object that's being created and destroyed often (that is to say create them only once, store them in an array for example and reuse them).

Good luck!


In a case of a shooter, are you saying that it is best for me to create, let's say 30 bullets (20 of main ship and 10 for the enemies) and just move them around (on and off the screen) instead of spawning and destroying them? :shock:
B
33
S
5
G
1
Posts: 163
Reputation: 2,698

Post » Sun Jun 08, 2014 7:46 pm

Yes, or toggle them visible/invisible. At least, if you're getting trouble because of your CPU usage. Creating and freeing objects into memory gives a lot of work to your garbage collector. Anyway, you're already setting the position of your active bullets all of the time in your code, aren't you? So by pooling your objects, you'll technically simply remove all of the object creation and destruction calls during gameplay sessions.

Good luck going forward, and don't hesitate to tell me if you need anything!
B
79
S
22
G
4
Posts: 311
Reputation: 12,814

Post » Sun Jun 08, 2014 7:59 pm

Valerien wrote:Yes, or toggle them visible/invisible. At least, if you're getting trouble because of your CPU usage. Creating and freeing objects into memory gives a lot of work to your garbage collector. Anyway, you're already setting the position of your active bullets all of the time in your code, aren't you? So by pooling your objects, you'll technically simply remove all of the object creation and destruction calls during gameplay sessions.
Good luck going forward, and don't hesitate to tell me if you need anything!


Great and inspiring works of yours, Nathan. Thanks for the response. I'm listening to your tunes, righ now. :mrgreen:

I've doodled what I understood you told me about the bullets. Is that correct?

Image

So I've got to define how many bullets I can have on my screen (by speed) and create them all at once on start of layout? Then, everytime it goes off the screen or get hit I move it back to the ship? :o
B
33
S
5
G
1
Posts: 163
Reputation: 2,698

Post » Sun Jun 08, 2014 8:08 pm

That's exactly it. You can just spawn like 25-30 bullets if you know for sure that you won't need more than that, or use a safe margin. Whenever the player fires a bullet, you then just have to go fetch any inactive bullet and fire it.

You don't need to take in account the precise number of bullets that your game need at a given time in the overall game progression: if this number grows as the player plays, the target device will still have to handle this object count. In other words, you can set a max number by hand.
B
79
S
22
G
4
Posts: 311
Reputation: 12,814

Post » Sun Jun 08, 2014 8:11 pm

@Valerian - Actually, C2 already automatically recycles instances so you don't have to to do any of that manually or worry about garbage collection. From https://www.scirra.com/blog/52/construc ... javascript :

Further, Javascript is a garbage-collected language. That means creating objects allocates memory, but the programmer doesn't say when it's released. Instead, the browser occasionally decides to search for the bits of memory which are no longer being used, and free them. That's called 'garbage collection'. Unfortunately garbage collection can take a lot of CPU time, and even cause your game to become noticeably jumpy, or even regularly freeze up momentarily! That's very annoying for action packed games. There are ways to work around this, but it can be difficult to factor this in to your own code. But don't worry! We've already optimised Construct 2's engine to create very little garbage. Even destroying and later creating a whole object doesn't leave the old object as garbage - it's recycled. So your games should very rarely have garbage collection pauses.
Moderator
B
95
S
34
G
33
Posts: 3,007
Reputation: 27,876

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests