It’s me again. (Asking a lot, so I’m hoping to give back a lot, too)
So, since my story javascript was getting massive while using the setup
object to create all the various items and such I need for my game, and I have barely even gotten started, I’ve decided it would be best to either (a) Do it all in an external script and use importScripts()
to bring it in, or (b) use JSON to store the data. I’m partial to the second option because that is the purpose of JSON and I’ve had success with it in previous projects.
In previous projects, I used plain AJAX scripts (the XMLHttpRequest object directly) to import the JSON and it was in the previous version of Twine when testing your game would open it in Twine’s tool instead of the browser. Now, I’m using jQuery AJAX and the project is opened in the browser: it’s not working, and I’m not sure which of these is causing me grief.
Here is how I’m sending the request:
if (window.hasOwnProperty("storyFormat")) {
setup.path = "C:/Users/phoen/Documents/Twine/Stories/TheFeydrinMeadows/";
}
else {
setup.path = "";
}
var dataSources = [
"src/test.json"
];
// Import data
for (var i = 0; i < dataSources.length; i++) {
var options = {
url: setup.path + dataSources[i],
type : "get",
async : false,
cache : false,
error : function (x, status, error) {
alert("Error loading " + i + ": " + error);
},
success : function (res, status, xhr) {
var obj = JSON.parse(res);
for (var prop in obj) {
setup[prop] = obj[prop];
}
}
};
$.ajax(options);
}
And here is my test JSON file:
{
"Range" : [
"melee",
"reach",
"short-range",
"mid-range",
"long-range"
]
}
I’m certain the file path is correct, but my error function is running and its out put is:
“Error loading 0: Network Error: a network error occurred”
I’ve been fiddling with this for a while but can’t seem to find the issue. My knowledge of how all this works is limited, so I thought I would ask here in case I’m making an obvious mistake.
NOTE: I’m using “async = false” because I need my story script to hang until this data is loaded. I have code that builds objects from various pieces stored in the database, and if those loops run before the data is loaded, the objects won’t be built.