From 7ff0176d62a21f6a9450b3a63c23231efd7dc979 Mon Sep 17 00:00:00 2001 From: Duncan Brown Date: Wed, 23 Mar 2022 18:10:09 -0400 Subject: [PATCH] fix: allows for proper reloading of all globals (reserved or otherwise); also now properly saves reserved objects; plus a couple small guards to correct issues while quitting after loading games in certain conditions --- .../core-scripts/esc/commands/set_global.gd | 15 +++++++++++---- .../core-scripts/esc/esc_object_manager.gd | 14 ++++++++++++-- .../game/core-scripts/esc/esc_room_manager.gd | 3 +-- .../save_data/esc_save_manager.gd | 19 +++++++++++++------ addons/escoria-core/game/escoria.gd | 6 +++++- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/addons/escoria-core/game/core-scripts/esc/commands/set_global.gd b/addons/escoria-core/game/core-scripts/esc/commands/set_global.gd index 74598d9d..dbdca3aa 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/set_global.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/set_global.gd @@ -1,4 +1,4 @@ -# `set_global name value` +# `set_global name value [ignore_reserved=false]` # # Changes the value of a global. # @@ -7,6 +7,9 @@ # - *name*: Name of the global # - *value*: Value to set the global to (can be of type string, boolean, integer # or float) +# - *force*: if false, setting a global whose name is reserved will +# trigger an error. Defaults to false. Reserved globals are: ESC_LAST_SCENE, +# FORCE_LAST_SCENE_NULL, ANIMATION_RESOURCES, ESC_CURRENT_SCENE # # @ESC extends ESCBaseCommand @@ -17,12 +20,16 @@ class_name SetGlobalCommand func configure() -> ESCCommandArgumentDescriptor: return ESCCommandArgumentDescriptor.new( 2, - [TYPE_STRING, [TYPE_INT, TYPE_BOOL, TYPE_STRING]], - [null, null] + [TYPE_STRING, [TYPE_INT, TYPE_BOOL, TYPE_STRING], TYPE_BOOL], + [null, null, false] ) # Run the command func run(command_params: Array) -> int: - escoria.globals_manager.set_global(command_params[0], command_params[1]) + escoria.globals_manager.set_global( + command_params[0], + command_params[1], + command_params[2] + ) return ESCExecution.RC_OK diff --git a/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd b/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd index dcb30191..2dae1a00 100644 --- a/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd +++ b/addons/escoria-core/game/core-scripts/esc/esc_object_manager.gd @@ -344,8 +344,9 @@ func unregister_object(object: ESCObject, room_key: ESCRoomObjectsKey) -> void: if escoria.inventory_manager.inventory_has(object.global_id): # Re-instance the node if it is an item present in inventory; that is, # re-register it with the new current room. - object.node = object.node.duplicate() - register_object(object, null) + if object.node != null: + object.node = object.node.duplicate() + register_object(object, null, true) room_objects.erase(object.global_id) @@ -389,6 +390,15 @@ func save_game(p_savegame: ESCSaveGame) -> void: p_savegame.objects[obj_global_id] = \ objects[obj_global_id].get_save_data() + # Add in reserved objects, too. + objects = reserved_objects_container.objects + + for obj_global_id in objects: + if not objects[obj_global_id] is ESCObject: + continue + p_savegame.objects[obj_global_id] = \ + objects[obj_global_id].get_save_data() + func get_start_location() -> ESCLocation: if _room_exists(current_room_key): diff --git a/addons/escoria-core/game/core-scripts/esc/esc_room_manager.gd b/addons/escoria-core/game/core-scripts/esc/esc_room_manager.gd index 3cae60b1..2e154c4b 100644 --- a/addons/escoria-core/game/core-scripts/esc/esc_room_manager.gd +++ b/addons/escoria-core/game/core-scripts/esc/esc_room_manager.gd @@ -62,8 +62,7 @@ func change_scene(room_path: String, enable_automatic_transitions: bool) -> void # This must happen if ESC_LAST_SCENE is set, or if we're running an # exit_scene event. Also room selector actions require the transition. if enable_automatic_transitions and ( - not escoria.globals_manager.get_global( \ - GLOBAL_LAST_SCENE).empty() + not escoria.globals_manager.get_global(GLOBAL_LAST_SCENE).empty() or ( escoria.event_manager.get_running_event(escoria.event_manager.CHANNEL_FRONT) != null \ and escoria.event_manager.get_running_event(escoria.event_manager.CHANNEL_FRONT).name \ diff --git a/addons/escoria-core/game/core-scripts/save_data/esc_save_manager.gd b/addons/escoria-core/game/core-scripts/save_data/esc_save_manager.gd index ccf4d522..b7a9b808 100644 --- a/addons/escoria-core/game/core-scripts/save_data/esc_save_manager.gd +++ b/addons/escoria-core/game/core-scripts/save_data/esc_save_manager.gd @@ -293,12 +293,19 @@ func load_game(id: int): ## GLOBALS for k in save_game.globals.keys(): - ESCCommand.new("%s %s %s" % - [ - _set_global.get_command_name(), - k, - save_game.globals[k] - ] + var global_value = save_game.globals[k] + + if global_value is String and global_value.empty(): + global_value = "''" + + load_statements.append( + ESCCommand.new("%s %s %s true" % + [ + _set_global.get_command_name(), + k, + global_value + ] + ) ) ## OBJECTS diff --git a/addons/escoria-core/game/escoria.gd b/addons/escoria-core/game/escoria.gd index 2cdcbae0..7a574b9e 100644 --- a/addons/escoria-core/game/escoria.gd +++ b/addons/escoria-core/game/escoria.gd @@ -226,7 +226,11 @@ func set_game_paused(p_paused: bool): emit_signal("paused") else: emit_signal("resumed") - get_tree().paused = p_paused + + var scene_tree = get_tree() + + if is_instance_valid(scene_tree): + scene_tree.paused = p_paused # Runs the event "event_name" from the "script" ESC script.