≡
Products
▼
Support
▼
Company
▼
Forum
Ambiera Forum
Discussions, Help and Support.
Create a new account
,
Search
, or Login:
Create reply in
Setting up and manipulating variables from read content
Posted by:
(you are not logged in)
Enter the missing letter in: "Internation?l"
(you are not logged in)
Text:
[quote][b][/b] wrote: Hi, Basically, what you need to do is to: 1. Serialize your data; 2. Write content to the file; then later 3. Read content from the file; 4. Deserizlize data; In context of your task serialization is converting you data structures, i.e. your variables to the string which then can be written to the file. Deserialization is a reverse process - you read the string and convert it back to the same data structure. In the world of JS you usually use global JSON object for that. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON. It only has 2 funcitons - parse and stringify - which is exactly that's needed. But, as always there is a catch. Coppercube uses some really old JavaScript version, which I believe, is actually the ES1 - a very first version which appeared in 1997. And this global JSON object appeared in only ES5 which came 12 years later. But, it's actually resolvable, since in the magic world of JS there are Polyfills. These are libraries which add new feature supports for the old browsers. For example IE doesn't support most of the modern JS features and there are always polyfills used there for it to be able to render the modern sites. There are even transcompillers like Babel which allow to compile the code written with a newer JS versions to older JS versions. In order to use JSON in ES1 we need a corresponding polyfill. So, overall: 1. At first you data structure. I'm not sure how do you store your items/upgrades information, but I would store it in a regular array as a property of some scens node's behavior. Something like: [code]this.inventory = [ { type: 'item', name: 'showel', amount: 1 }, { type: 'item', name: 'broom', amount: 3 }, { type: 'upgrade', name: 'laser_weapon', amount: 1 } ];[/code] In order to serialize this array we have to add a JSON object polyfill. This implementation is taken from the mozilla site: [code] if (ccbGetPlatform() === 'windows') { JSON = { parse: function (sJSON) { return eval('(' + sJSON + ')'); }, stringify: function (vContent) { if (vContent instanceof Object) { var sOutput = ''; if (vContent.constructor === Array) { for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ',', nId++); return '[' + sOutput.substr(0, sOutput.length - 1) + ']'; } if (vContent.toString !== Object.prototype.toString) { return '"' + vContent.toString().replace(/"/g, '\\$&') + '"'; } for (var sProp in vContent) { sOutput += '"' + sProp.replace(/"/g, '\\$&') + '":' + this.stringify(vContent[sProp]) + ','; } return '{' + sOutput.substr(0, sOutput.length - 1) + '}'; } return typeof vContent === 'string' ? '"' + vContent.replace(/"/g, '\\$&') + '"' : String(vContent); } }; } [/code] 2. Not it's possible to serialize this.inventory into string and write into file: [code] // serialize var inventoryStr = JSON.stringify(this.inventory); // write to file data.txt ccbWriteFileContent('data.txt', inventoryStr); [/code] 3. You can read the file context and deserialize the content back: [code] // read string from file var inventoryStr = ccbReadFileContent('data.txt'); [/code] 4/ Deserilize and assign back: [code] this.inventory = JSON.parse(inventoryStr); [/code] That's it:)[/quote]
Possible Codes
Feature
Code
Link
[url] www.example.com [/url]
Bold
[b]bold text[/b]
Image
[img]http://www.example.com/image.jpg[/img]
Quote
[quote]quoted text[/quote]
Code
[code]source code[/code]
Emoticons
Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact