Hey guys. Super noob question, but how do I read in text from a json file. If I could have a quick example I think I would be off to the races, but for some reason I feel a bit lost. I am simply trying to read text from the file. The format is taking the first name and last name of a character.
In the Editor, you can create a new JSON asset by selecting the ‘+’ button in the Asset panel. Edit the JSON and add something like:
{
"characters": [
{
"firstName": "John",
"lastName": "Smith"
},
{
"firstName": "Jane",
"lastName": "Bloggs"
}
]
}
Then, in a script, you can do:
pc.script.create('game', function (app) {
// Creates a new Game instance
var Game = function (entity) {
this.entity = entity;
};
Game.prototype = {
// Called once after all resources are loaded and before the first update
initialize: function () {
// Read the character data from the JSON asset
var characterData = app.assets.find('Characters').resource;
var characters = characterData.characters;
// Print the names to the JS console
for (var i = 0; i < characters.length; i++) {
var character = characters[i];
console.log(character.firstName + ' ' + character.lastName);
}
},
// Called every frame, dt is time in seconds since last update
update: function (dt) {
}
};
return Game;
});
Does that help?
I was in the vicinity because I made the JSON file and I just could not figure out the syntax. I feel kind of silly knowing it was this simple, but then again, I am just a humble developer still learning the ropes. Thanks for your time and help.