#Creating 8x8 Arrays out of a bigger Array

79 messages · Page 1 of 1 (latest)

grizzled kindle
#

I have created a 720x432 array and need to create an instance for each 8x8 square of the array which will be tiles for my world map. I can't wrack my head around how I can repeat this using a for statement. The idea is to create a tile and an array for that tile. The values stored in the array will be the same as the world map array: world[0][0] to world[7][7] then the next tile will have an array containing the values of the map array world[8][0] to world[15][7] and repeat until all of the 720x432 array values have been converted into 1215 tiles each with an 8x8 array.

rain pollen
#

what value is stored in the 720x432 array

#

and how do you want to store the new arrays?

#
var chunks = {};
var width = 720 div 8;
var height = 432 div 8;
for (var i = 0; i < width; i++) {
  for (var j = 0; j < height; j++) {
    var key = string(i) + "," + string(j);
    chunks[$ key] = array_create(8);
    for (var k = 0; k < 8; k++) {
      chunks[$ key][k] = array_create(8);
      for (var l = 0; l < 8; l++) {
        chunks[$ key][k][l] = world[(i * width) + l][(j * height) + ;];
      }
    }
  }
}```lets see if this worked heh
rain pollen
#

this appears to work yeah

#

this would give you a struct of chunks

#

maybe

#

hmm

grizzled kindle
#

just numbers are stored in the larger array, it is used for height values

#

What I want to do is create an instance and apply 1 of each array to the instance, for example:
newtile = instance_create(tile)
with (newtile) {
/ give that tile an 8x8 array
}

#

then repeat for each8x8 segment, sorry I dont think I explained it well enough in my original post

rain pollen
#

okay this works

#
function array_grid_to_chunks(array, width, height) {
    var chunks = {};
    var columns = array_length(array) div width;
    var rows = array_length(array) div height;
    
    for (var i = 0; i < columns; i++) {
        for (var j = 0; j < rows; j++) {
            var key = string(i) + "," + string(j);
            chunks[$ key] = array_create(width);
            
            for (var k = 0; k < width; k++) {
              chunks[$ key][k] = array_create(height);
              for (var l = 0; l < height; l++) {
                chunks[$ key][k][l] = array[(i * width) + k][(j * height) + l];
              }
            }

        }
    }
    return chunks;
}```
#

so this would return a struct of chunks, which are NxN arrays

#

so your syntax would be like:


var chunks = array_grid_to_chunks(world, 8, 8);

var w = 720 div 8;
var h = 432 div 8;
for (var i = 0; i < w; i++) {
  for (var j = 0; j < h; j++) {
    with (instance_create_depth(etc)) {
      my_array = chunks[$ string(i)+","+string(j)];
    }
  }
}```
#

basically what it does is go through the big array, cut it into NxN squares and store them into chunks in a dict

#

where the chunk coordinate is string key "x,y"

#

so chunk[$ "0,0"] would be the top left corner

grizzled kindle
#

Okay im gunna have to review this a few times to try and understand, ive not used structs before

rain pollen
#

feel free to ask any questions or whatever; easier for me to explain what youre not sure of than it is for me to go thru it line by line haha

#

the gist of it is just slicing the array up. there is no validation here tho, if you give it an array that doesnt divide into the width and height it will totes break

grizzled kindle
rain pollen
#

p much

grizzled kindle
#

okay I think I've got my head around it, 1 more thing though, which might make it more complicated. I want to store that number that I transferred from the original 720x432 array into an 1D array inside the 8x8 array. the reasoning:
the 720x432 array is the world map
each 8x8 array is a tile on the world map
each array inside the 8x8 defines a point on the 8x8 tile and the number I'm currently storing there is just a height value but I will need to store a lot more information there in the future like temperature, population etc.

rain pollen
#

so you have a huge world array (720x432 cells big) and you want to divide that into smaller arrays (8x8 cells big) and you want each cell of the 8x8 array to have a another array on it?

grizzled kindle
#

yeah 😅 and in that last array is where I want to store that data that we originally put in the 8x8 array

#

but, the 720x432 array is only used for its singular usage of storing the height values, the 8x8 isnt stored inside it

rain pollen
#

right

#
function array_grid_to_chunks(array, width, height) {
    var chunks = {};
    var columns = array_length(array) div width;
    var rows = array_length(array) div height;
    
    for (var i = 0; i < columns; i++) {
        for (var j = 0; j < rows; j++) {
            var key = string(i) + "," + string(j);
            chunks[$ key] = array_create(width);
            
            for (var k = 0; k < width; k++) {
              chunks[$ key][k] = array_create(height);
              for (var l = 0; l < height; l++) {
                chunks[$ key][k][l] = [ array[(i * width) + k][(j * height) + l] ];
              }
            }

        }
    }
    return chunks;
}```
grizzled kindle
#

the 8x8 is the segmented sections of that bigger array

rain pollen
#

this will give you a struct where the key is the chunk coordinate 0,0 or 0,1 or 5,6 etc, and each chunk is a 3d array and index 0 on that array is whatever got pulled off the biggest array

grizzled kindle
#

the 720x432 is purely to divide a sprite into colour values and then into a reference number that I use for height

rain pollen
#

yep

#

so if you wanted to get the very top-left thing from the biggest array, but get it in its final location on the 8x8 array it would look like this:

chunks[$ "0,0"][0][0][0];```
#

if you wanted to get the value to the right and bottom of that it would look like:

chunks[$ "0,0"][1][1][0];```
grizzled kindle
#

but if i wanted the [8][0] on the big array it would be [$ "1,0"][0][0][0] ?

rain pollen
#

yes exactly

grizzled kindle
#

sweet

rain pollen
#

if you want to put temperature on the 8,8 you could do

#
array_push(chunks[$ "0,0"][0][0], temperature);```
#

then you would get:

height = chunks[$ "0,0"][0][0][0];
temp = chunks[$ "0,0"][0][0][1];```
grizzled kindle
#

okay sweet I think I get all of that

rain pollen
#

sick

#

personally i would put a struct on the 8x8 instead of another array.gml //this: [$ "0,0"][0][0].height; //looks much better than: [$ "0,0"][0][0][0];

grizzled kindle
#

I agree

#

it would get quite confusing after adding a lot of variables to the mix

#

and it would future proof itself a lot better that way

rain pollen
#

you could do a constructor for your cell data

#
function celldata(height, temperature, clowns) constructor {
  self.height = height;
  self.temperature = temperature;
  self.clowns = clowns;
}```then this line:
```gml
chunks[$ key][k][l] = [ array[(i * width) + k][(j * height) + l] ];```would become this:
```gml
var height = array[(i * width) + k][(j * height) + l]
chunks[$ key][k][l] = new celldata(height, 0, false);```
grizzled kindle
rain pollen
#

no that would be a global function

#

edited it to be constructor proper

grizzled kindle
#

Okay, I'm gunna give all this a go tomorrow night and see how it goes. hopefully with all that I'm pretty much set for that part of my game

#

thanks very much for all your help ill let you know how it goes 😄

rain pollen
#

nice

grizzled kindle
rain pollen
#

yes

grizzled kindle
#

Im currently getting this error, not sure what I've missed

#

Im trying the basic version first to see if I can get that to work and my head around it

rain pollen
#

hmm

grizzled kindle
#

This is in the create event of the map object

var chunks = array_grid_to_chunks(pixels, 8, 8);

var w = 720 div 8;
var h = 432 div 8;
for (var i = 0; i < w; i++) {
for (var j = 0; j < h; j++) {
with (instance_create_depth(o_tile0)) {
array_tile = chunks[$ string(i)+","+string(j)];
}
}
}

#

and this is the script

function array_grid_to_chunks(array, width, height) {
var chunks = {};
var columns = array_length(array) div width;
var rows = array_length(array) div height;

for (var i = 0; i < columns; i++) {
    for (var j = 0; j < rows; j++) {
        var key = string(i) + "," + string(j);
        chunks[$ key] = array_create(width);
        
        for (var k = 0; k < width; k++) {
          chunks[$ key][k] = array_create(height);
          for (var l = 0; l < height; l++) {
            chunks[$ key][k][l] = [ array[(i * width) + k][(j * height) + l] ];
          }
        }

    }
}
return chunks;

}

#

Ah, I think I might know why, the size is off
Nvm its correct

grizzled kindle
#

Okay I realised I had changed the size of the original array but still getting this error once ive fixed it to the right dimensions

rain pollen
#

hmm

grizzled kindle
#

I have a feeling its something like: the array is 208 width but thats 0-207 and its calling upon 208 for whatever reason, but im unsure how not to do that?

#

as im not confident with the process of this

rain pollen
#

yeah its an off by one error

#

you could try subtracting 1 from width/height in that loop

grizzled kindle
#

it still doesnt seem to work so i've had a look at doing something different, here's what I've come up with but its showing an error of out of range again:

#

for (var tile_x = 0; tile_x < _w/8; tile_x++) {
for (var tile_y = 0; tile_y < _y/8; tile_y++) {
with (instance_create_depth(o_tile0)) {
for (var subtile_x = tile_x x8; subtile_x < tile_x x8+8; subtile_x++) {
for (var subtile_y = tile_y x8; subtile_y < tile_y x8+8; subtile_y++) {
tileheights[subtile_x-(tile_x x8)][subtile_y-(tile_y x8)] = pixels[subtile_x][subtile_y]
show_debug_message(tileheights[subtile_x-(tile_x x8)][subtile_y-(tile_y x8)])
}
}
}
}
}

grizzled kindle
#

Creating 8x8 Arrays out of a bigger Array

grizzled kindle
#

Is anyone able to help with this? I feel im so close to a solution but unsure why it keeps saying out of range

rain pollen
#

still not working? i have been using it for some procgen stuff since i wrote it for you the other day and it hasnt broken on me heh

grizzled kindle
#

I couldnt get it to work, so I did my own version of it which I thought was better suited for me, as I dont need to disect the other, I just need an 8x8 array for each tile I create

grizzled kindle
# rain pollen still not working? i have been using it for some procgen stuff since i wrote it ...

Do you know why this is coming up with an error out of range, it seems like it should work?

for (var tile_x = 0; tile_x < _w/8; tile_x++) {
for (var tile_y = 0; tile_y < _y/8; tile_y++) {
with (instance_create_depth(o_tile0)) {
for (var subtile_x = tile_x x8; subtile_x < tile_x x8+8; subtile_x++) {
for (var subtile_y = tile_y x8; subtile_y < tile_y x8+8; subtile_y++) {
tileheights[subtile_x-(tile_x x8)][subtile_y-(tile_y x8)] = pixels[subtile_x][subtile_y]
show_debug_message(tileheights[subtile_x-(tile_x x8)][subtile_y-(tile_y x8)])
}
}
}
}
}