[SOLVED] make an array without knowing the required size?

Get help using Construct 2

Post » Wed May 20, 2015 8:18 am

I want to load data into an array but the amount of data depends on the mysql database and that amount of data can grow and shrink.

I am placing little flags on a city map and have 4 bits of data coming in per flag. (id, color, x-coordinate and y-coordinate)
The number of flags will change constantly though because users can add and remove them.
So every time the map loads it gets the current flags (4 bits of data each) from the mysql database.
I want to have a 2 dimentional array to load the data into and use a row per flag. So position 1.0, 1.1, 1.2 and 1.3 are the first flag.
Then 2.0, 2.1, 2.2, and 2.3 for the next etc..
But I don't know how many flags will be loaded so i cannot beforehand create the array since I don't know the size it has to be yet.

So is there a way that i can either..

-Check the number of bits of data that come in through AJAX.LastData (divided by a separator). And then quickly create the array in the right size and load the data into the array?

OR

-Create a 4x1 array and increase the Y size of the array on the fly while loading data into it?
Last edited by UberDark on Wed May 20, 2015 8:48 am, edited 2 times in total.
B
54
S
7
G
8
Posts: 150
Reputation: 5,817

Post » Wed May 20, 2015 8:35 am

- Check the number of bits of data that come in through AJAX.LastData (divided by a separator). And then quickly create the array in the right size and load the data into the array?

This would be pretty easy using TokenCount

tokencount(src, separator)
Count how many tokens occur in src using separator. For example, tokencount("apples|oranges|bananas", "|") returns 3.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Wed May 20, 2015 8:39 am

Another way would be to push the desired info into the array..

Push
Add a new value either to the beginning (front) or end (back) of an axis. Since the Array is a 3D cube of values, technically this inserts a new 2D plane of elements all with the given value. However in 1D arrays this adds a single element, and in 2D arrays it inserts a new row of elements.
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Wed May 20, 2015 8:41 am

Tokencount? Awesome!
And then I just use the 'set size' and that will solve my problem yeeeeuy
CHEERS @LittleStain
B
54
S
7
G
8
Posts: 150
Reputation: 5,817


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, dop2000, tarek2 and 46 guests