+2 votes

I have a scene with several bowling balls that fall when the game starts. I would like to add a button that enables physics when clicked, so that the balls do not fall until the button is pressed. How can I turn physics on and off from GDScript?

in Engine by (16 points)

3 Answers

+2 votes

You can turn off physics for a node using set_physics_process(false). This means _physics_process() will not be called for this node.

For example, in your Ball node, you can have:

func _ready():
    set_physics_process(false)

You can then set up your button to call set_physics_process(true) for the Ball node.


Alternatively, you could give the Ball a custom Boolean like physics_on that is initially false.

Then you can have:

func _physics_process():
    if physics_on:
        # falling code goes here

You can set up your button such that it changes physics_on to true.

by (1,567 points)
edited by

thanks ! , it works !

+2 votes

If the balls you use are RigidBodies, you can use set_use_custom_integrator(). All rigid bodies have this method to turn on, or turn off, the integration of forces (like gravity) performed by Godot.

When you set set_use_custom_integrator(false), Godot performs integrations of forces to compute the new speed and position of the body (This is the default behavior). When you set set_use_custom_integrator(true), Godot doesn't integrate forces itself. That is, it ignores every forces including gravity. But you can still use the method func _integrate_forces( body_state ) to define any physic calculations yourself.

So in your case, I guess you could program your button so every bowling balls of your scene toogle their custom integrator to true or false. I suppose it won't represent much code if your button sends signals and if your bowling balls have the same script.

You may also want to immobilize your bodies once the custom integrator is used :

func _integrate_forces( body_state ):
    if is_using_custom_integrator() :
        body_state.set_linear_velocity( Vector2(0,0) )
        body_state.set_angular_velocity( 0 )
by (39 points)
edited by

Hi,

ı have the same issue but couldn't do it. Can you be more solid on explaining mouse click! In the code, no mouse is represented

+3 votes

I don't know a better way to do this scene-specifically, but globally you can use

Physics2DServer.set_active(false)

and

PhysicsServer.set_active(false)

This will disable ALL physics, including all Area/Body checks as well.

by (28 points)

This is AMAZING. I disabled this, and my game's FPS jumped way up!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.