Array from AJAX file sorting issue

0 favourites
  • 6 posts
From the Asset Store
A master sorting Puzzle fun and addictive puzzle game! A challenging yet relaxing game to exercise your brain!
  • Hello! I am a newbie to Construct 3 (though an experienced programmer) and am having an issue with an array that I am filling from an AJAX request. I followed instructions from a tutorial on reading the file and the array (simple 1-dimensional) APPEARS to be okay when I look at the elements. The first red flag is the last data that shows after I set a display field to AJAX.LastData is actually the very first line of the file. I was expecting it to be the last line of the file. Secondly, when I sort the data, the very first element of the array always goes to the END of the array. Also, when I attempt to look at all the other array elements (via clicking on a list that I filled based on the array elements and sending that value to a text field) none of the elements show up in the text field except the very first element (which, as mentioned above, is actually at the END of the array after my attempted sort.

    My spider sense says something is going on weird with my AJAX stuff, especially since I can manually create an array with the same elements and everything works fine. Have been beating my head with this issue for days, wasting precious time, and haven't been able to quite find the answer after countless searches for AJAX file importing help. And I know it's probably a stupidly simple answer. As I mentioned, I am a newbie to C3, usually just buy a couple software Bibles when I am learning something new, but after looking through the online documentation and everything I just haven't been able to find the answer. Will attempt to upload a pic of the situation. Thanks in advance for any kind help you can offer!

  • And sorry - just read the tips (for getting help from the community on the forums) where it suggests uploading the actual program file (instead of just a screenshot), but don't know how to do that (sorry, again I am also a newbie at posting in this forum)...

  • There are many things wrong on your screenshots.

    First, you don't need counters like animal_pos, use loopindex expression instead.

    Why do you have 2 delimiters in the text file - "|" and ";"? You can remove one. Also, I believe most of the issues with sorting and displaying data are caused by invisible characters - line breaks and spaces. You need to use trim() expression. So, if you remove "|" characters, your code should look like this:

    For "n" from 0 to tokencount(AJAX.LastData, ";")-1
     Array set value at loopindex to trim(tokenat(AJAX.LastData, loopindex, ";"))

    Finally, it would be easier to load a JSON file directly into the array. Right-click on Files, choose "Add -> Array". Then you will be able to load this file using a single "Array Set from JSON" action.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So first of all, "dop2000" I very much appreciate you taking the time to reply. That in and of itself is awesome.

    "Many things wrong with my screenshots" - didn't doubt that, hence my post! :) But (as mentioned in my post) I AM a newbie and totally dependent on what I can scrape from a combination of tutorials, community posts and previous non-C3 coding experience.

    Thank you for the "loopindex" suggestion. The tutorial I got this from used a local variable, but loopindex is much more efficient/neat.

    The double delimiters are again a byproduct of a tutorial. It was originally for a 2-dimensional array using the semicolons to separate x/y, but (not being 100% sure of the necessity of both in a 1-dim array situation) I choose to leave both in there. I have used trim functions outside of C3, so good to know it works here - it's definitely a safer way to parse things.

    Thank you most of all for an actual example of good code and not just a suggestion - suggestions are great for people who know all about a tool, but us newbies need a little hand-holding.

    End result? I working sample program for which I am very, very grateful! I will delve into loading the JSON file directly into an array once I purchase I license and have access to those features. Right now I am testing features to make sure Construct 3 is a good fit for what direction I need to go (educational games heavily dependent on data arrays). Seems to have a little bit of a bad rap in some circles as not being a serious engine and mainly used for hobbyists and rapid prototyping instead of professional development. I want to see if this is actually true or if some people are going with bigger names just because "everyone else is doing it".

    Also (since you have been so kind as to respond and save me from another day of head-banging), can you point me to a way in C3 to sort an array that has a mix of words that are lowercase and uppercase (without throwing all the uppercase words to the start of the array)? I've gotten around this in other software by temporarily changing the uppercase words to lowercase and replacing them after the sort (and/or using some ASCII magic), but haven't found a solution yet in my C3 searching. I am 100% sure this has already been covered somewhere...


  • to sort your array you pretty much will have to convert you words to lower case - using lowercase(text).

    what you could do is make your array 2 dimentional where the y=0 is the lowercase version of the word, and y=1 has the unconverted word.

    Then you can sort the array on the x axis.

    Another thing I have done with very large arrays is use a second array to filter and build a key to sort on (when I had to sort based on more than one field from the main array). the index array had the key in the 0 column, and the line number back to the main array in column 1.

  • AllanR, I love both of those suggestions. The first is definitely a simple way to do it (and what I will likely use for immediate testing) and the second suggestion with multiple arrays and keys definitely has merits as well for many scenarios. Thank you so much, definitely (again) saves me so much time. Would gladly buy you guys a Coke/coffee (TIME=MONEY) if you were local to ATL!

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