Why do Path2D extending scenes share curve instances?

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

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.

:bust_in_silhouette: Reply From: zdimaria

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

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.

stephandischer | 2018-03-18 10:35