Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Setting up and manipulating variables from read content

onceforloops
Registered User
Quote
2022-06-16 17:56:27

I keep reading posts and information about the read content from file function, write content to txt file etc.
I guess I don't understand. How would I write a small script, and lets say I wanted manipulate a variable by reading it from the content from external txt file? How would write to file a new value for a variable, and the read it back in again later during the life of the executable?
Can someone please help me with some thorough code examples? With some // comments?
For example, I fly my ship to planet "A". I want to save the specs/inventory/upgrades of my ship, MyShip. Save MyShip's inventory and ccbSwitch Scene to another scene where my player/pilot(s) are on the planet with their seperate inventory for surface encounters???


okeoke
Registered User
Quote
2022-06-16 20:11:22

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:
this.inventory = [
{
type: 'item',
name: 'showel',
amount: 1
},
{
type: 'item',
name: 'broom',
amount: 3
},
{
type: 'upgrade',
name: 'laser_weapon',
amount: 1
}
];

In order to serialize this array we have to add a JSON object polyfill. This implementation is taken from the mozilla site:

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);
}
};
}

2. Not it's possible to serialize this.inventory into string and write into file:

// serialize
var inventoryStr = JSON.stringify(this.inventory);
// write to file data.txt
ccbWriteFileContent('data.txt', inventoryStr);


3. You can read the file context and deserialize the content back:

// read string from file
var inventoryStr = ccbReadFileContent('data.txt');


4/ Deserilize and assign back:

this.inventory = JSON.parse(inventoryStr);


That's it:)


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inte?national" (you are not logged in)


Text:

 

  

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