Basically every node, which inherits from Node2D could do that. I guess you want to archieve this with the help of the nodes property
For a better overview, you could use Position2D nodes. They are basically just
Node2D nodes, but visually displayed as crosses in the scene, so you could use them as "ancor" and store or manipulate the
global_position in your script.
For example you could create a new simple scene for the red shape, something like this:
Then, you just need to drag the
Position2D nodes in the editors 2D-view on the holes of your sprite. Then you can just access the positions of the
Position2D nodes with something like this:
# This script is attached to 'RedShape'.
# Store references to the position nodes.
onready var posA = $PosA
onready var posD = $PosD
# Or every other function...
# Get the position relative to its parent (RedShape in this case).
var position = posA.position
# Just print the x and y coordinates.
print("A: Vector2(" + str(position.x) + ", " + str(position.y) + ")")
And if you want to spawn other sprites at this position, you could do something like this in the same script:
# Will shown up, in the inspector-tab of the node.
# Just drag for example an PNG file in there..
export(String, FILE, "*.png") var texture_path
# If you youse Godot 3.1 you can do it easier without calling 'load':
export var another_texture : Texture
func add_a_sprite_the_old_way( ):
var my_texture = load(texture_path)
var sprite = Sprite.new( )
# set the position of the sprite
sprite.position = posA.position
func add_a_sprite_the_new_way( ):
var sprite = Sprite.new( )
# also set the position....
If you are using Godot 3.1 and you really know the positions on each of your shapes. Then you could do it more dynamically and instanciate these
Position2D nodes inside your
Sprite via code:
# With this you can initialize an array from the inspector with Vector2 coordinates.
export var positions: = [ ]
for i in range(position.size()):
var position2D = Position2D.new()
position2D.position = positions[i]
See basic GD scripting and Arrays for more information.
The only problem I see is, when you have multiple of these scenes.
How do the other scenes know, how to attach them?
I would probably go for the first thing that comes to mind:
You could do something like naming/labeling them. A to A, B to B, like you mentioned.
Then if you try to connect two shapes, like you shown in your picture. You could iterate over the childs (which are nothing more than
Position2D nodes) of one of the
Sprite and pick the positions you want to connect.
Then set the position of the sprite, not the position2D.
But you will end up, that you put the origin of the
Sprite at the position of the
Position2D. Therefore you need to calculate an offset (the direction/distance from the
Position2Dnode to the center (origin) of the
Sprite), which needs to be added to the sprite's position.