Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Reading from an included text file in .zip with index.html file

A topic by FricativeMelon created Jan 05, 2022 Views: 605 Replies: 8
Viewing posts 1 to 5

I want to use text files to store JSON that I can  read from via my main index.html file, in the same directory within the zip file. I have been unable to find any general Javascript way to achieve this, so I'm wondering if anyone else has successfully done this for itch.io, and if so, how they did it. One can load entire scripts, so surely simply reading text is possible, right?

Moderator moved this topic to General Development
Moderator

Your game isn't kept as a zip file on the server. It's unpacked. You can load any file via AJAX, with a relative path.

I get a cross origin request error when I try to use AJAX (XMLHttpRequest) locally on my machine. I would assume that it wouldn't work on itch.io either, Am I right about that? If so, what am I doing wrong?

Moderator

Of course you're getting that error when opening a local file directly. You need to use a web server. On itch.io you already have one by definition, so it should work.

Thanks for your help! What would be the best way to be able to run the same code either on my own machine or through itch.io, and have it work either way (if there is one)?

Use Apache HTTP Server or something similar to run it from local host.

This is what I use to turn a folder on my machine into a localhost:3000 site: https://www.npmjs.com/package/http-server

(2 edits)

It may depend on what you want to do with them. I've used this for my game, where the game engine is written in JavaScript, and the data is stored in a separate file. In this case, I just made the data file another .js file, and stored the data as one big Javascript object.

e.g.

data.js

var gameObj = {

"house": {text: "My house",  directions: ["north"]},

"garden": {text: "My garden", directions: ["south", "east"]}

};

engine.js

function beginGame() {

doSomething(gameObj["house"]);

}

index.html

<head>

<script src="data.js"></script>

<script src="engine.js"></script>

</head>


You can check out an example in more detail by looking at the source of my first game (although please only use this for inspiration, don't reuse the actual code!): https://korosia.itch.io/the-witch-of-gingerglade

What are you trying to accomplish by doing this? If you just need to save and load state, you can make life much easier on yourself by using localStorage. Is your game multiplayer? If not, save yourself the trouble of managing state on your server and leave it on the client. Here’s a quick way to accomplish this (from StackOverflow):

var testObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));