+1 vote

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:

monster
|-- 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?

in Engine by (1,020 points)
edited by

1 Answer

+1 vote
Best answer

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 :)

by (629 points)
selected by

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

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.