diff --git a/addons/escoria-core/game/core-scripts/esc/commands/say.gd b/addons/escoria-core/game/core-scripts/esc/commands/say.gd index ecabd253..7bdc4857 100644 --- a/addons/escoria-core/game/core-scripts/esc/commands/say.gd +++ b/addons/escoria-core/game/core-scripts/esc/commands/say.gd @@ -4,6 +4,10 @@ # blocks further event execution until the dialog has finished being 'said' # (either as displayed text or as audible speech from a file). # +# Global variables can be substituted into the text by wrapping the global +# name in braces. +# e.g. say player "I have {coin_count} coins remaining". +# # **Parameters** # # - *player*: Global ID of the `ESCPlayer` or `ESCItem` object that is active @@ -21,6 +25,16 @@ extends ESCBaseCommand class_name SayCommand +var globals_regex : RegEx # Regex to match global variables in strings + + +# Constructor +func _init() -> void: + globals_regex = RegEx.new() + # Use look-ahead/behind to capture the term (i.e. global) in braces + globals_regex.compile("(?<=\\{)(.*)(?=\\})") + + # Return the descriptor of the arguments of this command func configure() -> ESCCommandArgumentDescriptor: return ESCCommandArgumentDescriptor.new( @@ -78,6 +92,8 @@ func run(command_params: Array) -> int: ) return ESCExecution.RC_ERROR + command_params[1] = _replace_globals(command_params[1]) + escoria.dialog_player.say( command_params[0], command_params[2], @@ -86,3 +102,16 @@ func run(command_params: Array) -> int: yield(escoria.dialog_player, "say_finished") escoria.current_state = escoria.GAME_STATE.DEFAULT return ESCExecution.RC_OK + + +# Replaces terms in braces with the value of the matching global (or Null +# if none exists) +func _replace_globals(string: String): + for result in globals_regex.search_all(string): + var globresult = escoria.globals_manager.get_global( + str(result.get_string()) + ) + string = string.replace( + "{" + result.get_string() + "}", str(globresult) + ) + return string diff --git a/game/rooms/room04/esc/left_exit.esc b/game/rooms/room04/esc/left_exit.esc index 0f38d1c7..13ed511b 100755 --- a/game/rooms/room04/esc/left_exit.esc +++ b/game/rooms/room04/esc/left_exit.esc @@ -1,2 +1,7 @@ :exit_scene change_scene "res://game/rooms/room03/room03.tscn" + +:look +# Test globals in say command +inc_global r4_door_look_count 1 +say player "You have looked at the doorway {r4_door_look_count} times" \ No newline at end of file diff --git a/game/rooms/room04/esc/room04.esc b/game/rooms/room04/esc/room04.esc index 03a51529..3ebca5dc 100644 --- a/game/rooms/room04/esc/room04.esc +++ b/game/rooms/room04/esc/room04.esc @@ -1,4 +1,5 @@ :setup +set_global r4_door_look_count 0 > [eq ESC_LAST_SCENE room3] teleport player l_exit diff --git a/game/rooms/room04/room04.tscn b/game/rooms/room04/room04.tscn index dbbfafc5..cb998244 100644 --- a/game/rooms/room04/room04.tscn +++ b/game/rooms/room04/room04.tscn @@ -131,7 +131,7 @@ animations = null polygon = PoolVector2Array( 22, 633, 21, 328, 143, 276, 143, 565 ) [node name="Position2D" type="Position2D" parent="Hotspots/l_door"] -position = Vector2( 80, 589 ) +position = Vector2( 92, 603 ) script = ExtResource( 9 ) [node name="r_door" type="Area2D" parent="Hotspots"]