Writefile, OpenFile, ReadFile help...

Started by Phemar, Wed 16/06/2004 06:38:16

Previous topic - Next topic

Phemar

Okay for my next extremely short test game I need the game to write a file to the game's directory telling a high score. Next time the player opens the game he can view the previous score and overwrite it if his current one is higher.

I've got this:

//GlobalInt (42); is the score in integer form.
string scorelabel;Ã,  //score in string form, for displaying on GUIs.
string scoredisplay; //erm, the high score for displaying...I can't really remember...

/**////I don't know where to put this:////**/
int scorewrite=FileOpen ("temp.tmp", FILE_WRITE); //I don't understand this whole
part (about which filename to specify, copied from manual)
if (scorewrite==0) {
Ã,  Display("The Game could not retreive the requested file.");
Ã,  }
else {
Ã,  FileWrite (scorewrite, scorelabel); //Writes string 'scorelabel' to the file
Ã,  FileRead (scorewrite, scoredisplay); //Read the file into a string called scoredisplay
Ã,  Display ("Your current high score is %s.", scoredisplay); //Displays your score.
Ã,  FileClose (scorewrite); //Close the file.
Ã,  }


The main problem lies in the fact that I can't grasp the whole concept...

What I want it to do, is kind of repeatedly execute the function to check for a new high score then write it.
Then when the player clicks on a button in the GUI, it displays his current high score.

Ignore all my script, I just wanted to show I did attempt it...

EDIT: But also, instead of displaying it, you could also do a SetLabelText () thing...

Gilbert

Okay, let's try cracking down the problem.

What you need is:

1. read the hiscore if requested
2. write the score to file if current score is > the hiscore stored in the file.

If you're only going to record the hiscore you just need to write an int variable to the file, if you want to record other info, such as signature, etc., you can also write some string into it.
But, to start simple I'll assume you only read and write one variable (of int type) to the file.

To do 1., first I'll suggest you have a variable holding the hiscore (I'll recommend a variable to hold it in the global script and make it a accessible for rooms using export and import, or just use a GlobalInt, I'll use a normal int variable in this example). So you only need to read the value back when the game is started, so I'll put:
On top of global script:
int hiscore;

In game_start():
int hischandle=FileOpen ("hiscore.dat", FILE_READ);
if (hischandle==0) {// file not exists, create one.
  hiscore=0;
  hischandle=FileOpen ("hiscore.dat", FILE_WRITE);
  FileWriteInt (hischandle, hiscore);
  FileClose(hischandle);
} else { // file exists, read its content into hiscore
  hiscore=FileReadInt(hischandle);
  FileClose(hischandle);
}



To do 2. I don't recommend doing it in a repeatedly execute manner, as it'll utilize file access in every game loop, I'll remmend checking if the score is higher than hiscore when a game's ended (like gameover or completed the game), and overwrites the file if there's a new hi score:
if (score>hiscore) { //if you break the hi score
  hiscore = score;
  int hischandle=FileOpen ("hiscore.dat", FILE_WRITE);
  if (hischandle==0) {// file error
    Display("The Game could not retreive the requested file."); 
 } else { // write to file
    FileWriteInt(hischandle, hiscore);
    FileClose(hischandle);
  }
}



Note that when you want to display/check/etc the current hi score, you can just access the variable hiscore.

Phemar

#2

Herm...All I receive is a 0...

My script is like this:

int highscore;
function repeatedly_execute () {
  if ((dead1==1) && (dead1==2) && (dead1==3) && (dead1==4) && (dead1==5) &&
     (dead1==6) && (dead1==7) && (dead1==8) && (dead1==9) && (dead1==10) && 
     (dead1==11)) { //all the aliens are dead, check No.2 at the bottom here.
    if (GetGlobalInt(42)>highscore) {
      highscore=GetGlobalInt(42);
      int hischandle=FileOpen ("highscore.dat", FILE_WRITE);
      if (hischandle==0) {
        Display ("The game could find a highscore.");
        }
      else {
        FileWriteInt (hischandle, highscore);
        FileClose (hischandle);
        }
      }
}}

function interface_click(int interface, int button) {
  if (interface==STATUSLINE) {
    if (button==7) {
      Display ("Highscore: %d", highscore); //displays your high score.
      }
    }
}


1. The problem is when I click the highscore button on the GUI, it displays a 0.
2. This checks all the aliens have been killed. What I want it to do is to give the player say 5 seconds to kill as many as they can, and then save a highscore. I can't do this with Wait (x), because that is a blocking command, and the player needs to be able to shoot, you see.
3. I've got this script in my room:
function room_a() {
  // script for room: Player enters screen (after fadein)
  int i=1;
  while (i<16) {
    Wait (5);
    ChangeCharacterView (i, 2);
    MoveCharacter (i, Random(320), Random(200));
    if (i==15) {
      i=1;
      }
    else {
      i++;
      }
    }   
}


The problem here is the wait command. I don't want it there, but if I take it out, then  the whole game freezes when i enter the room. (game starts in that room.)
What it's supposed to do is randomly move around a bunch of aliens.
If the wait command is there, then the game waits while all the aliens are walking around.
There are 15 aliens.
What the script is supposed to do, is choose a random location, and move the character 'i' there. Then loop. I can take out the wait command and destroy the loop, and it will work fine.
But I need the loop.
This one really has me stumped.

I hope I've explained myself clearly enough...Help anyone?

Pumaman

Can you post the script you have to read in the high score when you first start up the game?

Gilbert

Right, specifically did you read the hiscore back from the file at game start like I told you to?

Phemar


I really have no idea what you're talking about...sorry.

LordHart

I think it'd be best to have a timer set there, and then put the code you need for when they kill the 5 aliens in time or not in the repeatedly execute. I'm not entirely sure what you are trying to do with the script below, so I don't know if it'd be what you need.

function room_a() {
  // script for room: Player enters screen (after fadein)
  int i=1;
  while (i<16) {
    SetTimer (1, 200);
    ChangeCharacterView (i, 2);
    MoveCharacter (i, Random(320), Random(200));
    if (i==15) {
      i=1;
      }
    else {
      i++;
      }
    }   
}


The way you also had the wait command would of had it waiting 1/8th of a second. If tou were going to do 5 seconds, you must remember that the default games cycle is 40, so 5secs = 200.

Pumaman

Quote from: Zor© Ver. 2.3 on Sat 19/06/2004 07:04:21
I really have no idea what you're talking about...sorry.

Zor, please re-read Gilbert's original post carefully. The code that you posted will write the high score to a file at the end, but it never reads the hi score back in from the file when you start the game. That way, the high score will get reset to 0 every time to load the game up.

SMF spam blocked by CleanTalk