libgdx / gdx-ai

Artificial Intelligence framework for games based on libGDX or not. Features: Steering Behaviors, Formation Motion, Pathfinding, Behavior Trees and Finite State Machines
Apache License 2.0
1.18k stars 241 forks source link

Parallel task repeats other tasks. #91

Closed lukz closed 7 years ago

lukz commented 7 years ago

Is it intended for parallel task to repeat all tasks until all tasks return success condition or one return fail condition (depending on policy)?

I want to make a monster which starts to move and at the same time shoot 5 times. Movement can take a lot longer than shooting. No matter how I construct this parallel task monster is shooting constantly until movement task is finished or stop moving after finishing shooting.

To demonstrate:

root
  sequence
    parallel policy:"sequence"
      sequence
        wait seconds:0.3f
        log text:"TASK1"
      log text:"TASK2"
    log text:"END"
    wait seconds:10

(Log task returns SUCCESS instantly after execution)

This is output:

GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK2
GDXAI: TASK1
GDXAI: TASK2
GDXAI: END
mgsx-dev commented 7 years ago

In short Yes, parallel task will repeat all tasks (it restarts completed tasks).

For your case, you need an extra task to know when to stop shooting (when not moving). It could be something like this :

root
  sequence
    parallel policy:"sequence"
      sequence
        wait seconds:0.3f
        log text:"TASK1 (move finished)"
      sequence
        repeat times:5
          log text:"TASK2 (shoot once)"
        waitIdleState
    log text:"END"
    wait seconds:10

where "waitIdleState" is returning :

lukz commented 7 years ago

@mgsx-dev this seems to break whole idea of reusing tasks in BT without additional code. I guess I'll just replace parallel task with my own one ;)

davebaol commented 7 years ago

@lukz What if you wrap the shoot task with a custom decorator alwaysRunning that always returns STATUS_RUNNING regardless of child's status? Actually, I think that this kind of decorator might be part of the framework. PR welcome :smiley:

davebaol commented 7 years ago

@lukz I think that commit f747fbdc5e7c7fa2f3f1ae414c84a0c198414f70 implements exactly what you asked for, see PR https://github.com/libgdx/gdx-ai/pull/93 The wiki should be updated soon. Let me know if it's enough for your needs. Closing this.