I have to fill a dictionary in a loop with multiple info from the node:

var enemiesOnStage = ""
for _e in get_tree().get_root().get_node("MainScene2D").get_node("Enemy A").get_children ():
	enemiesOnStage = {
		"enemy" : _e.get_name(),
	}
print (enemiesOnStage)

... but of course in the example above I'll get the name of the last saved enemy node, How can I do that please?

i think you need array,not dictionary '''

var enemiesOnStage = []
for _e in get_tree().get_root().get_node("MainScene2D").get_node("Enemy A").get_children ():
	enemiesOnStage.append(_e.get_name())
print (enemiesOnStage)

'''

I have a dictionary because the rest of the code is saving file on harddrive.

I would suggest storing the enemies in an array, and then placing that array under the enemies key in the dictionary:

var enemies_array = []
for e in get_tree().get_root().get_node("MainScene2D").get_node("Enemy A").get_children():
	enemies_array.append(e.get_name())

# Store the array in a dictionary:
save_dictionary["enemies"] = enemies_array

Another way you can do it is by using a for loop, though it will be harder to use than an array:

var enemies_dictionary = {}
var enemies_to_loop_through = get_tree().get_root().get_node("MainScene2D").get_node("Enemy A").get_children()
for i in range(0, enemies_to_loop_through.size()):
	enemies_dictionary["Enemy_" + str(i)] = enemies_to_loop_through[i].get_name()

basically I have to fill this variable with more datas based on how many Enemies nodes remain in the scene

var dataPlayer = {
	"stageLevel" : stage_level,
	"playerName" : "playernamekkkkk",
	"playerX" : player_info.position.x,
	"playerY" : player_info.position.y,
	"playerEnergy" : player_info.player_energy,
	"playerScore" : player_info.score,
	"pcoins" : coins,
	
	+++++++ "enemy" : _e.get_name(),
	+++++++ "enemy" : _e.get_name(),
	...
}

@"K-Storm-Studio Ltd" said: how can I print the result out of the loop please?

Depends on which loop you are referring to. For the first code snippet I wrote, add print(enemies_array) after line 4. For the second code snippet, add print(enemies_dictionary) after line 4 (make sure it is not part of the for loop though!)

I would suggest storing the enemies in an array, and then placing that array under the enemies key in the dictionary: I agree with this approach. if it is in dictionary, you do not know how many enemy,So how do you access the data?

@vmjcv said:

I would suggest storing the enemies in an array, and then placing that array under the enemies key in the dictionary: I agree with this approach. if it is in dictionary, you do not know how many enemy,So how do you access the data?

right question! :) And because I had to save everything into the same dictionary and then to the same file I've found a solution storing all the variable into a single string :

enemiesOnStage += _e.get_name() + "-" + cnvbool + "........." + "/"

Then in loading function I get all the variables back splitting the string into array

2 years later