How do I defragment my array?

0 favourites
  • 2 posts
From the Asset Store
Array Editor
$0.25 USD
95% off
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • I have an intentional "fragmented" array where some elements are at unpredictable index positions. What I need to do is to only keep elements (string in this case) consecutively in index positions (0,1,2...n) thus eliminating any "holes" in between in the array and then reducing the Width (size or len) or array to just those elements that are not empty (strings, and not zero in this case).

    The code is shared in the attached image.

    What I want is something like (in C-like pseudocode):

    for (int i=0; i<Array1.width)

    if Array1 == 0


    But the problem is, where I added a sub-event under for-loop to check if Array1.At(LoopIndex("i")) it removes SOME empty elements but not all at once. If I repeatedly press the Defrag Array button, it eventually removes all zero/empty ones...but I need it to be done in one loop in one step.

    For example: If fragmented array was "0A0C0000D0"

    and I call Degrag function in my code, it turns it into "AC00D" (leaving two zeros still...where only ACD must be retained after degragmenting)

    After I press "Defrag Array" two more times, it correctly comes to the clean array: "ACD" with Width=3

    Which makes me suspect that I did not place the check if Array1.At(LoopIndex("i")) correctly to check each element within the for-loop in one fell swoop. This is in function DefragArray.

    Thank you in advance.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The trick is to process the array in reverse order - from (Array.width-1) to 0

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)