How do I filter an array for entries that don't exceed a certain character count?

0 favourites
  • 7 posts
From the Asset Store
A minimal theme studied and built for both professional & new entries
  • Hello friends,

    I have a spelling game where the words are being drawn from an array, and the words vary in length from 3-8 characters. I would like to create two different modes - normal and easy, based on character counts - without creating two separate projects or arrays.

    dropbox.com/s/5bbsm5gveebmm0a/spell.c3p

    I stripped out the pictures just to make it smaller. I've included a text box with the target word for testing purposes.

    I am imagining that I could invoke a mode using a variable, that reloads the array and filters entries based on character length. I would like to have a "normal" mode that uses all entries, and an easy mode that uses only entries up to 4 characters long. I would provide the user with a couple icons for normal and easy, and they could switch between the two modes. It would just invoke the filtering.

    I would much prefer to stay with the same one array for both modes, filtering the same contents rather than creating additional arrays.

    I tried to set a len condition, but I don't understand how to tell the system to only use array entries that fit a certain criteria.

    Currently the letter blocks are baked into the layout. I would much prefer if the blocks were created dynamically, the same way that the slots are. Ideally there would be two options for how to handle it, either backend options for me for how to configure things for export, and/or user-selectable options during game play:

    1) The number of letter blocks matches the character count of the target word, and...

    2) I explicitly declare the number of letter blocks

    I have now spent more than six hours trying to do only this, without success. I know how to make the boxes and do the offset, but the resulting boxes don't get tied into the rest of the mechanic properly.

    Thanks very much for any guidance. Much appreciated.

  • Assuming you load a file into the array, you would create a different file (list of possible words) for each mode. So the filtering process you describe is done already with the file.

  • Thanks for the reply.

    I only want to have one list. There is one list with hundreds of words, and I would like to have settings for 3 letters, 4 letters, 5 letters, etc. up to 10 letters eventually, so using separate lists would be very inefficient and time-consuming and complicated for me to manage. Plus the default state of the game is to draw entries from the entire list, utilizing all lengths of words.

    Is it not possible to filter an array based on character count, using len or some other method? If so, I will abandon this idea.

    If this kind of filtering is possible, it opens many possibilities for me, like presenting only the words that begin with a certain letter. It's also relevant to many other educational activities that I've been creating, so I'm very keen to understand how array filtering works, if it is possible. Thanks.

  • Sure but you didn't say that in the post, you said there are two modes only, a normal mode that contains all the words and another easier mode which has words up to 4 letters. For this you can easily create two files to load.

    If you're now saying that you need to draw certain words from the list and customise it then yes you can run through each value and compare len to a variable. If I was doing this I would still use files because it sounds like a game where you know the required list of possible words before a mode begins so therefore you can create lists and load them.

  • Yes, I want to run through the values, compare them to len, and use only certain ones, but I have not been able to figure out how to make this work in my project. I only want to have one list. Many thanks for any assistance.

  • Can’t open the project, but filtering can be done by looping over each entry in the array, checking its length and removing it if needed. The only catch is you’ll want to loop over it in reverse.

    Array: for each x
    Compare: len(array.at(array.width-1-array.curx) >3
    — array: remove at array.width-1-array.curx
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the reply! Sorry you couldn't download it. I thought I stripped out all 3rd party elements, not sure what else the problem might be. I will try to implement your advice. I'm not sure if the reverse part is reflected in your code sample. I wouldn't necessarily know how to do that otherwise, but I will work with it in any case. Thanks.

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