I am not able to access the ‘.data’ functionality from common JS. Cf this source’ example (40% down @ “There you go:”). I have rewritten the example into this:
var Gpuperf = pc.createScript('gpuperf');
// initialize code called once per entity
Gpuperf.prototype.initialize = function() {
this.t0 = performance.now(); this.fr =0; this.deltaT50 =0; deltaT_Total = 0; this.fr_Total =1;
this.rows = [];
this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
};
// update code called every frame
Gpuperf.prototype.update = function(dt) {
//var txtP = this.app.root.findByName("TextPerf");
var t1 = performance.now(); var t1DatapointIdx =0; var t1UsableVaue = (t1*1000).toFixed(2); var deltaT =0; deltaT+= dt;
try{
if(this.entity.animation.currentTime < this.entity.animation.duration) {
// Animation is running:
if(this.fr > 50) {this.deltaT50=deltaT/50*1000; this.fr =0; deltaT_Total = this.deltaT50/this.fr_Total*1000;
var perfPrScrRes =0; var h = window.innerHeight; var w = window.innerWidth; perfPrScrRes = this.deltaT50/(Math.sqrt(h*w)/1000)*1000;
this.app.root.findByName("TextPerf").element.text = "GPU scr P: "+perfPrScrRes.toFixed(3) + ":"+this.deltaT50.toFixed(3); var t1GetAnimTime = (t1 - this.t0)/1000;
this.rows.push([t1DatapointIdx, t1UsableVaue, perfPrScrRes, t1GetAnimTime]); t1DatapointIdx++; console.log("TRL: "+ this.rows.length);
}
} else {
var tmpGetAnimTime = (t1 - this.t0)/1000;
if(this.fr >3){this.app.root.findByName("TextPerf_AnimDone").element.text = tmpGetAnimTime.toFixed(2);}
if(this.fr > 50) {
this.fr =0;}
}
} catch(err){}
this.fr++; this.fr_Total++;
};
Gpuperf.prototype.onMouseWheel = function(event){
console.log("onMouseWheel " + this.rows.length +" :: "+ this.rows[1]);
var result; var ctr; var keys; var columnDelimiter; var lineDelimiter; var data;
data = this.rows.data || null;
if (data === null || !data.length) {
console.log("onMouseWheel null" ); // NB! This is where the whole script returns (stops)
return null;
}
columnDelimiter = this.rows.columnDelimiter || ',';
lineDelimiter = this.rows.lineDelimiter || '\n';
console.log("onMouseWheel tr2" );
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
console.log("onMouseWheel tr3" );
data.forEach(function(item) {
ctr = 0;
keys.forEach(function(key) {
if (ctr > 0) result += columnDelimiter;
result += item[key];
ctr++;
});
result += lineDelimiter;
});
console.log("onMouseWheel tr4" );
this.downloadCSV("stock-data.csv");
};
Gpuperf.prototype.downloadCSV = function(filename) {
var data, link;
var csv = convertArrayOfObjectsToCSV({
data: this.rows
});
if (csv === null) return;
filename = this.rows.filename || 'download';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data = encodeURI(csv); console.log("DWL data " + data );
link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
What to put instead at this.rows .data in order to get past the ‘if (data === null || !data.length) {’-condition?
ok, get that ‘PC-JS’ === vanilla JS (ES5) - still doesn’t change that a ‘common JS (of 2018)’ stackoverflow-community regards the usage of a ‘.data’-containing example by grading it; “Awesome answer. I’m upvoting this one … etc”. … Just saying …
We are back at the fact that ‘.data’ (apparently) doesn’t work in PC => vanilla JS (ES5).
So I guess I can move on in the debugging of the script , by passing the part of:
data = this.rows.data || null;
if (data === null || !data.length) {
console.log("onMouseWheel null" ); // NB! This is where the whole script returns (stops)
return null;
}
Then I get down to the parts of:
keys = Object.keys(data[0]);
result = '';
result += keys.join(columnDelimiter);
result += lineDelimiter;
console.log("onMouseWheel tr3" );
data.forEach(function(item) {
where access of ‘data’ is needed anyway.
As such; a method to use the ‘.data’-functionality (or equal to) alongside the list/array of this.rows[i].
{data = this.rows.data}
I am not sure you understood correctly what @yaustar and @Leonidas are saying. In javascript Array types do not have .data property. You cannot access this.rows.data - it simply doesn’t exist. It has nothing to do with Playcanvas.
Ok, let us turn the whole thing upside down and forget versions and parallel JS-communities. If a Stackoverflow example of 2018 does not comply with present PlayCanvas, then what to use in order to write to a .csv-file and save it? (being my main goal)
I have already tried out 3-4 of the examples in the stackoverflow example, without luck (and mainly due to JS-versions/age of the early examples (6-9 years old)).
If so, the function converts an array of objects to a CSV file. The args function param is an object that has .data as the array as well as other properties.
As you can see, .data is something they came up with as a name for their property. They could as well call it .myData. It is not part of standard js Array type, but a name of their own custom property.
var csv = convertArrayOfObjectsToCSV({
data: stockData
});
You can see here that they create a new object, with the property data that references stockData which is an array of objects to the function that converts it the CSV.