Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #macro FILE_SYSTEM "system.ext" // Edit for your game
- #macro FILE_EXT ".ext" // Edit for your game
- #macro TIMESTAMP [current_year, current_month, current_day, current_hour, current_minute]
- /////// SAVE LOAD FUNCTIONS ////////////
- function FileSaveMap(_filename, _map, _encode)
- {
- // Saves a DS _map to _filename. _encode true or false
- var _string = json_encode(_map);
- if _encode _string = base64_encode(_string);
- var _buffer = buffer_create(string_byte_length(_string)+1, buffer_fixed, 1);
- buffer_write(_buffer, buffer_string, _string);
- buffer_save(_buffer, _filename);
- buffer_delete(_buffer);
- }
- function FileLoadMap(_filename, _decode)
- {
- // Loads a file (json) and returns it as a DS map/list
- // _decode true or false depending on how you saved it previously
- if is_undefined(_filename) || !file_exists(_filename) return noone;
- var _buffer = buffer_load(_filename);
- var _map = buffer_read(_buffer, buffer_string);
- buffer_delete(_buffer);
- if _decode _map = base64_decode(_map);
- return json_decode(_map);
- }
- /////// SYSTEM AND PROFILES //////////
- function FileLoadSystem()
- {
- // Attempts to load the system file, or makes a default one
- var _system = FileLoadMap(FILE_SYSTEM, true);
- if (_system == noone)
- {
- // Create System Defaults add more for your game
- _system = ds_map_create();
- _system[? "VOLUME_BGM"] = 0.5;
- _system[? "VOLUME_SFX"] = 0.5;
- FileSaveMap(FILE_SYSTEM, _system, true);
- }
- // Returns a DS map with system info
- return _system;
- }
- function FileListByExtension(_ext, _list)
- {
- // Creates a list of files of the same extension
- // This does not load the files, it just compiles their names as strings
- // Optional _list for a previously created ds_list
- if is_undefined(_list) _list = ds_list_create();
- else ds_list_clear(_list);
- var _file = file_find_first("*"+_ext, 0);
- while (_file != "")
- {
- ds_list_add(_list, _file);
- _file = file_find_next();
- }
- return _list;
- }
- function FileLoadProfile(_file)
- {
- // Loads a profile by file name or creates a default one
- // Must declare global.ProfileList as a ds_list somewhere first
- // Must declare global.Sytem as a ds_map first
- if is_undefined(_file) _file = global.ProfileList[| 0];
- var _map = FileLoadMap(_file, true);
- if (_map == noone)
- {
- // Make a default profile
- _map = PlayerCreateProfile();
- FileSaveMap(_map[? "NAME"]+FILE_EXT, _map, true);
- global.System[? "DEFAULT_PROFILE"] = _map[? "FILE"];
- FileSaveMap(FILE_SYSTEM, global.System, true);
- }
- return _map;
- }
- function PlayerCreateProfile()
- {
- // Creates a default player profile and returns it as a ds_map
- var _new = ds_map_create();
- var _name = "save_"+NumToPaddedString(ds_list_size(global.ProfileList) + 1, 3);
- _new[? "NAME"] = _name;
- _new[? "FILE"] = _name+FILE_EXT;
- _new[? "TIME_CREATED"] = TIMESTAMP;
- _new[? "TIME_SAVED"] = TIMESTAMP;
- // Some example things to add to a new profile
- ds_map_add_map(_new, "STATS", ds_map_create());
- ds_map_add_map(_new, "KEYS", ds_map_create());
- return _new;
- }
- ////// Helper Functions ///////
- function NumToPaddedString(_num, _cnt)
- {
- // Creates a padded string with _num an adds zeros according to _cnt
- // for example, _num = 5, _cnt = 3 will return 005
- var _pad = "";
- repeat (_cnt) _pad = string_insert("0", _pad, string_length(_pad)+1);
- _pad = string_insert(_pad, string(_num), 1);
- return string_copy(_pad, string_length(_pad) - _cnt + 1, _cnt);
- }
Add Comment
Please, Sign In to add comment