+1 vote

I'm having a bit of trouble with getting a sprite to follow (or appear on) a PathFollow2D when I create the PathFollow2D via script.

func _ready():
path2d = Path2D.new()
curve = path2d.curve

#Add curve points to create a planetary orbit
curve.add_point ( Vector2 (0, yrad), Vector2( 0, 0 ), Vector2( xrad, 0 ), -1 )
curve.add_point ( Vector2 (xrad, 0), Vector2( 0, 0 ), Vector2( 0, -yrad ), -1 )
curve.add_point ( Vector2 (0, -yrad), Vector2( 0, 0 ), Vector2( -xrad, -0 ), -1 )
curve.add_point ( Vector2 (-xrad, 0), Vector2( 0, 0 ), Vector2( 0, yrad ), -1 )
curve.add_point ( Vector2 (0, yrad), Vector2( 0, 0 ), Vector2( 0, 0 ), -1 )

#Add a new PathFollow2D as a child of the Path2D
path2d.add_child(PathFollow2D.new())
var followpath = path2d.get_child(0)

#Create a test sprite and set its texture
var testsprite = Sprite.new()
testsprite.set_texture(preload("res://StarlightGalaxy/Materials/Planets/Asteroid1.png"))

#Add testsprite to the FollowPath2D
followpath.add_child(testsprite)
followpath.set_offset(0)

#Draw orbit of planet
func _draw():
draw_polyline(curve.get_baked_points(), Color(1,1,1,.1), 2.0)


func _process(delta):
followpath.set_offset(followpath.get_offset() + 50 * delta)

The above is a simplified version of what I'm trying to do (my actual script generates multiple orbits, then pulls from sprite factories etc) but even the above isn't working for me.

If I print the name of "testsprite" it gives me Sprite 12312 or whatever random number it generated, as opposed to "Null," but there is no sprite visible in-game (as far as I can see). I see the drawn Path2D, but not the child "testsprite."

I also deleted the background in the scene to ensure the sprite isn't simply appearing beneath the background.

If I haven't given enough info, let me know and I can add more. Thank you in advance!

in Engine by (46 points)
edited by

1 Answer

0 votes
Best answer

Well, I feel dumb. Apparently I forgot to parent my Path2D into the scene. Made Path2D a child of my root node and now everything's working fine.

by (46 points)
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.