GM:S 1.4 [SOLVED]Creating level from text file issue

Discussion in 'Programming' started by Siolfor the Jackal, Apr 17, 2019 at 1:45 PM.

  1. Siolfor the Jackal

    Siolfor the Jackal Member

    Joined:
    Jun 21, 2016
    Posts:
    635
    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:
    [​IMG]

    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);
            }
        }
    }
     
  2. O.Stogden

    O.Stogden Member

    Joined:
    Jul 25, 2018
    Posts:
    181
    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.
     
    Siolfor the Jackal likes this.
  3. AlexDerFerri

    AlexDerFerri Member

    Joined:
    Aug 24, 2017
    Posts:
    91
    I don't know if that could help but keep in mind that the function "string_char_at()" start counting at index 1.
     
    Siolfor the Jackal and O.Stogden like this.
  4. Siolfor the Jackal

    Siolfor the Jackal Member

    Joined:
    Jun 21, 2016
    Posts:
    635
    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: Apr 17, 2019 at 3:03 PM

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice