I want to save performance in a scene which contains a lot of objects with constantly working logic bricks. How to turn it off while the player is not nearby?
You can do it with logic bricks:
Explanation: This logic setting will suspend physics (Edit Object brick/Dynamics/Suspend Dynamics) at the beginning of the game (Delay = 0) and restore it (Edit Object brick/Dynamics/Restore Dynamics) when an object with a player property is in a 5 blender units distance.
Lets talk a bit about when logic bricks run
Sensors will always run when they have a connection to an enabled controller. A controller is enabled when it belongs to an active state of the game object.
This means you can dynamically disable sensors by switching the state.
Warning: Disabled sensors do not sense anything. They will start sensing within the next frame after enabling them.
Controllers are executed when they get at least one trigger from any connected sensor. Therefore the most inefficient design is to use an Always sensor with
[True Level Triggering] enabled. Therefore avoid that unless it is absolutely necessary.
Actuators run when they are active. They react on activation and deactivation signals from connected controllers. Activation signals override deactivation signals (OR behavior).
Actuators differ in how long they run.
Single Frame Run
These actuators run exactly one frame and deactivate themselves. They will ignore deactivation signals as they have no effect anyway.
Examples: * Property Actuator, Action Actuator in Property Mode, Random Actuator, Scene Actuator
These actuators perform there operation until it completes. Examples: * Action Actuator Play mode
These Actuator run unto they get a deactivation signal Examples: * Action Actuator Loop Stop Mode * Motion Actuator
Sometimes they continue but stop at a later time. Examples: * Action Actuator Loop End Mode (stops when the cycle completed) * Action Actuator Flipper mode (stops after finishing the reverse playback)
- You can measure if an actuator is running or not with the Actuator sensor (be aware at startup no actuator runs).
It is up to you to choose an efficient design. Anything that does not run ... eats less time.
Turning off logic that is not near the player
First of all you need an efficient way to identify such objects. Then you can
- Remove the game object
- Switch states to disable sensors
but you need the other way around too. When the (supposed) object comes near:
- add the game object
- Switch states to enable sensors
I hope it helps a bit.
you could add a 'Near' sensor to the player, it can set a variable or trigger a message actuator. Or add it to the items to check for the player.
You could also use it with an 'And' controller mixed with whatever you primary sensor is. Then both would have to be true to actuate.