+1 vote

Hello eveyone,

just came across some unexpected behaviour, which is messing with my understanding of how generally scenes suppose to work.
I created a simple scene which basically only contains a Path2D Node.
I noticed that all instances from that scene created from scripts share the same curve instance which is used in the Path2D node.
So here is what I am wondering about in a nutshell:

var MyPath2D = preload("res://MyPath2D.tscn")
var p1 = MyPath2D.instance()
var p2 = MyPath2D.instance()
p1.get_curve().add_point(Vector2(1,1))
print(p1.get_curve().get_point_count()) # returns "1"
print(p2.get_curve().get_point_count()) # returns "1"

Whereas when I am trying the same thing with a Path2D node directly with new() instead of instance() I am getting the results I would expect:

var p3 = Path2D.new()
var p4 = Path2D.new()
p3.get_curve().add_point(Vector2(1,1))
print(p3.get_curve().get_point_count()) # returns "1"
print(p4.get_curve().get_point_count()) # returns "0"

Am I missing some fundamental understanding here or should I consider this to be a bug?
Appriciate your thoughts on the matter. I can think about various workarounds for my case, however just wondering about the general concept here.

in Engine by (16 points)

1 Answer

0 votes
Best answer

It's not a bug. If you look at your MyPath2D.tscn there is a curve parameter pointing to a Curve2D node. This node doesn't change when you instance the scene.

To get the behavior you are expecting you can do something like:

var MyPath2D = preload("res://MyPath2D.tscn")
var p1 = MyPath2D.instance()
var p2 = MyPath2D.instance()
var curve_a = Curve2D.new()
var curve_b = Curve2D.new()
p1.curve = curve_a
p2.curve = curve_b
by (232 points)
selected by

thank you, already figured that. Appears a bit counterintuitive in that scenario to me, since the curve has been created as part of that Path2D in that scene, but is knd of scoped globally, but i guess conecept wise its the right thing to do.

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.