Saving and Loading Enemy Data

Godot Version

4.2

Question

in the ready funtion in my world scene, it is supposed to load_data. in load data, it checks if there are any enemies, if there aren’t then it loads 3 slimes as default. if not, it will load the data from the JSON File and generate the slimes based on the data. My JSON Save file is empty, and there are no enemies in the starting world scene, so it should generate 3 of them randomly. but it doesn’t. Can someone see why?

extends Node2D
@onready var player = $"Y-Sort/Player"
@onready var mob_node = $"Y-Sort/Mobs"
const slime_scene = preload("res://Mobs/slime.tscn")
var enemy_data_array : Array[EnemyData] = []
# Called when the node enters the scene tree for the first time.
func _ready():
	player.global_position = Global.player_pos
	load_saved_data()
func assign_unique_names_to_enemies():
	var enemies = get_tree().get_nodes_in_group("Enemies")
	var counter = 1
	for slime in enemies:
		slime.name = "enemy" + str(counter)
		counter += 1
		
func load_saved_data():
	var existing_slimes = get_tree().get_nodes_in_group("Enemies")
	if existing_slimes == null:
		create_default_slimes(3)
		assign_unique_names_to_enemies()
		return
	for slime in existing_slimes:
		slime.queue_free()
		
	var save_path = "user://saves/save.json"  # Adjust this based on your save file path
	if FileAccess.file_exists(save_path):
		var file = FileAccess.open(save_path, FileAccess.READ)
		if file != null:
			var content = file.get_as_text()
			file.close()

			var data = JSON.parse_string(content)
			if data != null and "enemies" in data:
				var enemy_data_array_serialized = data.enemies
				for enemy_data_dict in enemy_data_array_serialized:
					var enemy_data = EnemyData.new()
					enemy_data.health = enemy_data_dict.health
					enemy_data.global_position = Vector2(enemy_data_dict.global_position.x, enemy_data_dict.global_position.y)
					enemy_data.custom_id = enemy_data_dict.custom_id
					enemy_data_array.append(enemy_data)
					var slime_instance = slime_scene.instantiate()
				# Set the loaded data
					slime_instance.set_loaded_data(enemy_data)
				# Add the slime to the Mob node
					mob_node.add_child(slime_instance)
				assign_unique_names_to_enemies()
		else:
			return
func find_enemy_by_custom_id(custom_id: String) -> Node2D:
	var enemies = get_tree().get_nodes_in_group("Enemies")
	for slime in enemies:
		if slime.name == custom_id:
			
			return slime
	return null
func create_default_slimes(number: int):
	for i in range(number):  # You can adjust the number of default slimes
		var default_enemy_data = EnemyData.new()
		default_enemy_data.health = 4
		default_enemy_data.global_position = Vector2(randi_range(100, 500), randi_range(100, 500))
		default_enemy_data.custom_id = "default_enemy_slime" + str(i + 1)

		var default_slime_instance = slime_scene.instantiate()
		default_slime_instance.set_loaded_data(default_enemy_data)
		mob_node.add_child(default_slime_instance)

it doesn’t seem to be getting past the var existing_slimes = get_tree().get_nodes_in_group(“Enemies”)

you mean the error here?

replace with

if existing_slimes.size()==0:
		create_default_slimes(3)
		assign_unique_names_to_enemies()
		return

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.