How do I Load Data Into An Array

Get help using Construct 2

Post » Thu May 15, 2014 9:40 pm

Hey there,
Do I use "On Start of Layout" and then fill it in with events? Or is there some other way? Like a text file?

Thank You
B
56
S
15
G
13
Posts: 826
Reputation: 17,645

Post » Thu May 15, 2014 9:46 pm

Tekniko wrote:Hey there,
Do I use "On Start of Layout" and then fill it in with events? Or is there some other way? Like a text file?

Thank You


You can do the on start of layout + events, however, it is not the best technique, the best is I think to load a JSON file (which can be a project file) into it when it is created.

I do not remember the exact syntax though
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Thu May 15, 2014 10:23 pm

Ahh, haven't touched Json yet. I'll hit the manual and see where I end up with that. Thanks!
B
56
S
15
G
13
Posts: 826
Reputation: 17,645

Post » Thu May 15, 2014 11:45 pm

Code: Select all
{"c2array":true,
"size" :[2,5,1],
"data":[
[["(0,0)"],["(0,1)"],["(0,2)"],["(0,3)"],["(0,4)"]],
[["(1,0)"],["(1,1)"],["(1,2)"],["(1,3)"],["(1,4)"]]
]
}


should return an array like this:

Visual representation of the array wrote:"(0,0)" | "(1,0)″
"(0,1)" | "(1,1)"
"(0,2)" | "(1,2)"
"(0,3)" | "(1,3)"
"(0,4)" | "(1,4)"


Hope that helps (I do not know about a 3D array though)
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Fri May 16, 2014 12:38 am

@Aphrodite Where would that code even go?
B
56
S
15
G
13
Posts: 826
Reputation: 17,645

Post » Fri May 16, 2014 12:48 am

Tekniko wrote:@Aphrodite Where would that code even go?


The "proper way" to do it would be to use a project file (which is actually just a text file containing that code, which is actually a C2 array in a JSON format), to call it using the AJAX object, and to load the Ajax request result inside the array using the array's action "Load".

https://www.scirra.com/manual/108/array
https://www.scirra.com/manual/107/ajax
https://www.scirra.com/manual/141/files

However it might be a little too much to worry about if you don't know yet how to do all this.
I suggest you go by setting each value with a "set at X/XY/XYZ" for now, just remember that if your array is not large enough you could have issues(a value outside an array is always 0).
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Sun May 18, 2014 9:14 am

Hi. I'm also struggling to get data from a text file into an array, so if anybody can enlighten me.
I have a list of 500 Words in a TXT file. looking like this:
and
when
sell
up
to
five
hiundred

Should it be formatted otherwise?

I want an array, from which I can select a random word.

Anybody?
B
5
S
1
Posts: 23
Reputation: 471

Post » Sun May 18, 2014 11:06 am

gplar wrote:Hi. I'm also struggling to get data from a text file into an array, so if anybody can enlighten me.
I have a list of 500 Words in a TXT file. looking like this:


Should it be formatted otherwise?

I want an array, from which I can select a random word.

Anybody?


I think for a list that simple, you could do something like:

Code: Select all
and|when|sell|up|to|five|hiundred


And retrieve one of them with the tokenat expression: https://www.scirra.com/manual/126/system-expressions

With the parameter index equals to int(random(number of elements))
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Sun May 18, 2014 3:23 pm

Thanks, that's quick! :-)
Still I'd like to import the list and make an array, as this is the first app I'm converting to Windows, (I used Stencyl to make it for iOs, but they don't support HTML5 or Windows touchscreens.) and down the road I'll need multidim arrays for other apps.
I feel a little lost, as I know what I want to do, but can't quite figure out where to find the solutions, yet. :-)
(I used a list object , which is one of the nice aspects of Stencyl, for this.)
B
5
S
1
Posts: 23
Reputation: 471

Post » Sun May 18, 2014 5:44 pm

@gplar : I've made a little function (using C2's Function Object) that can take your list
and
when
sell
up
to
five
hiundred

into a one dimensionnal array:

Image
First it clears the array, then it size it accordingly to the number of values, then it enters the values inside it, you may wonder "how do I concretely use that thing", well, like this:
Image

The first parameter is the list itself (I put it directly, but it can be a separate project file), the second parameter is the characters between each elements of the list (here it is newline, because each part of the list is separated by a new line)

The debugger shows that at the end, the array has the correct values after using it.
Image

Hope that helps
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Next

Return to How do I....?

Who is online

Users browsing this forum: jorgmaquoi, Yahoo [Bot] and 23 guests