From 3298918cab8ee7b5262fa16e67ea486fce0fc130 Mon Sep 17 00:00:00 2001 From: Tim deignan Date: Sat, 23 Sep 2023 16:45:45 -0400 Subject: [PATCH 1/5] fix: while running check --- lib/nodes/decorators/While.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/nodes/decorators/While.ts b/lib/nodes/decorators/While.ts index 6906b8d..a1ad442 100644 --- a/lib/nodes/decorators/While.ts +++ b/lib/nodes/decorators/While.ts @@ -32,7 +32,7 @@ export class While extends Decorator { protected decorateCall(handleEvent: (state: S, event: E) => ResultCode, state: S, event: E) { const storage: WhileNodeStorage = this.getNodeStorage(state); - if (storage.running || this.conditional(state, event)) { + if (storage.lastLoopResult === rc.RUNNING || this.conditional(state, event)) { if (storage.beganAtLeastOneLoop) { Action.treePublisher.publishResult(state, event, false); clearEventSeenRecursive(this.child, state); @@ -52,6 +52,7 @@ export class While extends Decorator { if (res === rc.RUNNING) { // yield to the behavior tree because the child node is running + storage.lastLoopResult = res; return res; } else if (storage.break) { // teardown internal state and yield to the behavior tree because the loop has completed From 26858bd5f8f9f4f295ef6503d090d2ee954b9105 Mon Sep 17 00:00:00 2001 From: Tim deignan Date: Sat, 23 Sep 2023 23:37:27 -0400 Subject: [PATCH 2/5] feat: use storage.running instead of storage.lastLoopResult --- lib/nodes/decorators/While.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nodes/decorators/While.ts b/lib/nodes/decorators/While.ts index a1ad442..75fed75 100644 --- a/lib/nodes/decorators/While.ts +++ b/lib/nodes/decorators/While.ts @@ -32,7 +32,7 @@ export class While extends Decorator { protected decorateCall(handleEvent: (state: S, event: E) => ResultCode, state: S, event: E) { const storage: WhileNodeStorage = this.getNodeStorage(state); - if (storage.lastLoopResult === rc.RUNNING || this.conditional(state, event)) { + if (storage.running !== undefined || this.conditional(state, event)) { if (storage.beganAtLeastOneLoop) { Action.treePublisher.publishResult(state, event, false); clearEventSeenRecursive(this.child, state); @@ -52,7 +52,7 @@ export class While extends Decorator { if (res === rc.RUNNING) { // yield to the behavior tree because the child node is running - storage.lastLoopResult = res; + storage.running = 0; return res; } else if (storage.break) { // teardown internal state and yield to the behavior tree because the loop has completed From 3ec991329feebdb21c007d59df98396e57280054 Mon Sep 17 00:00:00 2001 From: Tim deignan Date: Mon, 25 Sep 2023 00:46:49 -0400 Subject: [PATCH 3/5] feat: override onEvent in while --- lib/nodes/decorators/While.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/nodes/decorators/While.ts b/lib/nodes/decorators/While.ts index 75fed75..79579a6 100644 --- a/lib/nodes/decorators/While.ts +++ b/lib/nodes/decorators/While.ts @@ -71,6 +71,10 @@ export class While extends Decorator { } } + protected onEvent(state: S, event: E): ResultCode { + return this.handleChild(state, event); + } + get symbol(): string { return '↻'; } From 4c66a22e1ad07ee95e6fc0c06a17c39eddea5904 Mon Sep 17 00:00:00 2001 From: Tim deignan Date: Wed, 27 Sep 2023 12:44:42 -0400 Subject: [PATCH 4/5] feat: rm storage.running = 0 in while --- lib/nodes/decorators/While.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/nodes/decorators/While.ts b/lib/nodes/decorators/While.ts index 79579a6..19fea13 100644 --- a/lib/nodes/decorators/While.ts +++ b/lib/nodes/decorators/While.ts @@ -52,7 +52,6 @@ export class While extends Decorator { if (res === rc.RUNNING) { // yield to the behavior tree because the child node is running - storage.running = 0; return res; } else if (storage.break) { // teardown internal state and yield to the behavior tree because the loop has completed From 163aaa1e2ee2b564f18b7f7cd881f17c582e223d Mon Sep 17 00:00:00 2001 From: Tim deignan Date: Wed, 27 Sep 2023 12:52:07 -0400 Subject: [PATCH 5/5] feat: improve test --- test/nodes/decorators/While.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/nodes/decorators/While.test.ts b/test/nodes/decorators/While.test.ts index dba61f2..9291a8f 100644 --- a/test/nodes/decorators/While.test.ts +++ b/test/nodes/decorators/While.test.ts @@ -110,8 +110,8 @@ describe('While', function () { it('latches', function () { const testAction = new TestAction('testAction', [ - { rc: rc.RUNNING, inc: false }, - { rc: rc.SUCCESS, inc: true }, + { rc: rc.RUNNING, inc: true }, + { rc: rc.SUCCESS, inc: false }, ]); const uut = new decorators.While( @@ -129,17 +129,17 @@ describe('While', function () { let result = uut.handleEvent(state, 0); assert.strictEqual(rc.RUNNING, result); - assert.strictEqual(state.counter, 0); + assert.strictEqual(state.counter, 1); assert.strictEqual(testAction.eventCount, 1); result = uut.handleEvent(state, 0); assert.strictEqual(rc.RUNNING, result); - assert.strictEqual(state.counter, 1); + assert.strictEqual(state.counter, 2); assert.strictEqual(testAction.eventCount, 3); result = uut.handleEvent(state, 0); assert.strictEqual(rc.RUNNING, result); - assert.strictEqual(state.counter, 2); + assert.strictEqual(state.counter, 3); assert.strictEqual(testAction.eventCount, 5); result = uut.handleEvent(state, 0);