Announcement

Collapse
No announcement yet.

C++ AddDynamic

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    C++ AddDynamic

    Hello,
    I am trying to use OnPerceptionUpdated.AddDynamic to update when a pawn is detected, But it wont let me unless i make the array in the function const and with a reference.
    the problem is that if i make it that way it wont let me loop on the detected pawns.
    If anyone can help me solve this ill be grateful.

    The function declaration in the header
    Code:
    UFUNCTION()
        void OnPawnDetected(TArray<AActor*> DetectedPawns);
    The cpp add dynamic
    Code:
    PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AAI_Zombie_Controller::OnPawnDetected);
    The OnPawnDetected function
    Code:
    void AAI_Zombie_Controller::OnPawnDetected(TArray<AActor*> DetectedPawns)
    {
        for (size_t i = 0; i < DetectedPawns.Num; i++)
        {
            DistanceToPlayer = GetPawn()->GetDistanceTo(DetectedPawns[i]);
        }
        bIsPlayerDetected = true;
    }
    Last edited by Angleforce; 11-07-2019, 05:18 AM.

    #2
    The delegate signature for OnPerceptionUpdated is:

    Code:
    DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPerceptionUpdatedDelegate, const TArray<AActor*>&, UpdatedActors);
    So it can only be bound to a function that accepts a const ref to an array of AActor*. There's no way around that part.

    But GetDistanceTo accepts a const AActor*, so there should really not be an issue here.

    You can also cast away the constness using a const_cast but that's very rarely the right solution.

    Comment


      #3
      Originally posted by RotemS View Post
      The delegate signature for OnPerceptionUpdated is:

      Code:
      DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPerceptionUpdatedDelegate, const TArray<AActor*>&, UpdatedActors);
      So it can only be bound to a function that accepts a const ref to an array of AActor*. There's no way around that part.

      But GetDistanceTo accepts a const AActor*, so there should really not be an issue here.

      You can also cast away the constness using a const_cast but that's very rarely the right solution.
      Im getting this eror in the loop:
      Severity Code Description Project File Line Suppression State
      Error C2446 '<': no conversion from 'int (__cdecl TArray<AActor *,FDefaultAllocator>::* )(void) const' to 'size_t'.

      how can i fix this?

      Comment


        #4
        This has nothing to do with the previous issue. You simply forgot the () after .Num. it should be DetectedPawns.Num()

        Comment


          #5
          Originally posted by RotemS View Post
          This has nothing to do with the previous issue. You simply forgot the () after .Num. it should be DetectedPawns.Num()
          Thanks you were right

          Comment

          Working...
          X