How to read information from a file with Data.readInt?


(BlueSystem) #1

Hello, I’m trying to load information (scores) from a txt file with the Data class and readInt function, but i don’t know how to read a specific line. Are the parameters “name” and “defautValue” but i don’t understand what their functionalities. Thanks!


(Bora Kasap) #2

how did you set file path for external file, actually i’m curious about that before answering your question :smiley:


(BlueSystem) #3

I placed the txt file in the same folder that .as file and i try to load it with Data.load(“highscores.txt”);


(Jacob Albano) #4

The Data class uses Flash cookies. You can’t load from an arbitrary text file unfortunately.


(Alex Larioza) #5

:open_mouth: When was the Data class added?!

Alternatively, you can use Flash’s URLLoader to load external files (but not write).


(BlueSystem) #6

In that case, how could read and write a Flash cookie with the Data class? Since I do not understand its operation with the parameters “name” and “defaultValue”, or if there is any tutorial, better.


(Bora Kasap) #7

So, you can use the same way as saving & loading game for saving & loading highscores…

I’ve just looked around and cannot find a “short” sample to show whole data saving & loading process in a single post.

I’m just writing here some descriptions & a sample to save and load highscores.

var playername:String = "Alobar";
var playerscore:int = 250;
Data.writeInt(playername, score); 

this function doesn’t save any files, it just stores an integer variable named as “Alobar”

writeBool(), writeString(), writeUint() are the same for different variable data types…

then you can save your stored data as a file with Data.save("gamename_highscores"); but that’s an encyripted file…

So now, you can use Data.load("gamename_highscores"); to load your highscores data from the same file.

After you load your data you can pull highscores from data to variables

var playerscore:int = Data.readInt(playername, 0); the zero number here is the default score and it is used only when data not found… because of you have to try to read some data to check is there any highscores data saved before when playing game first time, because of flash cannot now is that first time or not… that means you need to load your data before you save your data to check if already saved before