Skip to content

Instantly share code, notes, and snippets.

@mildmojo
Last active February 21, 2018 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildmojo/4f055c219e781c62b2efac7e090f3a32 to your computer and use it in GitHub Desktop.
Save mildmojo/4f055c219e781c62b2efac7e090f3a32 to your computer and use it in GitHub Desktop.
DEPRECATED! See notes in code and in first comment. (Bitsy game engine mod to load game data from an external file or URL)
/*
************************************************************************
THIS GIST IS OUTDATED
There's a new version published in the bitsy-hacks repo, which fixes
some bugs. Go get it instead!
Direct link to the external game data mod:
https://raw.githubusercontent.com/seleb/bitsy-hacks/master/external-game-data.js
bitsy-hacks repo at: https://github.com/seleb/bitsy-hacks
************************************************************************
=================================
EXTERNAL GAME DATA MOD (mildmojo)
=================================
Usage: IMPORT <file or URL>
Examples: IMPORT frontier.bitsydata
IMPORT http://my-cool-website.nz/frontier/frontier.txt
IMPORT /games/frontier/data/frontier.txt
Installation:
1. Paste this code in new script tags right after the last /script> tag
in your exported game HTML file.
2. Copy all your Bitsy game data from the top of your HTML into another
file and replace it with an IMPORT statement that refers to that file.
NOTE: Chrome can only fetch external files when they're served from a
web server, not just opened from a file on disk. You could use Firefox
or, if you have NodeJS or Python installed, try running one of these
to serve the current directory from a local web server:
https://gist.github.com/willurd/5720255
If this mod finds an IMPORT statement anywhere in the Bitsy data
contained in the HTML file, it will replace all game data with the
IMPORTed data. It will not execute nested IMPORT statements in
external files.
License: WTFPL (do WTF you want)
*/
var _load_game = load_game;
load_game = function(game_data, startWithTitle) {
tryImportGameData(game_data, function withGameData(err, importedData) {
if (err) {
console.error(err);
console.warn('Make sure game data IMPORT statement refers to a valid file or URL.');
_load_game(game_data, startWithTitle);
} else {
_load_game(importedData, startWithTitle);
}
});
};
function tryImportGameData(gameData, done) {
var rejectBlanks = function(line) { return !line.match(/^\s*$/); };
var lines = gameData.split("\n")
lines = lines.filter(rejectBlanks);
for (var i = 0; i < lines.length; i++) {
if (getType(lines[i]) === "IMPORT") {
var src = lines[i].split(/\s+/)[1];
if (!src) return done('IMPORT requires a URL or path to a Bitsy data file!');
return fetchData(src, done);
} else {
return done(null, gameData);
}
}
}
function fetchData(url, done) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
return done(null, this.response);
} else {
return done('Failed to load game data: ' + request.statusText + ' (' + this.status + ')');
}
};
request.onerror = function() {
return done('Failed to load game data: ' + request.statusText);
};
request.send();
}
@mildmojo
Copy link
Author

mildmojo commented Feb 21, 2018

THIS GIST IS OUTDATED

There's a new version published in the bitsy-hacks repo, which fixes some bugs. Go get it instead!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment