Announcement

Collapse
No announcement yet.

Problem with sorting array

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

    Problem with sorting array

    Hi,

    I have (again) a problem with arrays.
    This time its about sorting an array.
    Since there is no sorting functionality for arrays, it needs to be implemented in the graph. Also, since arrays dont have a "swap elements" function, that has to be done in BP.

    As Im only sorting arrays rather small in size, the good old bubble sort will suffice.
    But it doesnt work

    Here is what I did:

    This is the array, the Skills. This function just returns a data structure with some random values:

    Click image for larger version

Name:	MakeStatsFunc.JPG
Views:	1
Size:	87.7 KB
ID:	1171831

    This is how the whole thing is called from the event graph. just putting some text out with the contents of the array (that should be sorted).

    Click image for larger version

Name:	eventgraph.JPG
Views:	1
Size:	93.0 KB
ID:	1171832

    During gameplay, this is what I get:
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.901517
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.709525
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.538652
    LogBlueprintUserMessages: [CustomController_C_2] Development/0.362896
    LogBlueprintUserMessages: [CustomController_C_2] -----------------------------
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.994201
    LogBlueprintUserMessages: [CustomController_C_2] Medical/0.810694
    LogBlueprintUserMessages: [CustomController_C_2] Development/0.328379
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.058626
    LogBlueprintUserMessages: [CustomController_C_2] -----------------------------
    LogBlueprintUserMessages: [CustomController_C_2] Support/0.915036
    LogBlueprintUserMessages: [CustomController_C_2] Support/0.388928
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.627613
    LogBlueprintUserMessages: [CustomController_C_2] Development/0.489578
    LogBlueprintUserMessages: [CustomController_C_2] -----------------------------
    LogBlueprintUserMessages: [CustomController_C_2] Development/0.899564
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.385022
    LogBlueprintUserMessages: [CustomController_C_2] Development/0.536699
    LogBlueprintUserMessages: [CustomController_C_2] Medical/0.239479
    LogBlueprintUserMessages: [CustomController_C_2] -----------------------------
    LogBlueprintUserMessages: [CustomController_C_2] Medical/0.919919
    LogBlueprintUserMessages: [CustomController_C_2] Combat/0.859554
    LogBlueprintUserMessages: [CustomController_C_2] Support/0.396313
    LogBlueprintUserMessages: [CustomController_C_2] Development/0.100284
    LogBlueprintUserMessages: [CustomController_C_2] ------------------------
    So, obviously not only doesnt get the data sorted, but also the data gets messed up. All of a sudden there are 3 Combat, or 2 support skills there.

    The GetBestFit function is suppoesed to handle the bubble sort.. First some initial values are stored local (as Zack Parrish would )
    It should be noted that the Skills input is passed as reference.

    Click image for larger version

Name:	BestFitInitial.JPG
Views:	1
Size:	73.1 KB
ID:	1171834

    Then, I loop through the array until, at the end of the loop, the IsSorted value (which is intialized to true right before the loop) stayed true after the loop completes.
    Well, its bubble sort, you know......

    Click image for larger version

Name:	BestFitOuterLoop.JPG
Views:	1
Size:	68.2 KB
ID:	1171836

    The loop itself looks like this:

    I get some index values and the corresponding data elements. Then I compare the skill value.

    The sorting should be in descending order, so in case The current element skill value is greater than the next element skill value, we do nothing, those two are already sorted.
    Else, the items get swapped and sorted is set to false, which will cause the loop to be repeated util no swap was necessary, which means the array is sorted.

    Click image for larger version

Name:	BetFitIteration.JPG
Views:	1
Size:	142.1 KB
ID:	1171837


    Alas, arrays dont have a swap function, here its is:
    Pretty straight forward. I get the elements and set one for another.
    It should be noted that, again, the function data input is passed as reference.

    Click image for larger version

Name:	SwapElementsFunc.JPG
Views:	1
Size:	114.2 KB
ID:	1171838


    But, as you can see from the log, it just doesnt work

    Is there something wrong with my graph logic
    Am I assigning something wrong somewhere
    Sorting doesnt work, thats one thing, but why does my data gets messed up

    Any help is greatly appreciated

    Cheers,
    Klaus

    #2
    I can't remember where but im pretty sure somebody added in a sort array blueprint node plugin that you can get. Might have to do a bit of digging to find it though.

    Comment


      #3
      Thanks for the suggestion
      But Id rather like to do this without a plugin.
      Once for a learning effect and secondly also not being dependent on someone elses plugins (and their maintenance).

      But I still cant figure out what going wrong

      Comment


        #4
        Try printing what ItemRefA and B are after each set array elem.

        Comment


          #5
          Try printing what ItemRefA and B are after each set array elem.
          Here is what I did:

          The swap function now prints ou the reference data:

          Click image for larger version

Name:	swapdebug.JPG
Views:	1
Size:	118.6 KB
ID:	1100243

          And the MakeStats function now supplies steady values for easier debugging (instead of random floats)
          Those are the test values:

          Click image for larger version

Name:	makestatsdebug.JPG
Views:	1
Size:	72.1 KB
ID:	1100244

          During gameplay, the log now shows:

          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          LogBlueprintUserMessages: Support/0.9
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          LogBlueprintUserMessages: Development/0.7
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          LogBlueprintUserMessages: Combat/0.5
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          LogBlueprintUserMessages: Medical/0.3
          RefA: Combat/0.5
          RefB: Development/0.7
          RefA: Combat/0.5
          RefB: Support/0.9
          RefA: Development/0.7
          RefB: Support/0.9
          LogBlueprintUserMessages: -----------------------------
          Which somehow seems to do the sorting right all of a sudden.

          Is is perhaps possible that the array input of the loop is "polled" from its connected source for each iteration of the foor loop?

          Like:
          Click image for larger version

Name:	eventgraphsketch.jpg
Views:	1
Size:	192.1 KB
ID:	1100247

          Somehow that seems too odd to be true.
          However it would explain why the data got garbled up as for each iteration a complete new random set of stats was created.

          Im gonna test some more on this and then simply call the sorting function right in the MakeStats function so they get emitted already sorted in the first place.

          Still, can somebody shed some light on node behavior?
          When keep nodes their data and when do they refetch it each time they are polled

          Thanks for any help

          Cheers,
          Klaus

          Comment

          Working...
          X