Get Physics2DDirectBodyState from children's Rigidbody2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mateusak
:warning: Old Version Published before Godot 3 was released.

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?

:bust_in_silhouette: Reply From: eska

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
	pass

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.

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

mateusak | 2016-06-26 17:51

Messed up the connect code, fixed now

eska | 2016-06-26 18:23