How to combine/mix physics behaviors?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bartosz

I’m trying to recreate in Godot my workflow from unity. I’m used to mixin-like reuse of components e.g my characters have abilities that correspond to simple physical action e.g. move, jump, fly, swim, grab…

Each of that abilities operates on characters own rigid_body and can be combine with any other ability without coding to create some more complex behavior e.g. monster + move + jump + detect_obstacle = monster_that_can_jump_over_pits

I’m new to Godot so I hacked this solution:

|-- rigid_body
|-- move
|-- jump
|-- detect_obstacle

where rigid_body is actual RigidBody2D of monster and move,jump,detect_obstacle are empty RigidBody2D with script attached to them. Inside of scripts I use _integrate_forces.

Is it correct approach to combine/mix behaviors in Godot without coding or is there a better way?

:bust_in_silhouette: Reply From: markopolo

That’s how I do things currently, I don’t know of a “best practices” when it comes to compositing behaviors.

move,jump,detect_obstacle are empty RigidBody2D with script attached to them.

If these things are just behaviors that act on your “rigid_body” node, they don’t need to be RigidBody2D themselves. I tend to have a RigidBody2D or KinematicBody2D as the root node for actors/characters and behaviors as Node or Node2D with attached scripts. The script on the RigidBody2D calls whatever behavior I want from its _integrate_forces. It sort of keeps things a bit more decoupled and avoids empty bodies :slight_smile:

Making behaviors simple nodes make sense. Thanks to your answer I was able to enhance my scripts

Bartosz | 2018-04-05 19:21