3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: How to Read and Write Highscore to a file?  (Read 135 times)

« on: April 25, 2014, 06:41:03 AM »
How can I read and write a value or a string (Highscore) in a file using script?

 I used iFileReadOpen() and iFileWriteOpen(), it shows error in opening, reading and writing in the file.
--Sam
« Reply #1 on: April 27, 2014, 09:35:39 AM »
Look up on the forums on how to save / write strings and integers to files.
Google: site:3drad.com/forum highscore
[::-Bitpoint-::]
« Reply #2 on: April 28, 2014, 02:18:52 AM »
Code: [Select]
int FileDS=0, Err=0;
string StrData="";
string FileName=".\\text_file.txt";

///=====================================
void Main()
{
   /// ad a trigger process or key stroke or what have you to
   ///start the save and or read file process

   //Save_File();
   //Read_File();
}

///=====================================
void Save_File()
{
   int F_Data=0;
   F_Data=iFileWriteOpen(FileName);
   if(F_Data>=0){
      iFileStringWrite(F_Data,26+"Data_text",true);
   }
   iFileClose(F_Data);
}
///=====================================
void Read_File()
{
   FileDS=iFileReadOpen(FileName);
   if(FileDS==-1){
      Err=2;
   }else{
      iFileStringRead(FileDS,StrData);
      if(StrData==""){
         Err=3;
      }else{
         Err=0;
      }
   }
   iFileClose(FileDS);
}
« Reply #3 on: April 28, 2014, 02:58:37 AM »
Let me to give you an example...

Code: [Select]
string highscore;
float score;
Void Main() {
       if (iInitializing()) {
       int Score = iFileReadOpen(".\\example.txt");
    // You can write every extension you want
            iFileStringRead(Score,highscore);
               iFileClose(Score);
            score = iStringVal(highscore);
       }

   //Place everything here like score = IN_X ...

   if (iDeinitializing()) {
       iStringStr(highscore,score,"%0.0f");
     int Score = iFileValueWrite(".\\example.txt");
        iFileStringWrite(Score,highscore,true);
            iFileClose(Score);
     }
}

If your highscore is not a value like upper float "score" better remove the functions iStringVal() and iStringStr().

Script can have errors because i've been writing it on android. Please reply for errors.
If we don't give up we can achieve the unthinkable!
« Reply #4 on: April 28, 2014, 07:17:26 AM »
there's several considerations you've gotta take into account Sam...

first is the logic (always)... next is the code... finally, the write privileges that the user has for the particular OS...

the logic is pretty straight forward...

check for file exist
open file to read
iterate through file reading score and storing highest score
close file
compare stored high score with current score
announce new high score if current score is highest

open file to append and add new record
close file


the code again should be pretty straight forward... just look at the script reference (there are examples) and follow your logic...

the user write privileges are gonna be predicated on several things... primarily the version of windows the user is running your program on...  you should try to write to the user's document folder... might be a good idea to create a game folder in user documents...

sorry i can't be more specific, i'm ont on my home machine at this time...


--Mike
« Reply #5 on: April 29, 2014, 01:52:23 AM »
Thanks its working now.
--Sam
Pages: [1]