Message from JavaScript discussions

April 2019

— If you want to go the proper "OOP way", you might want a separate class at this point:


class SaveData() {
static load() {
return loadDataSomehow();
}
static save() {
return saveDataSomehow();
}
}

Message permanent page

— 

Maybe you can make save data take a path of some kind, instead of static methods, then it could be something like:

class SaveData {
constructor(path) {
this.path = path;
}
save(data) {
return fs.writeFileSync(
this.path,
JSON.stringify(data));
}
load(path = this.path) {
return JSON.parse(
fs.readFileSync(path));
}
}

— And you would instantiate like so:

new Game(new SaveData('data/save.json'));

— Within Game:

constructor(saveData) {
this.saveData = saveData;
}

addToInventory(item) {
this.inventory.push(item);
// the object should probably be replaced by this.getData() or something, which would produce an object containing all data to be saved
this.saveData.save({ inventory: this.inventory });
}

Message permanent page

— And you could save less often if you wished (instead of at every change)

— Suggest a good website project.

— All of this is clearly out of my league TRGWII 😂

— Heh

— I need to think on it for a while

— Where is this app gonna run?

— It will affect how and where you save to

— This is just for training purposes