How to create entities that may, or may not, follow a path

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

In my game there are a variety of NPCs that all derive from a custom Entity scene. Most just move via KinematicBody2D movement, however some will move into position and then enter into a PathFollow2D movement. I’m having trouble creating this scene’s nodes though.

Current setup:

Node2D
- KinematicBody2D
--Sprite, CollisionPolygon, etc

Desired Setup:

Node2D
-Path2D
--PathFollow2D
--- KinematicBody2D
----Sprite, CollisionPolygon, etc

However, the nodes that do not follow a path will throw errors because No points in Curve2D, and the ones that do follow a path cannot move the KinematicBody to a child position in the scene tree as it will break parental inheritance.

The only idea I have to solve this is to make the Path and PathFollow nodes Node2D placeholders in Entity, and change the type to Paths in the desired children. This doesn’t seem to break inheritance or cause wonky issues. But it’s pretty hacky/fragile/wasteful, is there a better way? Edit: Changing type doesn’t work, it just resets to node2D after closing the scene. No idea how to get this to work now.

:bust_in_silhouette: Reply From: aozasori

Though this is a bit different from your setup, here is one thing you could do…
1: Use Path2D’s get_curve to get the Curve2D.
2: Use Curve2D’s get_point_pos to get position to move to.
3: Use Tween to move the entity via interpolate_method with the KinematicBody2D’s move.

I would suggest as setup:

Path2D
-KinematicBody2D
--Tween
--Sprite, CollisionPolygon, etc

Or for minimalism:

KinematicBody2D
-Tween
-Sprite, CollisionPolygon, etc

With any needed Path2D separate (although you might still want to add any entities that use a path as its child to simplify the code).

But i don’t know what other considerations you might have, so take my suggestion with a grain of salt. :slight_smile:

:bust_in_silhouette: Reply From: eons

You can place RemoteTransform2D nodes on the paths and connect to the nodes when you want them to stick to it.
It basically copies its transform to the attached node.

RemoteTransform nodes were originally made for animations but can be used to separate nodes from paths keeping scene structure clean and other useful things.