feat: add esc_current_scene reserved global (#474)
Co-authored-by: Duncan Brown <duncan@bhs-consultants.com>
This commit is contained in:
@@ -66,13 +66,14 @@ func run(command_params: Array) -> int:
|
||||
# exit_scene event. Also room selector actions require the transition.
|
||||
if command_params[1] \
|
||||
and (
|
||||
not escoria.globals_manager.get_global("ESC_LAST_SCENE").empty()
|
||||
not escoria.globals_manager.get_global( \
|
||||
escoria.room_manager.GLOBAL_LAST_SCENE).empty()
|
||||
or (
|
||||
escoria.event_manager.get_running_event("_front") != null \
|
||||
and escoria.event_manager.get_running_event("_front").name \
|
||||
in ["newgame", "exit_scene", "room_selector"]
|
||||
and escoria.globals_manager.get_global(
|
||||
"ESC_LAST_SCENE"
|
||||
escoria.room_manager.GLOBAL_LAST_SCENE
|
||||
).empty()
|
||||
)
|
||||
):
|
||||
@@ -91,16 +92,18 @@ func run(command_params: Array) -> int:
|
||||
escoria.game_scene.unpause_game()
|
||||
|
||||
# If FORCE_LAST_SCENE_NULL is true, force ESC_LAST_SCENE to empty
|
||||
if escoria.globals_manager.get_global("FORCE_LAST_SCENE_NULL"):
|
||||
if escoria.globals_manager.get_global( \
|
||||
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL):
|
||||
|
||||
escoria.globals_manager.set_global(
|
||||
"ESC_LAST_SCENE",
|
||||
escoria.room_manager.GLOBAL_LAST_SCENE,
|
||||
null,
|
||||
true
|
||||
)
|
||||
elif escoria.main.current_scene:
|
||||
# If FORCE_LAST_SCENE_NULL is false, set ESC_LAST_SCENE = current roomid
|
||||
escoria.globals_manager.set_global(
|
||||
"ESC_LAST_SCENE",
|
||||
escoria.room_manager.GLOBAL_LAST_SCENE,
|
||||
escoria.main.current_scene.global_id,
|
||||
true
|
||||
)
|
||||
@@ -150,6 +153,14 @@ func run(command_params: Array) -> int:
|
||||
room_scene.game = escoria.game_scene
|
||||
escoria.main.set_scene(room_scene)
|
||||
|
||||
# We know the scene has been loaded. Make its global ID available for
|
||||
# use by ESC script.
|
||||
escoria.globals_manager.set_global(
|
||||
escoria.room_manager.GLOBAL_CURRENT_SCENE,
|
||||
room_scene.global_id,
|
||||
true
|
||||
)
|
||||
|
||||
# Clear queued resources
|
||||
escoria.resource_cache.clear()
|
||||
|
||||
|
||||
@@ -5,30 +5,16 @@ extends Resource
|
||||
class_name ESCGlobalsManager
|
||||
|
||||
|
||||
const GLOBAL_ANIMATION_RESOURCES = "ANIMATION_RESOURCES"
|
||||
|
||||
|
||||
# Emitted when a global is changed
|
||||
signal global_changed(global, old_value, new_value)
|
||||
|
||||
|
||||
# A list of reserved globals which can not be overridden
|
||||
const RESERVED_GLOBALS = [
|
||||
"ESC_LAST_SCENE", # Contains the global_id of previous room
|
||||
"FORCE_LAST_SCENE_NULL", # If true, ESC_LAST_SCENE is not considered for
|
||||
# automatic transitions
|
||||
"ANIMATION_RESOURCES"
|
||||
]
|
||||
|
||||
|
||||
# The globals registry
|
||||
export(Dictionary) var _globals = {}
|
||||
|
||||
|
||||
|
||||
func _init():
|
||||
set_global("ESC_LAST_SCENE", "", true)
|
||||
set_global("FORCE_LAST_SCENE_NULL", false, true)
|
||||
# Registry of globals that are to be reserved for internal use only.
|
||||
var _reserved_globals: Dictionary = {}
|
||||
|
||||
|
||||
# Check if a global was registered
|
||||
@@ -41,6 +27,27 @@ func has(key: String) -> bool:
|
||||
return _globals.has(key)
|
||||
|
||||
|
||||
# Registers a global as being reserved and initializes it.
|
||||
#
|
||||
# #### Parameters
|
||||
#
|
||||
# - key: The key of the global to register
|
||||
# - value: The initial value (optional)
|
||||
func register_reserved_global(key: String, value = null) -> void:
|
||||
if key in _reserved_globals:
|
||||
escoria.logger.report_errors(
|
||||
"ESCGlobalsManager.register_reserved_global: Can not override reserved global",
|
||||
[
|
||||
"Global key %s is already registered as reserved" % key
|
||||
]
|
||||
)
|
||||
_reserved_globals[key] = value
|
||||
_globals[key] = value
|
||||
|
||||
if value != null:
|
||||
emit_signal("global_changed", key, _globals[key], value)
|
||||
|
||||
|
||||
# Get the current value of a global
|
||||
#
|
||||
# #### Parameters
|
||||
@@ -77,7 +84,7 @@ func filter(pattern: String) -> Dictionary:
|
||||
# - key: The key of the global to modify
|
||||
# - value: The new value
|
||||
func set_global(key: String, value, ignore_reserved: bool = false) -> void:
|
||||
if key in RESERVED_GLOBALS and not ignore_reserved:
|
||||
if key in _reserved_globals and not ignore_reserved:
|
||||
escoria.logger.report_errors(
|
||||
"ESCGlobalsManager.set_global: Can not override reserved global",
|
||||
[
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
extends Object
|
||||
class_name ESCRoomManager
|
||||
|
||||
|
||||
# Reserved globals which can not be overridden; prefixed with "GLOBAL_"
|
||||
#
|
||||
# Contains the global_id of previous room
|
||||
const GLOBAL_LAST_SCENE = "ESC_LAST_SCENE"
|
||||
|
||||
# If true, ESC_LAST_SCENE is not considered for automatic transitions
|
||||
const GLOBAL_FORCE_LAST_SCENE_NULL = "FORCE_LAST_SCENE_NULL"
|
||||
|
||||
const GLOBAL_ANIMATION_RESOURCES = "ANIMATION_RESOURCES"
|
||||
|
||||
# Contains the global_id of the current room
|
||||
const GLOBAL_CURRENT_SCENE = "ESC_CURRENT_SCENE"
|
||||
|
||||
# Dict of the reserved globals to register and their initial values.
|
||||
const RESERVED_GLOBALS = {
|
||||
GLOBAL_LAST_SCENE: "",
|
||||
GLOBAL_FORCE_LAST_SCENE_NULL: false,
|
||||
GLOBAL_ANIMATION_RESOURCES: [],
|
||||
GLOBAL_CURRENT_SCENE: ""
|
||||
}
|
||||
|
||||
|
||||
# Registers all reserved global flags for use.
|
||||
func register_reserved_globals() -> void:
|
||||
for key in RESERVED_GLOBALS:
|
||||
escoria.globals_manager.register_reserved_global( \
|
||||
key,
|
||||
RESERVED_GLOBALS[key])
|
||||
@@ -96,10 +96,10 @@ func _ready():
|
||||
true
|
||||
)
|
||||
if escoria.globals_manager.has(
|
||||
escoria.globals_manager.GLOBAL_ANIMATION_RESOURCES
|
||||
escoria.room_manager.GLOBAL_ANIMATION_RESOURCES
|
||||
):
|
||||
var animations = escoria.globals_manager.get_global(
|
||||
escoria.globals_manager.GLOBAL_ANIMATION_RESOURCES
|
||||
escoria.room_manager.GLOBAL_ANIMATION_RESOURCES
|
||||
)
|
||||
|
||||
if player.global_id in animations and \
|
||||
@@ -168,7 +168,8 @@ func perform_script_events():
|
||||
if enabled_automatic_transitions \
|
||||
or (
|
||||
not enabled_automatic_transitions \
|
||||
and escoria.globals_manager.get_global("FORCE_LAST_SCENE_NULL")
|
||||
and escoria.globals_manager.get_global( \
|
||||
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL)
|
||||
):
|
||||
var script_transition_in = escoria.esc_compiler.compile([
|
||||
":transition_in",
|
||||
@@ -195,18 +196,28 @@ func perform_script_events():
|
||||
|
||||
# Now that :ready is finished, if FORCE_LAST_SCENE_NULL was true, reset it
|
||||
# to false
|
||||
if escoria.globals_manager.get_global("FORCE_LAST_SCENE_NULL"):
|
||||
if escoria.globals_manager.get_global( \
|
||||
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL):
|
||||
|
||||
escoria.globals_manager.set_global(
|
||||
"FORCE_LAST_SCENE_NULL",
|
||||
escoria.room_manager.GLOBAL_FORCE_LAST_SCENE_NULL,
|
||||
false,
|
||||
true
|
||||
)
|
||||
escoria.globals_manager.set_global(
|
||||
"ESC_LAST_SCENE",
|
||||
escoria.room_manager.GLOBAL_LAST_SCENE,
|
||||
escoria.main.current_scene.global_id \
|
||||
if escoria.main.current_scene != null else "",
|
||||
true
|
||||
)
|
||||
|
||||
# Make the room's global ID available for use in ESC script.
|
||||
escoria.globals_manager.set_global(
|
||||
escoria.room_manager.GLOBAL_CURRENT_SCENE,
|
||||
escoria.main.current_scene.global_id \
|
||||
if escoria.main.current_scene != null else "",
|
||||
true
|
||||
)
|
||||
|
||||
|
||||
# Runs the script event from the script attached, if any.
|
||||
|
||||
Reference in New Issue
Block a user