Initialize an Array

0 favourites
  • 8 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Hey guys,

    Is there a way to either load a string from a .txt (or other file format) into an array, without having to insert a bunch of events at start up?

    Basically, can I load multiple values for an array at once, preferably through a file or just one input? The array won't change at all during the game, so it would just be static so I can pull the values from it later in the game.

    (I've tried using the "Load" function for the array, but it either doesn't work or yells at me for syntax errors, so...)

    Any help would be appreciated, thanks! (:

  • load function only accepts previously saved arrays in json format, so you might have to populate the array manualy once, then save it as json file, and then use that file to load.

  • Okay, so I got it to work with "Load from JSON string", but, why is it like this? You need DOUBLE QUOTES, like five hundred brackets??? I just. Thank you for the help, but this is absurd..

    ( for anyone looking for how to get as close to an array literal as you can get in this software:

    System -> On start of layout (or whatever proc you want)

    -> Array -> Load from JSON string "{""c2array"":true,""size"":[10,1,1],""data"":[[[""words""]],[[""words""]],[[""words""]],[[""magic""]],[[5]],[[""numbers work okay without quotes""]],[[1]],[[2]],[[3]],[[4]]]}"

    Seriously though, why does it have all these unnecessary complications? Anyway, you can populate an array like this, if you do the quotes and brackets right, just make sure you do it just so, it'll yell at you if you don't, or just not work.

    I still haven't figured out how to load an actual file into the array, but that's fine, this works. It's all in one place which is good enough.

    But yeah, thanks, got it working without having to manually add individual values to the array and save and load and save and load and such. Cheers!

  • Okay, so I got it to work with "Load from JSON string", but, why is it like this? You need DOUBLE QUOTES, like five hundred brackets??? I just. Thank you for the help, but this is absurd..

    ( for anyone looking for how to get as close to an array literal as you can get in this software:

    System -> On start of layout (or whatever proc you want)

    -> Array -> Load from JSON string "{""c2array"":true,""size"":[10,1,1],""data"":[[[""words""]],[[""words""]],[[""words""]],[[""magic""]],[[5]],[[""numbers work okay without quotes""]],[[1]],[[2]],[[3]],[[4]]]}"

    Seriously though, why does it have all these unnecessary complications? Anyway, you can populate an array like this, if you do the quotes and brackets right, just make sure you do it just so, it'll yell at you if you don't, or just not work.

    I still haven't figured out how to load an actual file into the array, but that's fine, this works. It's all in one place which is good enough.

    But yeah, thanks, got it working without having to manually add individual values to the array and save and load and save and load and such. Cheers!

    Did you check the tutorial section? Did you read the manual?

    There are multiple ways to do this, you can use a text file, a ini file, you can write a single array entry, you can even have a variable = "Words, words, magic, 5, words etc".

    So many ways - this software is brilliant.

    Suggest you add files to your project files,

    Here is a tutorialI did awhile ago.

    It wasn't written to answer you question, but it does what you need to do.

    And here is really simple example...if data isn't going to change, this demonstrates another way to do it...

    [attachment=0:o06o51m3][/attachment:o06o51m3]

    You don't even need to use the array in above example.

    Just put into variable = "words|Words|words|magic|5|19|etc|"

    And use tokenat(text,index,seperator)

    so tokenat(variable,3,"|") will give you magic as index is 0words,1Words,2words,3(magic)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for putting in the effort for that example, but... that's pretty much what we were already doing, just loading it with AJAX rather than putting in the JSON manually. And yeah, we've seen the tokenat() method, but that's still not exactly great. The hope was for a way to just initialize an array with some values with something like an array literal in other languages (so, for example, something like

    myarray = [ "test", "more testing" ];[/code:8f6w8ssa], or the equivalent as a single construct2 action), rather than either inserting every value individually with a separate action (as recommended by the documentation) or having to use funky string-splitting (as recommended by the fora) or abusing the serialization/deserialization mechanism (what we ended up doing).
    
    So, thanks, but your solution is really not what we were looking for, and at this point we were more commenting that it's odd not to have a way to initialize an array with multiple values easily than looking for more help.
  • What you can do is use the Function object to do something similar. It's not quite a "one line" initialization but it's as close as you can get.

  • Magistross, you never cease to amaze. Nice use of functions.

    Was about to use regex, but that got a little complicated - lol.

  • To be fair, Regex would probably be the route I'd take to enable multi-dimensional "one-line" initialization. But as pokeapache said, that's just a silly amount of work just for the instantiation of a simple object.

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