• 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!

Windows how to export a json/ .txt file with the game

if i have a json ( .txt ) file with my games stats in how do i then export it with the game
if you can do it at all....
 

samspade

Member
A couple questions:
  • export, import, or both?
  • is the data (or do you want the data to be) an array/struct or map/list?
Here are the functions I use:

GML:
/// @function import_json(file_name, func)
/// @param {string} _file_name        the file to get the json data from
/// @param {func}    _func            the function to use on the string
/// @description imports json data form a file. Pass json_decode
///            to return the data as maps and lists. Pass json_parse to
///            return the data as arrays and structs.
function import_json(_file_name, _func) {
    if (file_exists(_file_name)) {
        var _file, _json_string;
        _file = file_text_open_read(_file_name);
        _json_string = "";
        while (!file_text_eof(_file)) {
            _json_string += file_text_read_string(_file);
            file_text_readln(_file);
        }
        file_text_close(_file);
        return script_execute(_func, _json_string);
    }
    return undefined;
}
    
    
/// @function export_json(file_name, data, func)
/// @param {string} _file_name        the file to save the json data to
/// @param {struct/array/map} _data    the data to save as json data
/// @param {func}    _func            the function to use on the data
/// @description saves json data to a file. Pass a map and json_encode
///            to return save json data stored as maps and lists. Pass an
///            array or struct and json_parse to save the data stored
///            as arrays and structs.   
function export_json(_file_name, _data, _func) {
    var _file = file_text_open_write(_file_name);
    var _string = script_execute(_func, _data);
    file_text_write_string(_file, _string);
    file_text_close(_file);
}
Here's my tutorial on them as well: https://www.youtube.com/playlist?list=PLwgH1hDD0q1EHVrscm8m_N2kKtQaDAgO8
 

samspade

Member
Sorry, I don't understand. You would either import a text file, or you would export to a text file. Saying you can't export a text file doesn't make sense.
 
yes but i thought expansions only was for code expansions and stuff sorry for wasting your time i just have to read more carefully next time (ps love your yt)
 
Top