Skip to content

Commit b5f412e

Browse files
committed
Fix loading callbacks from saved inventories.
1 parent 752c58c commit b5f412e

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

blade-engine/src/com/bladecoder/engine/model/World.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,8 +1266,18 @@ public void read(Json json, JsonValue jsonData) {
12661266
getInkManager().read(json, jsonData.get("inkManager"));
12671267
}
12681268

1269-
inventories = json.readValue("inventories", HashMap.class, Inventory.class, jsonData);
1269+
// inventories have to be put in the hash to find the actors when reading saved data
12701270
currentInventory = json.readValue("currentInventory", String.class, jsonData);
1271+
1272+
JsonValue jsonInventories = jsonData.get("inventories");
1273+
inventories = new HashMap<String,Inventory>();
1274+
1275+
for (int i = 0; i < jsonInventories.size; i++) {
1276+
JsonValue jsonValue = jsonInventories.get(i);
1277+
Inventory inv = new Inventory();
1278+
inventories.put(jsonValue.name, inv);
1279+
inv.read(json, jsonValue);
1280+
}
12711281

12721282
if (jsonData.get("uiActors") != null) {
12731283
getUIActors().read(json, jsonData.get("uiActors"));

blade-engine/src/com/bladecoder/engine/util/ActionCallbackSerialization.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.bladecoder.engine.ink.InkManager;
2121
import com.bladecoder.engine.model.BaseActor;
2222
import com.bladecoder.engine.model.InteractiveActor;
23+
import com.bladecoder.engine.model.Inventory;
2324
import com.bladecoder.engine.model.Scene;
2425
import com.bladecoder.engine.model.UIActors;
2526
import com.bladecoder.engine.model.Verb;
@@ -48,6 +49,7 @@ public class ActionCallbackSerialization {
4849
private static final String SEPARATION_SYMBOL = "#";
4950
private static final String INK_MANAGER_TAG = "INK_MANAGER";
5051
private static final String UIACTORS_TAG = "UIACTORS";
52+
private static final String INVENTORY_TAG = "INVENTORY";
5153
private static final String DEFAULT_VERB_TAG = "DEFAULT_VERB";
5254

5355
private static String find(ActionCallback cb, Verb v) {
@@ -152,6 +154,22 @@ private static String find(ActionCallback cb, UIActors uia) {
152154

153155
return null;
154156
}
157+
158+
private static String find(ActionCallback cb, Inventory inv) {
159+
for (int i =0; i < inv.getNumItems(); i++) {
160+
InteractiveActor a = inv.get(i);
161+
String id = find(cb, a);
162+
163+
if (id != null) {
164+
StringBuilder stringBuilder = new StringBuilder(INVENTORY_TAG);
165+
stringBuilder.append(SEPARATION_SYMBOL).append(id);
166+
167+
return stringBuilder.toString();
168+
}
169+
}
170+
171+
return null;
172+
}
155173

156174
/**
157175
* Generates a String for serialization that allows locate the
@@ -171,6 +189,12 @@ public static String find(ActionCallback cb) {
171189
// search in UIActors
172190
id = find(cb, World.getInstance().getUIActors());
173191

192+
if (id != null)
193+
return id;
194+
195+
// search in inventory
196+
id = find(cb, World.getInstance().getInventory());
197+
174198
if (id != null)
175199
return id;
176200

@@ -188,6 +212,7 @@ public static String find(ActionCallback cb) {
188212
if (id != null)
189213
return id;
190214

215+
// search in player
191216
id = find(cb, s.getPlayer());
192217
if (id != null)
193218
return id;
@@ -248,7 +273,7 @@ public static ActionCallback find(String id) {
248273
String verbId;
249274
int actionPos = -1;
250275

251-
if (id.startsWith(UIACTORS_TAG)) {
276+
if (id.startsWith(UIACTORS_TAG) || id.startsWith(INVENTORY_TAG)) {
252277

253278
actorId = split[1];
254279
verbId = split[2];
@@ -276,15 +301,20 @@ public static ActionCallback find(String id) {
276301
} else {
277302
a = (InteractiveActor) s.getActor(actorId, true);
278303

279-
if (a == null)
304+
if (a == null) {
305+
EngineLogger.error("ActionCallbackSerialization - Actor not found: " + actorId + " cb: " + id);
280306
return null;
307+
}
281308

282309
v = a.getVerbManager().getVerbs().get(verbId);
283310
}
284311
}
285312

286-
if (v == null)
313+
if (v == null) {
314+
EngineLogger.error("ActionCallbackSerialization - Verb not found: " + verbId + " cb: " + id);
315+
287316
return null;
317+
}
288318

289319
if (actionPos == -1)
290320
return v;
@@ -293,6 +323,8 @@ public static ActionCallback find(String id) {
293323

294324
if (action instanceof ActionCallback)
295325
return (ActionCallback) action;
326+
327+
EngineLogger.error("ActionCallbackSerialization - CB not found: " + id);
296328

297329
return null;
298330
}

0 commit comments

Comments
 (0)