Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Function for reading settings-files.

agbdev
Registered User
Quote
2021-12-20 18:21:29

Heythere ;D
Big JavaScript noob here,
I am currently trying to write an function which can read KeyValue from an KeyName in an settings.file.

Ive tried to do it like that:

function getSettings(keyname) {
// Read the settings file.
const textInFile = ccbReadFileContent("settings.gpf");
//print(textInFile)
// Split all lines and put them into a list.
const linesInFile = textInFile.split("\n");
// Loop trough all lines and find the line with the matching KeyName.
for (let i = 0; i < linesInFile.length; i++) {
// Current selected line
const lineWithIndex = linesInFile[i];
// If the line matches the KeyName, return to the value of 1 line below.
if( lineWithIndex === "["+keyname+"]" ) {
return linesInFile[i + 1];
}
}
}


The settings file would look like this:
[ApplicationTitle]
GameXY
[ApplicationVersion]
0.3

So if you want to get the games title, you would need to getSettings("ApplicationTitle");

But the function allways returns 0, i dont know why.

Thanks for any help or tips :D


sven
Registered User
Quote
2021-12-20 19:55:23

There is emty space at the end of your string thats why it never == with string you compare..


One way for doing it


function getSettings(keyname) {
const textInFile = ccbReadFileContent("settings.gpf");
const linesInFile = textInFile.split("\n");
for (let i = 0; i < linesInFile.length; i++) {
const lineWithIndex = linesInFile[i];
//print("|"+lineWithIndex.slice(0, -1)+"|");//VISUAL
if( lineWithIndex.slice(0, -1) === "["+keyname+"]" ) {
return linesInFile[i + 1];
}
}
return "NoMatch";
}



Another way

function getSettings(keyname) {
const textInFile = ccbReadFileContent("settings.gpf");
const linesInFile = textInFile.split("\n");
for (let i = 0; i < linesInFile.length; i++) {
const lineWithIndex = linesInFile[i];
if( lineWithIndex === "["+keyname+"]"+"\r" ) {
return linesInFile[i + 1];
}
}
return "NoMatch";
}



agbdev
Registered User
Quote
2021-12-20 20:44:40

Thanks :D


Create reply:


Posted by: (you are not logged in)


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