How can I stop navigating NPC while they are already going to the point?

Hello!
Im making CustomNPC to run to some point and if they see enemy , or hit by enemy, I want to make them Attack enemy.Im using verse because I want them to run to some point.
Even if I use Navigatable.StopNavigation() They dont stop until they reach the point.
I attached trash code to test navigation.

Please tell me if we can stop NPC while they are navigated and running to point.

using { /Fortnite.com/AI }
using { /Fortnite.com/Characters }
using { /Verse.org/Colors }
using { /Verse.org/Random }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Fortnite.com/Devices }

NPCBehavior<public> := class(npc_behavior):

    @editable Flag1 : creative_prop = creative_prop{}
    @editable Flag2 : creative_prop = creative_prop{}

    @editable Button : button_device = button_device{}

    var LastFlag : int = 2
    var IsNPCAttack : logic = false

    OnBegin<override>()<suspends>:void =
        Button.InteractedWithEvent.Subscribe(ButtonPushed)

        if:
            Agent := GetAgent[]
            Character := Agent.GetFortCharacter[]
            Navigatable := Character.GetNavigatable[]
            Focus := Character.GetFocusInterface[]

        then:

            loop:
                Flag1Location := Flag1.GetTransform().Translation
                Flag2Location := Flag2.GetTransform().Translation
                NavTargetFlag1 := MakeNavigationTarget(Flag1Location)
                NavTargetFlag2 := MakeNavigationTarget(Flag2Location)
                
                if(LastFlag = 2):
                    Print("Going to flag1")
                    NavResultGoTo := Navigatable.NavigateTo(NavTargetFlag1, ?MovementType:=movement_types.Running)
                    set LastFlag = 1
                    
                if(LastFlag = 1):
                    Print("Going to flag2")
                    NavResultGoTo := Navigatable.NavigateTo(NavTargetFlag2, ?MovementType:=movement_types.Running)
                    Print("Reach")
                    set LastFlag = 2
                if(IsNPCAttack = true):
                    Navigatable.StopNavigation()
                    Sleep(5.0)


    ButtonPushed(Agent:agent):void=
        Print("ButtonPush")
        set IsNPCAttack = true

Thank you in advance!

The NavigateTo function internally has its own loop, so any time you call NavigateTo, the code will run until the Navigation_result is returned, at which point the navigateTo function stops running. What you need to code is a race that would be something like :

race :
–The navigate to function succeeding
–and the perception trigger that interrupts it

That way, the code will try to run both until only the first one succeeds and the other is interrupted and cancelled. If you are wanting to test it on a button, you can Await the button’s ‘Interacted with event’. So the code would be something like:

Here is a better example of the indented race code syntax:

1 Like

Thank you very muchh
Now I know how to use race: !

1 Like

awesome :smiley: also, you’ll need to exit out of the loop after the button has been pressed, so you can add the ‘return’ keyword on its own line at the end of the block awaiting the button’s event :slight_smile: Hopefully this works for you, let me know if you still need any help :slight_smile: Cheers

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.