luxkun / ReGoap

Generic C# GOAP (Goal Oriented Action Planning) library with Unity3d examples
Apache License 2.0
1.02k stars 149 forks source link

How can you re-evaluate goals? #37

Closed cckelly closed 3 years ago

cckelly commented 4 years ago

I have a goal that has the requirement of "killPlayer", which moves an enemy towards the player and once we are close enough, fires off a shoot player action which has the effect "killPlayer". Now, the player doesn't die most of the time so I basically want this goal re-ran (get player, move towards player, shoot player OR if close enough, just shoot player), how can I do this? Or if this is not the proper way to get this behavior, what am I missing?

Thanks in advance

dithyrambs commented 4 years ago

So the Planner should re-evaluate which goal to run if a plan fails--which fails if an action fails. So you only need to worry about failing your ShootPlayerAction, and the planner should take care of the rest theoretically, if things are set up correctly.

ReGoapAction's Run method is run every frame, and provides a 'done' or 'fail' callback which you would call accordingly within the method, depending on some test.

   if (  SomeTestForSuccess() )
        {
            done(this);
        }
        else
        {
            fail(this);
        }

Upon failure or success, and if the conditions for the goal are met and it is re-ran, the planner should choose a goal and then actions to accomplish it. So I think ideally, you would break up your current action into TWO actions: eg. GetInShootingRangeAction and ShootPlayerAction. The planner would decide if the plan need the GetInShootingRangeAction or not. If the NPC is at the desired destination, the planner should only need the ShootPlayerAction. (And of course, the planner knows this because it checks the Action's preconditions in the GetPreconditions and CheckProceduralPreconditions methods).

Some resources:

I was pretty confused about how to implement actions/goals until I started reading the source code of FEAR, which is publicly available now. Additionally, assuming you've read Jeff Orkin's many white papers on GOAP (you should definitely read them all if you haven't), I still found that he skipped over a lot of the details, but there are some very useful academic papers on GOAP that do a much better job of explaining the system.

Good luck!

cckelly commented 4 years ago

@dithyrambs extremely helpful thank you! I think the piece I was missing is I'm not failing actions - assuming the test for my ShootPlayerAction would be checking if the player is still alive. Ah yes breaking up into two actions makes sense, because there could be two plans depending on world state. One being the NPC getting in range to shoot and then shooting, the other being already in range and can just run the ShootPlayerAction. I can use a precondition of "nearPlayer" either with a bool value or a float that represents how close the NPC is to the player.

Yeah I just started reading and learning on it this weekend, definitely need some more resources to get up to speed with. Thanks again!

zukas3 commented 3 years ago

@dithyrambs ReGoapAction's Run method is run every frame, and provides a 'done' or 'fail' callback which you would call accordingly within the method, depending on some test.

Doesn't seem to be true, I couldn't trace the code to find where it would be running on every frame. It seems that even an example GenericGoToAction.cs relies on delegates to inform whether the action has been successful rather than ticking it.