Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
array from txt file

nigec
Registered User
Quote
2022-05-30 23:35:38

I have a text file with a string of texture names
"mycursor.png", "gcur.png", "gcur_action.png", "gcur_forward.png", "gcur_down.png"


this works:
const cursorNames =["mycursor.png", "gcur.png", "gcur_action.png", "gcur_forward.png", "gcur_down.png"];


and I know:

 curtxt =ccbReadFileContent( "cursor.txt").toString('utf-8');

reads the file, but how do I use the text file rather than string in the code line

I hope it makes sense :|


sven
Registered User
Quote
2022-05-30 23:57:05

split will do the job for you.

var stringfromfile=ccbReadFileContent("MyFile.txt");

var lines = stringfromfile.split('\n');

for(var i = 0; i < lines.length; i++){
print( lines[i]);
}


instead of '\n' you can use whatever separator you want to use here.. for example "," or space.


nigec
Registered User
Quote
2022-05-31 09:38:17

Awesome, thank you Sven


nigec
Registered User
Quote
2022-05-31 11:57:59

here's what I've come up with, I wanted various types of cursors controlled by a variable, which changes depending on the hotspot interaction, I can also do inventory cursors
I might have a look at doing the switch code dynamically

/* <action jsname="action_addcursor" description="add cursor">
<property name="Cursor_Path" type="string" default="cursors/" />
</action>
*/
//

action_addcursor = function () {

};
action_addcursor.prototype.execute = function (currentNode) {
var path = this.Cursor_Path;
print(path);
const cursorNames = [];
var stringfromfile = ccbReadFileContent("cursor.txt");
var lines = stringfromfile.split(',');
for (var i = 0; i < lines.length; i++) {
print(lines[i]);
cursorNames.push(lines[i]);
}
print(cursorNames[0]);
var curs = path + cursorNames[1];
var curs2 = path + cursorNames[2];
var curs3 = path + cursorNames[3];
var curs4 = path + cursorNames[4];

function onFrameDrawing() {
var mX = ccbGetMousePosX();
var mY = ccbGetMousePosY();
var invcur = ccbGetCopperCubeVariable("invcurs");
var tex = ccbLoadTexture(curs);

switch (invcur) {
case 1:
tex = ccbLoadTexture(curs)
break;
case 2:
tex = ccbLoadTexture(curs2)
break;
case 3:
tex = ccbLoadTexture(curs3)
break;
case 4:
tex = ccbLoadTexture(curs4)
break;
default:
tex = ccbLoadTexture(curs);
}
ccbDrawTextureRectangleWithAlpha(tex, mX - 4, mY - 4, mX + 28, mY + 28);

ccbSetCursorVisible(false);
}

ccbRegisterOnFrameEvent(onFrameDrawing);

}



DouweDabbe
Guest
Quote
2022-06-07 01:01:44

I think is much easier to use JSON object here:

var yourCursorObject = {
name : [""],
path: [""],
image: []
};

Does coppercube support JavaScript Json?


In the meantime PLEASE start migrating your JSON-using applications over to Crockford’s json2.js. It is fully compatible with the ECMAScript 5 specification and gracefully degrades if a native (faster!) implementation exists.

There are two primary methods for handling JSON: JSON.parse (which converts a JSON string into a JavaScript object) and JSON.stringify (which convert a JavaScript object into a serialized string).

https://stackoverflow.com/questions/22883743/how-can-i-write-an-array-to-a-file-in-nodejs-and-keep-the-square-brackets


nigec
Registered User
Quote
2022-06-07 09:31:53

I'm struggling with js trying to figure out what works and what doesn't, I'm piecing bits together and hoping it works :)
But yes I agree I need to start using JSON, I'm trying to do my own inventory and other things
I'll have a look at the link, thank you


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internat?onal" (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