Skip to content

Commit d20ac2b

Browse files
committed
Fix bug getting patched variable.
1 parent 9af5d21 commit d20ac2b

6 files changed

Lines changed: 56 additions & 6 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Matching Ink v0.9.0
2-
version=0.7.3
2+
version=0.7.4
33

src/main/java/com/bladecoder/ink/runtime/StoryState.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ void pushEvaluationStack(RTObject obj) {
475475

476476
if (rawList.getOrigins() == null)
477477
rawList.setOrigins(new ArrayList<ListDefinition>());
478+
478479
rawList.getOrigins().clear();
479480

480481
for (String n : rawList.getOriginNames()) {

src/main/java/com/bladecoder/ink/runtime/VariablesState.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,24 @@ RTObject getRawVariableWithName(String name, int contextIndex) throws Exception
216216
RTObject varValue = null;
217217
// 0 context = global
218218
if (contextIndex == 0 || contextIndex == -1) {
219+
if (patch != null && patch.getGlobal(name) != null)
220+
return patch.getGlobal(name);
221+
219222
varValue = globalVariables.get(name);
220223
if (varValue != null) {
221224
return varValue;
222225
}
223226

227+
// Getting variables can actually happen during globals set up since you can do
228+
// VAR x = A_LIST_ITEM
229+
// So _defaultGlobalVariables may be null.
230+
// We need to do this check though in case a new global is added, so we need to
231+
// revert to the default globals dictionary since an initial value hasn't yet
232+
// been set.
233+
if (defaultGlobalVariables != null && defaultGlobalVariables.containsKey(name)) {
234+
return defaultGlobalVariables.get(name);
235+
}
236+
224237
ListValue listItemValue = listDefsOrigin.findSingleItemListWithName(name);
225238
if (listItemValue != null)
226239
return listItemValue;

src/test/java/com/bladecoder/ink/runtime/test/ListSpecTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public void testListSaveLoad() throws Exception {
7777
story.choosePathString("elsewhere");
7878
// FIXME: This is the test from the C# impl. Is it correct?
7979
// Assert.assertEquals("a, x, c, z\n", story.continueMaximally());
80-
80+
8181
Assert.assertEquals("z\n", story.continueMaximally());
8282
}
83-
83+
8484
/**
8585
* "- TestEmptyListOriginAfterAssignment"
8686
*/
@@ -92,14 +92,35 @@ public void testEmptyListOriginAfterAssignment() throws Exception {
9292

9393
Assert.assertEquals("a, b, c\n", story.continueMaximally());
9494
}
95-
96-
//@Test
95+
96+
// @Test
9797
public void testListRange() throws Exception {
9898

9999
String json = TestUtils.getJsonString("inkfiles/lists/list-range.ink.json");
100100
Story story = new Story(json);
101101

102-
Assert.assertEquals("Pound, Pizza, Euro, Pasta, Dollar, Curry, Paella\nEuro, Pasta, Dollar, Curry\nTwo, Three, Four, Five, Six\nPizza, Pasta\n", story.continueMaximally());
102+
Assert.assertEquals(
103+
"Pound, Pizza, Euro, Pasta, Dollar, Curry, Paella\nEuro, Pasta, Dollar, Curry\nTwo, Three, Four, Five, Six\nPizza, Pasta\n",
104+
story.continueMaximally());
105+
}
106+
107+
@Test
108+
public void testBugAddingElement() throws Exception {
109+
110+
String json = TestUtils.getJsonString("inkfiles/lists/bug-adding-element.ink.json");
111+
Story story = new Story(json);
112+
113+
String s = story.continueMaximally();
114+
Assert.assertEquals("", s);
115+
116+
story.chooseChoiceIndex(0);
117+
s = story.continueMaximally();
118+
Assert.assertEquals("a\n", s);
119+
120+
story.chooseChoiceIndex(1);
121+
s = story.continueMaximally();
122+
Assert.assertEquals("OK\n", s);
123+
103124
}
104125

105126
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
LIST gameState = KNOW_ALIEN_REPORT
2+
3+
- (init)
4+
5+
+ a
6+
~ gameState += KNOW_ALIEN_REPORT
7+
-> init
8+
9+
+ {gameState ? KNOW_ALIEN_REPORT} OK
10+
-> init
11+
12+
+ FAIL
13+
-> END
14+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"inkVersion":19,"root":[[[["ev",{"^->":"0.init.0.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-0","flg":2},{"s":["^a",{"->":"$r","var":true},null]}],["ev",{"^->":"0.init.1.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str",{"VAR?":"gameState"},{"VAR?":"KNOW_ALIEN_REPORT"},"?","/ev",{"*":".^.^.c-1","flg":3},{"s":["^OK",{"->":"$r","var":true},null]}],["ev",{"^->":"0.init.2.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.^.c-2","flg":2},{"s":["^FAIL",{"->":"$r","var":true},null]}],{"c-0":["ev",{"^->":"0.init.c-0.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.0.s"},[{"#n":"$r2"}],"\n","ev",{"VAR?":"gameState"},{"VAR?":"KNOW_ALIEN_REPORT"},"+",{"VAR=":"gameState","re":true},"/ev",{"->":".^.^"},{"->":"0.g-0"},{"#f":5}],"c-1":["ev",{"^->":"0.init.c-1.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.1.s"},[{"#n":"$r2"}],"\n",{"->":".^.^"},{"->":"0.g-0"},{"#f":5}],"c-2":["ev",{"^->":"0.init.c-2.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.2.s"},[{"#n":"$r2"}],"\n","end",{"->":"0.g-0"},{"#f":5}],"#f":5,"#n":"init"}],{"g-0":["done",{"#f":5}]}],"done",{"global decl":["ev",{"list":{},"origins":["gameState"]},{"VAR=":"gameState"},"/ev","end",null],"#f":1}],"listDefs":{"gameState":{"KNOW_ALIEN_REPORT":1}}}

0 commit comments

Comments
 (0)