• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED]Creating level from text file issue

Hey there,
I'm having a go a making a simple level editor, but I'm having an issue with the level import.
I'm loading in a simple .txt file and looping through to place wall objects, but it seems to be copying the first column and inserting it and I have no idea why.

At the moment, my text file starts like this in the top left corner:
Code:
WWW
W
But it in the game it displays this:


Which as you can see, is too many blocks. There should only be three on top, then one underneath, instead we get four and then two.

This is the script I am using to import the level:
Code:
var i, j, _file, _grid, _height, _width, _arr, _str;
_grid = 16;
_height = round(room_height / _grid);
_width = round(room_width / _grid);

_file = file_text_open_read(working_directory + "levels\lvl_pac01.txt");

for(i = 0; i <= _height; i++)
{
    _arr[i] = file_text_read_string(_file);
    file_text_readln(_file);
}
file_text_close(_file);

for (i = 0; i <= _height; i++)
{
    for (j = 0; j <= _width; j++)
    {
        _str = string_char_at(_arr[i], j);
    
        if(_str == "W")
        {
            instance_create(j * _grid, i * _grid, obj_wall);
        }
    }
}
 

O.Stogden

Member
Could be that your loop counter starts at 0, so if you count up from 0, you're adding 1 to the total.

You have 3 W's, if you loop until you hit 3, that's 4 loops.

I think... haha.

Starting j at 1 should fix. If I'm reading this correctly. I'm super tired.
 
Oh that is interesting, sounds like that's all worth investigating. I think I tried starting the loop at one but I will try it all again. Thanks guys, will update you later.

EDIT: I changed the string_char_at(); line to be j + 1 and it worked great! Fixed my problem. Thank you so much.
 
Last edited:
Top