Skip to content

Commit 9d42341

Browse files
committed
implement the rest of use cases
1 parent 5847f46 commit 9d42341

6 files changed

Lines changed: 32 additions & 13 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ A few gotchas to look out for:
6262

6363
As of version v0.0.2, all Yarn Spinner opcodes are currently implemented, as well as Yarn Spinner 1's internal standard library of functions and operators. As of version v0.2.1, typed versions of these functions (introduced in Yarn Spinner 2) are present, but full YS2 parity has not been verified at this time. The known features currently missing are:
6464

65-
- Inline expressions [(see Yarn docs on "Using Variables in Lines")](https://docs.yarnspinner.dev/getting-started/writing-in-yarn/logic-and-variables#using-variables-in-lines)
6665
- Line conditions and the `IsAvailable` flag on options [(see Yarn Docs on "Conditional Options")](https://docs.yarnspinner.dev/getting-started/writing-in-yarn/flow-control#conditional-options)
6766
- Localisation and Line IDs [(see Yarn's Localization docs)](https://docs.yarnspinner.dev/using-yarnspinner-with-unity/assets-and-localization)
6867
- An appropriate replacement for the distinction Yarn makes between Functions and Coroutines in Unity (to allow users to register blocking command handlers via this Python runner independent of Unity)

examples/yarn2/expressions.csv

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
id,text,file,node,lineNumber
2-
line:/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/expressions.yarn-Start-0,Hello there {0}.,/Users/sweaver/Git/YarnRunner-Python/examples/yarn2/expressions.yarn,Start,5
2+
line:C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn-Start-0,My name is {0}.,C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn,Start,7
3+
line:C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn-Start-1,I want to hug {0},C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn,Start,13
4+
line:C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn-Start-2,You punch {0},C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn,Start,14
5+
line:C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn-Start-3,I want to punch {0},C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn,Start,15
6+
line:C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn-Start-4,You hug {0},C:\Documents\development\YarnRunner-Python\examples\yarn2\expressions.yarn,Start,16

examples/yarn2/expressions.yarn

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
title: Start
22
---
33
<<set $name to "Sam">>
4+
<<set $friend to "Jose">>
45

5-
Hello there {$name}.
6+
//simple expression in line
7+
My name is {$name}.
8+
9+
//in custom command parameters
10+
<<test_command "Hello there {$name}." 5 "My name is {$name} {$friend}.">>
11+
12+
//in options
13+
-> I want to hug {$friend}
14+
You punch {$friend}
15+
-> I want to punch {$friend}
16+
You hug {$friend}
617
===

examples/yarn2/expressions.yarnc

836 Bytes
Binary file not shown.

tests/test_expressions.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@
1313
runner1 = YarnRunner(compiled_yarn_f1, names_csv_f1, autostart=False)
1414
runner2 = YarnRunner(compiled_yarn_f2, names_csv_f2, autostart=False)
1515

16-
# TODO: implement a test for expression parsing
1716

17+
def expression_command_handler(first, second, third):
18+
assert first == "Hello there Sam."
19+
assert second == "5"
20+
assert third == "My name is Sam Jose."
1821

1922
def test_expressions1():
2023
expected_line = "Hello there Sam."
2124

2225
runner1.resume()
2326

24-
actual_line = runner1.get_line()
25-
assert actual_line == expected_line
26-
27+
assert runner1.get_line() == expected_line
2728

2829
def test_expressions2():
29-
expected_line = "Hello there Sam."
30+
runner2.add_command_handler("test_command", expression_command_handler)
31+
expected_line = "My name is Sam."
3032

3133
runner2.resume()
34+
choices = runner2.get_choices()
3235

33-
actual_line = runner2.get_line()
34-
assert actual_line == expected_line
36+
assert runner2.get_line() == expected_line
37+
assert choices[0]['text'] == "I want to hug Jose"
38+
assert choices[1]['text'] == "I want to punch Jose"

yarnrunner_python/runner.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ def sanitize_quotes(arg):
256256
if len(instruction.operands) > 1:
257257
line_substitutions = self.__find_expressions(
258258
instruction.operands[1])
259-
# TODO: implement substitutions
259+
for index, arg in enumerate(args):
260+
args[index] = arg.format(*line_substitutions)
260261

261-
# TODO: maybe do some argument type parsing later
262262
ret = self._command_handlers[command](*args)
263263

264264
if type(ret) is str:
@@ -270,13 +270,14 @@ def __add_option(self, instruction):
270270

271271
# if this instruction has a second operand, it's the number of expressions
272272
# on the line that need to be evaluated.
273+
line_substitutions = []
273274
if len(instruction.operands) > 2:
274275
line_substitutions = self.__find_expressions(
275276
instruction.operands[2])
276277

277278
self._option_buffer.append({
278279
'index': len(self._option_buffer),
279-
'text': self.__lookup_string(title_string_key),
280+
'text': self.__lookup_string(title_string_key).format(*line_substitutions),
280281
'choice': choice_path
281282
})
282283

0 commit comments

Comments
 (0)