**This is an old revision of the document!**

This page is a draft for good rule to apply for optimize a game.
This is a draft in progress @Bouh

The optimization of a game is important, a good optimization will allow a game to work faster, and it will be more fluid.

GDevelop 5 gives you total freedom in events, which can be awesome but also cause very costly for the processor where the game is running.

It is important to know that are events and there are each called once per second.

For example for a game at 60fps if you have this action then in this action will be executed 60 times in a second.

Optimization in the engine

The GDevelop engine do once simple thing, he said to the graphic api do not render the object not in viewport. This doesn't affect the creator because it's handle in background.

But this is an interesting thing. Hide or disable what you don't need. This is the key of the optimization.

Trigger once

Tigger once is a condition, this allow to run the actions ony once, for each time the conditions have been met.

Let's take an example where I want to create an object every time I click. I'll show you the wrong way and the right way to do it.

What i want : When i press the mouse left button i create an object called “MyObject”. What i get : When i press the mouse left button i create multiple objects called “MyObject”.

This is bad because when you press the button your finger is on button during a short time maybe like 0.3seconds. During this time the event can be called multiple times then the object is created more what once.

For fix that we need add the condition Trigger once in the condition.

Behaviors

There is multiple behaviors, Pathfinding and Physic Engine 2.0 are the biggest consumers of power.

Delete the object unused

Let's take the example where I have a gun and I create one bullet when i do fire. This bullet have a force toward a direction. If you shot very often bullets will increase in number on scene and each have a physic. If you didn't manage the bullets you will get some lag during the game. The good solution is to delete the bullets if there is off screen.

An behavior exist “Destroy when outside of the screen” attach it on the bullet object. All bullets moving offscreen will be deleted.

Disable the object unused

Behaviors can be disable. This is perfect when you didn't need to affect the instance out of the screen.

Let's take the example where I want 100 enemies to move to a position each 2 seconds close to their taking into account the obstacles (For simulate guardian rounds with random paths).

Bad way is to move the enemies like this : If you do only this all objects will move, but maybe you don't need the objects are not in viewport.

Why calculate something what we didn't see?

Instead you can move only the object visible (in viewport), there is a simple way:

  • Disable the behavior Pathfinding of the objects not in viewport once time.
  • And do the invert event, when the objects are in viewport, enable the behavior once time.