0 votes

The wiki says that set_linear_velocity shouldn't be used every frame, exception when in _integrate_forces. But i'm doing a component-based architecture and my component needs to set it every frame! What do I do? Is there a way to get the state of the Rigidbody without using _integrate_forces?

in Engine by (1,106 points)

1 Answer

+1 vote
Best answer

Create an integrate_forces signal, then implement _integrate_forces in the RigidBody like this:

extends RigidBody

signal integrate_forces( state )

func _integrate_forces( state ):
    emit_signal( "integrate_forces", state )

Then the component can just connect the signal to its own desired function:

extends YourComponentType

func _ready():
    get_node("path/to/rigidbody").connect( "integrate_forces", self, "_on_rigidbody_integration")

func _on_rigidbody_integration( state ):
    # your integration code here

Make sure not to establish the connection with the CONNECT_DEFERRED flag, or integration will be over by the time the connected function is called.

by (1,086 points)
edited by

Didn't knew about the signals, that eases everything. Much thanks!

Messed up the connect code, fixed now

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.