How to save the position of an object and variables in dictionary

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

Hello friends, how can I save several positions of different objects in the same dictionary?
I want to save the position and a variable of a music game note together with the other potions of the other notes but I cannot do it
Or I would also like to do something like a print but the print saturates me and it only prints 20 lines
I would like to be able to do the print thing but save it in a text or something to do

var pos = Vector2()
var color = 1
onready var n = preload("res//:rut_of_note.tscn")

func _ready():  
var note = n.instance()
pos = global_position
color = 1

print("note.position =",pos)
print("note.color =",color)

I don’t know how I can help you with saving the positions of objects to a dictionary. But if you want to see the positions (along with other information) for nodes, try looking on the “Remote” tab in the scene tree while the game is running. The “Remote” tab shows the scene tree of the currently running game. Click on one of the nodes, and the information for the node will appear in the Inspector.

Ertain | 2021-11-14 01:35

:bust_in_silhouette: Reply From: Wakatta

Since you will be dealing with multiple objects then nested dictionaries are best used for your purposes.

var notes = Dictionary()
onready var n = preload("res//:rut_of_note.tscn")

func _ready():  
    var note = Dictionary()
    note.instance = n.instance()
    note.color = 1

    #instance has to be part of the scentree to get its position
    note.pos = note.instance.global_position

    #add note to notes dictionary using its instance id as reference
    notes[str(note.instance.get_instance_id())] = note

You can print the notes dictionary as a whole


Or with line formatting

for _note in notes:

Or the way you had it before

for _note in notes:
    print("note.position =", _note.pos)
    print("note.color =", _note.color)