Arrays : How do I locate to a specific value

0 favourites
  • 5 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • My question is kind of generic.

    I am aware that arrays are not datasets so as to locate to a certain entry, however I am working on a 3D array that contains lots of info and I am in need of finding the position of a specific entry (or more) and then get one of dimentions (all fields from that dimention) for further use...

    So I was wondering if there was a quicker way to get to the info I need than the one I've currently implemented.

    What I currently do.

    1. I check if the value (ie.: "name") I am looking for is contained in the array.

    2. I loop twice each dimention (Height,Depth)

       and when "name"=Myarray.At(x,Height++,Depth++)

       get the info I need for all x fields for each Height and Depth found to be matching.

    As far as I am concerned this works fine and there is no delay, however i keep wondering if there is some smarter way to do it.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You might want to fiddle with Array.indexOf, it returns the X-index of a given value (or -1 if not found). But the documentation isn't really clear on how it behaves with 3-D arrays.

  • Thx but in any case this gives the first instance found and LastIndexof gets the first from reverse (or last...). But if I have 3 values that correspond to "name", I am missing one, this way.

  • Oh, so you can have multiple identical entries ? How about a function that return a string with all matching index concatanated with a separator ?

  • Now i'm loading results to a secondary temporary array and I'm quite happy with how this works, my only issue that currently I'm looping through the other two dimentions to get this done (just like i would in js probably).

    My array is a comprehensive Tech tree for the game I'm making

    1 dimention = [Tech Level],[Tech Level Title],[Tech Level subtext]

    2 dimention = [Type of Technology],[Type SubText]

    3 dimention = [id],[Name of Tech],[Subtype],[Description],[Research Points],.... n=25

    So I'm getting all information of dimention 3 where ie [Tech level]<= 4 and [Type of Technology] = Fleet and then load Dropdownlists and a temprary array for the click options. Concatenating [Tech Level subtext],[Type SubText],[Description] provides the full description of the item.

    I just didn't like how i made it... I always think there is a better way to do things. :)

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