How to animate a Line2D?

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

I have a Line2D with a texture that tiles depending on the length of the line.

I would like to animate this line using some frames I have saved as individual png files. I don’t know the best way to go about it.

I tried using an AnimationPlayer which loads new textures into the Line2D, but it was grossly inefficient to load a new texture every few milliseconds.

Any ideas about how to animate a Line2D?

I wonder why this was flagged.

SIsilicon | 2018-11-22 11:20

Hmm… me too?

Diet Estus | 2018-11-22 20:57

:bust_in_silhouette: Reply From: paolobb4

You probably want to make a sprite sheet (put all frames on the same image), load it as an AtlasTexture and update its region property with an AnimationPlayer.

I haven’t tryed this but seems like the way to go about it. Let us know how it goes :wink:

Alright, I have created a spritesheet with two frames called cord.png, which has a resolution of 62x12. Each frame is 31x12. I added the following code to my _ready()method in the hopes that it would set the Line2D's texture as the first frame of the spritesheet.

# load atlas texture
atlas_texture = AtlasTexture.new()
atlas_texture.atlas = load("img/cord.png")
atlas_texture.region = Rect2(Vector2(0,0), Vector2(31,12))
get_node("Line2D").texture = atlas_texture

But the Line2D still seems to use the entire spritesheet as its texture, rather than the subregion of it.

Any thoughts about what I’m doing wrong?

Diet Estus | 2018-11-22 21:16

It appears the above method works fine for a Sprite but not for a Line2D. I wonder if Line2Ds are simply unable to use an AtlasTexture as their texture.

Diet Estus | 2018-11-22 21:43

Yes, I did some testing and unfortunately it seems it completely ignores the Region property, picks up the hole image and renders it (in a tiled fashion) once and never again. I presume for optimization purposes. In fact I’ve been tinkering with lines recently and, once created, the image doesn’t update even when you change the line’s points from script.

I am also interested in this functionality (an updating Line2D) for my current project although I wasn’t going to get into it until a couple more weeks, but still, I’m going to look into making a plugin for it. I’ll let you know if I manage to get something working but for the time being it seems like there’s no solution to this problem.

Best of lucks :wink:

paolobb4 | 2018-11-23 12:03