feat: Add global interpolation for say command (#562)

Co-authored-by: Balloonpopper <balloonpopper@git.com>
This commit is contained in:
balloonpopper
2022-04-05 16:02:17 +10:00
committed by GitHub
parent 9cc06679c9
commit 167e06bfd5
4 changed files with 36 additions and 1 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -1,4 +1,5 @@
:setup
set_global r4_door_look_count 0
> [eq ESC_LAST_SCENE room3]
teleport player l_exit

View File

@@ -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"]