GMS function load_csv() in Godot

KoromoKoromo Posts: 9Member

How to implement the function "load_csv()" of GMS(gamemakerstudio) in Godot and read the corresponding values in the CSV file and store in a Variable[x,y]?

get_csv_line() :just read one line and only first line (By seek() change but too troubles)

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 4,855
    Accepted Answer

    Well, it really depends on how Game Maker Studio stores CSV files, and/or how the CSV file you are wanting to read is setup.

    CSV stands for "Comma Separated Value", meaning that every comma (,) means a new value.
    For example, here is what a simple CSV file could contain:
    2,3,1,4, 1,2,4,3, 4,1,3,2, 3,1,2,1,

    And with a CSV file formatted like this, we could parse it into a two dimensional array in Godot like this (untested):

    var csv_array = []
    var csv_file = File.new();
    csv_file.open("filepath_to_csv_file.csv", csv_file.READ)
    while not csv_file.eof_reached():
        var csv_row = []
        var csv_line = csv_file.get_line()
        for element in csv_line.split(","):
            csv_row.append(element);
            # If you know the data will always be floats, use the following instead of the above.
            #csv_row.append(float(element))
        csv_array.append(csv_row);
    # Then you can access the array like this:
    # (Assuming there is something at that position)
    print ("Example value at X=5 Y=2 : ", csv_array[5][2]);
    # To get the size of the data (assuming every row has the same size), you just do this:
    var csv_size_column = csv_array.size();
    var csv_size_row = csv_array[0].size();
    

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 4,855Admin
    Accepted Answer

    Well, it really depends on how Game Maker Studio stores CSV files, and/or how the CSV file you are wanting to read is setup.

    CSV stands for "Comma Separated Value", meaning that every comma (,) means a new value.
    For example, here is what a simple CSV file could contain:
    2,3,1,4, 1,2,4,3, 4,1,3,2, 3,1,2,1,

    And with a CSV file formatted like this, we could parse it into a two dimensional array in Godot like this (untested):

    var csv_array = []
    var csv_file = File.new();
    csv_file.open("filepath_to_csv_file.csv", csv_file.READ)
    while not csv_file.eof_reached():
        var csv_row = []
        var csv_line = csv_file.get_line()
        for element in csv_line.split(","):
            csv_row.append(element);
            # If you know the data will always be floats, use the following instead of the above.
            #csv_row.append(float(element))
        csv_array.append(csv_row);
    # Then you can access the array like this:
    # (Assuming there is something at that position)
    print ("Example value at X=5 Y=2 : ", csv_array[5][2]);
    # To get the size of the data (assuming every row has the same size), you just do this:
    var csv_size_column = csv_array.size();
    var csv_size_row = csv_array[0].size();
    

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file