I've got a turn based rogue like setup where every time the player moves an autoload gets all the NPCs in the map and executes a turn() in them. and a amateurishly made save and load system that loads different maps, I just save the important variables in a dictionary and to load it creates completely new NPCs and sets their values to the original NPCs.
in the turn function the game checks a state variable of and commits an action according to it. it works fine at first and everything even seems to be recreated properly when exiting and reentering a save game. but for some reason when I exit the room and reenter it, the state variable in the turn() function is allways set to 'idle'(enum for 0). I printed the state variable from the turn function and in a _process(), even in process it prints the variable accurately, but for some reason the turn function(which happens to be the most important of the functions) allways returns the idle state.
code for saving and loading:
func enter_level(level, pos, with_player = false):
var cur_level = main_screen.get_children()[0]
pack_level(cur_level, with_player)
cur_level.queue_free()
if world.has(level):
var map_scene = world[level].duplicate()
var load_level:Node = map_scene["World"]["packed_scene"].duplicate()
var children = []
for i in load_level.get_children():
children.append(i.name)
print(children)
for ps in map_scene["player_starts"]:
var ps_node = map_scene["player_starts"][ps]["packed_scene"].instance()
ps_node.name = map_scene["player_starts"][ps]["name"]
if children.has(ps_node.name):
load_level.get_node(ps_node.name).replace_by(ps_node)
else:
load_level.add_child(ps_node)
ps_node.name = map_scene["player_starts"][ps]["name"]
load_level.spawn_index = pos
main_screen.add_child(load_level)
for npc in map_scene["NPC"]:
var npc_node = map_scene["NPC"][npc]["packed_scene"].instance()
if children.has(npc_node.name):
load_level.get_node(npc_node.name).replace_by(npc_node)
else:
load_level.add_child(npc_node)
npc_node.direction = map_scene["NPC"][npc]["direction"]
npc_node.npc_state = map_scene["NPC"][npc]["npc_state"]
npc_node.HP = map_scene["NPC"][npc]["HP"]
npc_node.wander_distance = map_scene["NPC"][npc]["wander_distance"]
npc_node.attackers = names2nodes(load_level, map_scene["NPC"][npc]["attackers"])
npc_node.victims = names2nodes(load_level, map_scene["NPC"][npc]["victims"])
npc_node.attack_ready = map_scene["NPC"][npc]["attack_ready"]
if children.has(npc_node.name):
load_level.get_node(npc_node.name).replace_by(npc_node)
else:
main_screen.add_child(base_world[level])
func pack_level(top_node, with_player = false):
var NPCs = get_tree().get_nodes_in_group("NPC")
var player_starts = get_tree().get_nodes_in_group("player_starts")
for i in top_node.get_children():
if i.is_in_group("player_starts"):
player_starts.append(i)
var cur_level = PackedScene.new()
cur_level.pack(top_node)
var save_dict:Dictionary = {
"World":{
"packed_scene":cur_level.instance()
},
"NPC":{},
"player_starts":{}
}
var enumerate = 0
for npc in NPCs:
enumerate += 1
var npc_vals = {
"packed_scene":pack_sub_scene(npc),
"direction":npc.direction,
"npc_state":npc.npc_state,
"HP":npc.HP,
"wander_distance":npc.wander_distance,
"attackers":nodes2names(npc.attackers),
"victims":nodes2names(npc.victims),
"attack_ready":npc.attack_ready
}
save_dict["NPC"][enumerate] = npc_vals
enumerate = 0
for player_start in player_starts:
enumerate += 1
save_dict["player_starts"][enumerate] = {
"packed_scene":pack_sub_scene(player_start),
"name":player_start.name
}
world[top_node.name] = save_dict