Skip to content

Commit

Permalink
fix: Read variables recursively in any group
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Sep 1, 2021
1 parent 9a03ebf commit 3f5a698
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/VariablesExtra.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ITiledMapObject } from "@workadventure/tiled-map-type-guard/dist";
import type {ITiledMapLayer, ITiledMapObject} from "@workadventure/tiled-map-type-guard/dist";
import { Properties } from "./Properties";

export class VariableDescriptor {
Expand Down Expand Up @@ -36,15 +36,21 @@ export async function getAllVariables(): Promise<Map<string, VariableDescriptor>

const variables = new Map<string, VariableDescriptor>();

for (const layer of map.layers) {
getAllVariablesRecursive(map.layers, variables);

return variables;
}

function getAllVariablesRecursive(layers: ITiledMapLayer[], variables: Map<string, VariableDescriptor>): void {
for (const layer of layers) {
if (layer.type === "objectgroup") {
for (const object of layer.objects) {
if (object.type === "variable") {
variables.set(object.name, new VariableDescriptor(object));
}
}
} else if (layer.type === "group") {
getAllVariablesRecursive(layer.layers, variables);
}
}

return variables;
}

0 comments on commit 3f5a698

Please sign in to comment.