How to start a PathFollow2D at the previous end position in a loop?

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

I have an animated sprite that follows a pattern defined by a Path2D, using PathFollow2D. The path is not closed and ends in a different place to where is starts. What I am trying to do is get the PathFollow2D to loop, but have each iteration start where the sprite finished.

Currently the loop just makes the sprite jump back to it’s original starting position.

Is there an easy way to achieve this behaviour?

Things i’ve tried, without success:

I have tried setting the parent position.x and position.y when the unit_offset get to > 1. I thought this almost worked, but I don’t seem to be able to get a compatible current position of the sprite. So while I can us this technique as a point to change the starting position on each loop, I can’t get figure out how to get the current position of the sprite in a format I can use.

I also thought I could keep the path separate from the sprite (by not having it as a child node) and then just use the PathFollow2D coordinates to manually update the sprite position. I’ve not had much luck here either as the coordinates don’t seem to be the same as the viewports.

I think what happens with a Path2D (and to a lesser extent PathFollow2D) is that the values for a path are first computed, and put into a special array. So what you may have to do is update this special array (I think it’s a PoolVector2Array).

Ertain | 2019-03-24 23:06

Thanks Ertain. In the end I managed to change the origin of the path using the global viewport coordinates. So thankfully in the end I didn’t have to manipulate the Path2D array.

YorkshireKev | 2019-03-25 19:20

Glad to hear that.

Ertain | 2019-03-25 21:34

:bust_in_silhouette: Reply From: YorkshireKev

For the benefit of future searchers I have managed to convert from coordinates from the path to the viewports using the method: get_global_transform_with_canvas().origin.x

I don’t know if this is the correct or most elegant way to loop through the path without it jumping back to the start position, but here is my working code for reference:

Please note that the indenting gets messed up when pasted into the comment, but I’m sure you’ll figure it out! (all but the first line are indented)

if get_parent().get_unit_offset() > 1:
    get_parent().get_parent().get_parent().position.x = get_global_transform_with_canvas().origin.x
    get_parent().get_parent().get_parent().position.y = get_global_transform_with_canvas().origin.y
    get_parent().set_unit_offset(0)

What I’m doing here is setting the root node (get_parent().get_parent().get_parent().position.nn), which sets the origin of the PathFollow2D, with the current global position of the sprite, before resetting the offset of the path. It’s also worth noting that because we’re resetting the unit_offset back to zero, there is no need to tick the loop checkbox.

Thank you for putting in this answer. Though I would recommend putting code in backticks so that it’s easily readable. Due to these posts using Markdown, underscores are interpreted as italics.

Ertain | 2019-03-25 21:36

Thanks. I’ve added the backticks. Looks better but I couldn’t figure out how to keep the indentation. :-/

YorkshireKev | 2019-03-26 18:58

Use four spaces instead of an indentation/tab.

Ertain | 2019-03-26 23:07