ambiera home ambiera home

Ambiera Forum

Discussions, Help and Support.

Create a new account, Search, or Login:
Name:  Password:    
Ambiera Forum > CopperCube > Programming and Scripting
save an existing variable to an external .txt file or .INI file.
Author Text

edkeyte
Registered User
Quote
2014-01-04 03:18:48

How could I save an existing variable to an external .txt file or .INI file?

If I had a variable name like: "Score"
I'm trying to use these JS methods without success:

ccbGetCopperCubeVariable(varname)

ccbWriteFileContent(filename, content)

If I use: ccbGetCopperCubeVariable to get the value of the variable into the content of the text file and then write that value using: ccbWriteFileContent

But then the saved .INI file content would look something like this:

Score=562

Saved inside scores.ini which could be saved in the same directory as the game app.

Niko could you give me a hint as to how the code would be written out to make this work?

Or if there is an easier way to just get in game variable data in and out of CC to and from an external text file that could reside in the same directory as my app. please let me know.
Thanks.

Ed.


niko
Moderator
Quote
2014-01-04 07:17:51

If you are struggling to write a script for writing out that content of your file, it will probably get very difficult for you to read it, because this would involve even more complex code (parsing ini files isn't that simple).
But to start, for writing out that file, it would look like this:

var varContent = ccbGetCopperCubeVariable('score');
var iniFileContent = 'score=';
iniFileContent += varContent;
iniFileContent += '\n';
ccbWriteFileContent('saved.ini', iniFileContent);

I haven't tried this, but I think it should work. Note that on newer versions of Windows, it is not allowed to write into certain directories, so the file will end up somewhere else, if it is for example installed in "C:\program files", but this is virtualized, so for the game, it should work anyway, you might just not see the file at the location of your game.


edkeyte
Registered User
Quote
2014-01-04 10:33:26

Thanks Niko. That works fine cheers.


techno-valley
Registered User
Quote
2014-01-04 13:44:59

Thanks edkeyte for this very interesting topic and Thanks Niko as well for this answer.

I would appreciate if someone can post here the reverse operation
Load operation

I tried the following code to read a value from file and assign to a variable in coppercube


var varContent = 0;
varContent = ccbReadFileContent(Saved.dat);
ccbSetCopperCubeVariable(Score,varContent);



pmax
Registered User
Quote
2014-01-04 17:41:01

If you can be helpful, you can see the plugin that I tried to do.
reads an external file to edit a selected object.

http://www.ambiera.com/forum.php...


techno-valley
Registered User
Quote
2014-01-04 22:13:13

Thanks a lot pmax for your code

I've found the error in my code as well

I discovered where was my problem
variables should be defined before being used
var <variable name>
and in the ccb functions has to be used between single quotes ... I don't have any java programming basics background ..


var varContent = 0;
var varContent = ccbReadFileContent('saved.dat');
ccbSetCopperCubeVariable('Score',varContent);



edkeyte
Registered User
Quote
2014-01-05 05:42:39

That's great techno-valley!

Thanks!

The loaded data works but unlike the first example from niko you are not getting just the number value of the score.
You're getting both the full text from the file and the score too.

Do you know how to just get the number without the first part?

Here's what it looks like anyway.


http://www.queenseng.com/cc-test...

Also @pmax code is fantastic! Thanks.

Ed.


techno-valley
Registered User
Quote
2014-01-05 11:40:45

yes

Just replace the load code with the following



var varContent = 0;
var readline;
var varContent = ccbReadFileContent('saved.txt');
readline = varContent.substring(6,varContent.length-1)
ccbSetCopperCubeVariable('Score',readline);


Code explanation:

The second line containing " readline = " code reads from Character number 6 ("equal sign =") till the end of line (varContent.length-1 to avoid taking the next line carriage return)

Let me know if you have any question.

Thanks for pmax code as well


volt
Registered User
Quote
2016-03-16 21:09:30

This is a very helpful discussion, thanks for posting. I tried the sample and it works very well for an exe but does not seem to work in WebGL. What do you think needs to be changed in the code to work in WebGL


techno-valley
Registered User
Quote
2016-03-17 08:09:49

Hello Volt,

from the documentation

ccbWriteFileContent(filename, content)

Note: This function may not do anything depending on the target it runs on (websites for example have no access to the file system).

I've tried to save locally to the disk using WebGL and Flash output. It didn't work neither for WebGL nor for Flash.


volt
Registered User
Quote
2016-03-17 13:53:24

Hello techno-valley,

Thanks for the response, what about saving to a file on my server? www.mydomain.com/save.txt


techno-valley
Registered User
Quote
2016-03-17 23:30:55

Hello volt,

you may check the following two commands in the documentation

ccbDoHTTPRequest(url, callback)
ccbCancelHTTPRequest(connectionIdToCancel)

You can update a database hosted on the server other than saving text file for example.


alokik
Registered User
Quote
2016-08-08 20:37:28

Hey,
How to save multiple variables, position and other parameters in a single file??


camagames
Registered User
Quote
2017-05-06 23:01:09

Please, can you provide an example of this saving and loading codes? the one once posted is no longer available...

Please, reply!


techno-valley
Registered User
Quote
2017-05-07 14:47:59

I've created a quick example.

You may download here :
http://www.techno-valley.com/cce...


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inter?ational" (you are not logged in)


Text:

 

  

Forum 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


   





Software: Ambiera Light Forum. © 2008-2015 Nikolaus Gebhardt, Ambiera e.U.