Initialize an Array

Get help using Construct 2

Post » Wed Oct 08, 2014 1:24 am

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! (:
B
4
Posts: 3
Reputation: 206

Post » Wed Oct 08, 2014 6:01 am

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.
Image
B
58
S
13
G
6
Posts: 271
Reputation: 12,128

Post » Wed Oct 08, 2014 6:31 am

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!
B
4
Posts: 3
Reputation: 206

Post » Wed Oct 08, 2014 6:45 am

pokeapache wrote: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...
JustAnotherStupidExampleLol.capx


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)
You do not have the required permissions to view the files attached to this post.
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,718

Post » Wed Oct 08, 2014 3:21 pm

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
Code: Select all
myarray = [ "test", "more testing" ];
, 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.
B
4
Posts: 3
Reputation: 206

Post » Wed Oct 08, 2014 6:28 pm

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.
You do not have the required permissions to view the files attached to this post.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198

Post » Wed Oct 08, 2014 7:18 pm

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

Was about to use regex, but that got a little complicated - lol.
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,718

Post » Wed Oct 08, 2014 7:43 pm

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.
B
74
S
31
G
26
Posts: 994
Reputation: 20,198


Return to How do I....?

Who is online

Users browsing this forum: YoHoho and 11 guests