How to read a 3D array from text file?

M

meme

Guest
Hey guys, this seems to be more complicated than the title, but here is the idea in steps:
1. I'm making a game that creates levels on its own, but to do this I need to create room chunks/templates and then "glue" it up in a final level room (e.g. chunk is 100x100, final level consists of 10 chunks because its size is 1000x100).

2. I created a python program that externally extracts the chunks into multiple (x, y, layer, obj) objects, and then stores it inside a 3D array. This array consists of multiple chunks, each consists of multiple objects, each consists of (x, y, layer, obj). So the total dimension is 3.

3. This array is then written into "output.txt" file such as this:
1612265526395.png

Question is how do I convert this 3D array into GameMaker? My initial thought is to just copy paste the entire output.txt content into GameMaker, but I don't know whether this is a reliable solution. And yes I'm aware that im using plain array instead of proper data structures.

Thanks for reading, if u have any suggestion please let me know.
 

Tyg

Member
You just need some kind of reader script for imports the format you have and store it in a list
ill see if i can whip one up for you :)

can you put your data in this format
[12,16,"post1",objLight]
[10,45,"post2",objCar]
[17,37,"post3",objPlane]
[126,16,"post4",objWoman]
[0,116,"post5",objGlass]
Basically remove the semicolon from the end and on line 2 is it array[[],[]] or array[] for each ?

this function will read the file, i havent finished with the decoding function yet that decodes the string
put the text file is in the datafiles folder

function Load_Chunk_File(argument0)
{
var filename = argument0;
var file = file_text_open_read(filename);
if file == -1{show_debug_message("*FAILED TO OPEN TEXTFILE" + string(filename)); return -1;}
global.CHUNKS = ds_list_create();

while !file_text_eof(file)
{ iC = file_text_readln(file);
// ds_list_add(global.CHUNKS, decode_chunk(iC)); // Commented out until i finish the decode function
}
file_text_close(file);
show_debug_message("FILE READ OK " + string(filename));
return;
}
 
Last edited:

chamaeleon

Member
Or use json_parse and get actual arrays instead of ds_lists (with the same caveat of having removed the semicolon, although a test seems to indicate the parser didn't care about a trailing semicolon).
GML:
var s = "[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]";
var a = json_parse(s);
show_debug_message(a);
show_debug_message(a[0][1][0]);
Code:
[ [ [ 1,2 ],[ 3,4 ] ],[ [ 5,6 ],[ 7,8 ] ] ]
3
 
Last edited:
M

meme

Guest
You just need some kind of reader script for imports the format you have and store it in a list
ill see if i can whip one up for you :)

can you put your data in this format
[12,16,"post1",objLight]
[10,45,"post2",objCar]
[17,37,"post3",objPlane]
[126,16,"post4",objWoman]
[0,116,"post5",objGlass]
Basically remove the semicolon from the end and on line 2 is it array[[],[]] or array[] for each ?

this function will read the file, i havent finished with the decoding function yet that decodes the string
put the text file is in the datafiles folder

function Load_Chunk_File(argument0)
{
var filename = argument0;
var file = file_text_open_read(filename);
if file == -1{show_debug_message("*FAILED TO OPEN TEXTFILE" + string(filename)); return -1;}
global.CHUNKS = ds_list_create();

while !file_text_eof(file)
{ iC = file_text_readln(file);
// ds_list_add(global.CHUNKS, decode_chunk(iC)); // Commented out until i finish the decode function
}
file_text_close(file);
show_debug_message("FILE READ OK " + string(filename));
return;
}
Wow ty for the insight, ^^
 
M

meme

Guest
Or use json_parse and get actual arrays instead of ds_lists (with the same caveat of having removed the semicolon, although a test seems to indicate the parser didn't care about a trailing semicolon).
GML:
var s = "[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]";
var a = json_parse(s);
show_debug_message(a);
show_debug_message(a[0][1][0]);
Code:
[ [ [ 1,2 ],[ 3,4 ] ],[ [ 5,6 ],[ 7,8 ] ] ]
3
It seems there is no json_parse function, where can I get that?
 
M

meme

Guest
Thanks guys, I managed to make it work :D

GML:
// Read the chunk info
var _buffer = buffer_load(working_directory + "\output.txt");
global.chunkCollection = json_parse(buffer_read(_buffer, buffer_string));
buffer_delete(_buffer);
 

Tyg

Member
function Load_Chunk_File(argument0)
{
var filename = argument0;
var file = file_text_open_read(filename);
if file == -1{show_debug_message("*FAILED TO OPEN TEXTFILE" + string(filename)); return -1;}
global.CHUNKS = ds_list_create();
iC = file_text_readln(file);

while !file_text_eof(file)
{
outChunk = decode_chunk(iC);
ds_list_add(global.CHUNKS, outChunk);
iC = file_text_readln(file);
}
file_text_close(file);
show_debug_message("FILE READ OK " + string(filename));
return;
}

function decode_chunk(argument0) {
var inChunk = argument0;

limit = string_pos(",", inChunk);
X = real(string_copy(inChunk,2,limit-2));
inChunk = string_delete(inChunk,1,limit);
limit = string_pos(",", inChunk);
Y = real(string_copy(inChunk,1,limit-1));
inChunk = string_delete(inChunk,1,limit);
limit = string_pos(",", inChunk);
LAY = string_copy(inChunk,1,limit-1);
inChunk = string_delete(inChunk,1,limit);
limit = string_pos("]", inChunk);
OBJ = string_copy(inChunk,1,limit-1);
inChunk = string_delete(inChunk,1,limit);

return [X,Y,LAY,OBJ];
}

on Draw event

draw_text(0, 0, string("This is the loaded file: ") + "");
for(i = 0; i < ds_list_size(global.CHUNKS); i += 1) {
outText = ds_list_find_value(global.CHUNKS, i);
draw_text(0, (i+1)*30, + string(outText));
}

oops...too late lol

Thanks, ill have too check out json_parse
 
Top