NebulaGames

C++ to Blueprints Distance Sort

Aug 12th, 2018
1,948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. USTRUCT(BlueprintType)      // Define custom struct and expose to blueprints
  2. struct FActor_Dist          // This will be the name of the new struct in BPs
  3. {
  4.  
  5.     GENERATED_USTRUCT_BODY()
  6.  
  7. public:
  8.  
  9.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Actor Distance")    // allow this property to be edited in BPs
  10.     float distance = 0;
  11.  
  12.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Actor Distance")    // allow this property to be edited in BPs
  13.     AActor* actor = NULL;  
  14.    
  15. };
  16.  
  17. -----------------------------------------------------------------------------------------------------------------------------------------------------------
  18.  
  19.    
  20. public:
  21.  
  22. UFUNCTION(BlueprintCallable, Category = "Sorting Functions")    // Sort actors by distance from target actor   
  23.         static void  Distance_Sort(UPARAM() TArray <AActor*> Array_To_Sort, UPARAM()AActor* From_Actor, bool Descending, TArray <FActor_Dist> &Sorted_Array);
  24.  
  25.  
  26.  
  27. ------------------------------------------------------------------------------------------------------------------------------------------------------------
  28.  
  29.  
  30. //Pass a copy of the actor array to sort and the "central" actor to measure distance from, output a reference to the sorted struct array created in the function
  31.  
  32. void  UMySortFunction::Distance_Sort(UPARAM() TArray <AActor*> Array_To_Sort, UPARAM()AActor* From_Actor, bool Descending, TArray <FActor_Dist> &Sorted_Array)
  33. {
  34.  
  35.     if (Array_To_Sort.Num() > 0)            // check that input array is populated
  36.     {
  37.         TArray <FActor_Dist> Combined_Data; // define temporary array of custom struct variables
  38.         FActor_Dist CurActor;
  39.         FVector location1 = From_Actor->GetActorLocation(); // get the world location of the "central" actor distance will be measured from
  40.         float length;
  41.  
  42.  
  43.         for (int x = 0; x < Array_To_Sort.Num(); ++x)       // loop through all actors in array
  44.         {
  45.             FVector location2 = Array_To_Sort[x]->GetActorLocation();   // get world location of actor in array
  46.             length = (location1 - location2).Size();                    // get distance between "central" actor and this actor from the array
  47.             CurActor.distance = length;                                 // set the custom struct variable for distance
  48.             CurActor.actor = Array_To_Sort[x];                          // set the custom struct actor
  49.  
  50.             Combined_Data.Add(CurActor);                                // add the newly created struct to the temporary array
  51.         }
  52.  
  53.         for (FActor_Dist x : Combined_Data)                             // using the distance value of each struct order the actors based on distance from "central" actor
  54.         {
  55.             int32 m = Combined_Data.Num();                              // run basic bubble sort algorithm
  56.             int32 a, k;
  57.             bool bDidSwap;
  58.  
  59.             for (a = 0; a < (m - 1); a++)
  60.             {
  61.                 bDidSwap = false;
  62.  
  63.                 if (Descending == true)                                 // sort high to low
  64.                 {
  65.                     for (k = 0; k < m - a - 1; k++)
  66.                     {
  67.                         if (Combined_Data[k].distance < Combined_Data[k + 1].distance)
  68.                         {
  69.                             FActor_Dist z;
  70.                             z = Combined_Data[k];
  71.                             Combined_Data[k] = Combined_Data[k + 1];
  72.                             Combined_Data[k + 1] = z;
  73.                             bDidSwap = true;
  74.                         }
  75.                     }
  76.  
  77.                     if (bDidSwap == false)
  78.                     {
  79.                         break;
  80.                     }
  81.                 }
  82.                 else                                                    // sort low to high
  83.                 {
  84.                     for (k = 0; k < m - a - 1; k++)
  85.                     {
  86.                         if (Combined_Data[k].distance > Combined_Data[k + 1].distance)
  87.                         {
  88.                             FActor_Dist z;
  89.                             z = Combined_Data[k];
  90.                             Combined_Data[k] = Combined_Data[k + 1];
  91.                             Combined_Data[k + 1] = z;
  92.                             bDidSwap = true;
  93.                         }
  94.                     }
  95.  
  96.                     if (bDidSwap == false)
  97.                     {
  98.                         break;
  99.                     }
  100.                 }
  101.             }
  102.  
  103.             Sorted_Array = Combined_Data;                               // set output struct array to sorted temporary array
  104.  
  105.         }
  106.     }
  107.  
  108.     else                                                                // if no elements in array exit function
  109.         ;
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment