How to read a whole line from a file... [UPDATED 02-11-05] [SOLVED]

Started by DoorKnobHandle, Tue 01/02/2005 19:16:26

Previous topic - Next topic

DoorKnobHandle

Hello!

I got three brief questions:

- I know how to get a single character out of a textfile, but is there a trick how to read a whole line..?
- How can I animate a cursor? I made my cursormode animated and I assigned a view and I animated
that view (in all four directions actually right now). It still is not animated. What is wrong?
- How can I RawDraw on GUIs?

Thanks for your help on any of those questions!

Scummbuddy

I don't believe you can rawdraw on top of guis. they are higher than the rawdraw.

is your cursor enabled, and the current cursor?
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

Goot

RawDraw functions only apply to the background, so objects, characters and GUI's are in front of them. You seem to be animating the cursor right. Just double check to make sure that you have the right view number assigned to the right cursor mode. Also, cursors only animate with loop 0 of the given view, so you can't do different loops for different directions without some intense scripting. (You would have to save the mouse coordinates to variables and then find out if the new coordinates are farther to the top, left, bottom, or right of the old ones.)

Scorpiorus

Quote- I know how to get a single character out of a textfile, but is there a trick how to read a whole line..?
There is a FileRead(int handle, string buffer) function you can use to read strings that are written with FileWrite. Or do you mean a plain text line that you can type-in in Notepad and then read from AGS?

DoorKnobHandle

Thanks for you help so far.

To Scummbuddy:
Ok! Thank you.

To Goot:
I triple checked. It says it will use view 9 and view 9 has the cursor animation in loop 0!
Strange...

To Scorpiorus:
I want to read a line out of a note-pad text document. It is going to include game relevant information,
and it is going to be a level file, for example, the first line contains the number of the player's starting
position (x) and the second one the y coordinate, the third is the levels name, etc...

Scorpiorus

Quote from: mamarulez on Wed 02/02/2005 20:25:24To Scorpiorus:
I want to read a line out of a note-pad text document. It is going to include game relevant information,
and it is going to be a level file, for example, the first line contains the number of the player's starting
position (x) and the second one the y coordinate, the third is the levels name, etc...
Here is an example of function that reads text from a plain text file:

function FileReadRawString(int fileHandle, string buffer) {
 
    int error_code = 0;
    char byte = 0;
   
    StrCopy(buffer, "");
 
    while (FileIsEOF(fileHandle)==0)
    {
        byte = FileReadRawChar(fileHandle);
       
        error_code = FileIsError(fileHandle);
       
        if (!error_code) // if there is no error reading the file
        {
            if (byte == 10)
            {
                return error_code;
            }
            else if (byte != 13)
            {           
                StrSetCharAt(buffer, StrLen(buffer), byte);
            }
        }
        else
        {
            return error_code;
        }
    }
 
    return FileIsError(fileHandle);
}

It returns 0 if the line was read successfully.

DoorKnobHandle

thank you so much, man. I know I seen this func before but I couldnt find it.


you made my day ^^

DoorKnobHandle

Sorry for warming this thread up, but I still can't get my animated cursor to work although I checked everything more than twice.

QuoteI triple checked. It says it will use view 9 and view 9 has the cursor animation in loop 0!

I also have a question with that FileReadRawString function. It lets me read out the first line from a file correctly, but how can I jump in the second line to read out the next variable?

Goot

Try using a different loop and see if it works. Or try using a different cursor mode. Also, make sure you don't have it checked so that it only animates when over a hotspot or character or whatever it is. And make sure you're not using a different cursor mode that looks the same as the one set to View 9.

DoorKnobHandle

checked it all. still does not work. it shows the first sprite from view 9 loop 0 but it is not animated...

I did not check the two additional options (Only over hotspot etc...) and I also tried it with standard cursor mode checked and not checked... Makes no differnece at all...

DoorKnobHandle

#10

What the hell is wrong???

Replace the 24 with 9... Still does not work!

EDIT:

Now I'm havin troubles with this as well although I already had it working...

Doesnt give me any errors while compiling but then when I load a game it throws
me out with an error in the third line in that FileReadRawString func!

Code: ags

function FileReadRawString(int fileHandle, string buffer) {
Ã,  
Ã,  Ã,  int error_code = 0;
Ã,  Ã,  char byte = 0;
Ã,  Ã,  
Ã,  Ã,  StrCopy(bbuffer, "");
Ã,  
Ã,  Ã,  while (FileIsEOF(fileHandle)==0)
Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  byte = FileReadRawChar(fileHandle);
Ã,  Ã,  Ã,  Ã,  
Ã,  Ã,  Ã,  Ã,  error_code = FileIsError(fileHandle);
Ã,  Ã,  Ã,  Ã,  
Ã,  Ã,  Ã,  Ã,  if (!error_code) // if there is no error reading the file
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  if (byte == 10)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  return error_code;
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  else if (byte != 13)
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  {Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  StrSetCharAt(buffer, StrLen(buffer), byte);
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  Ã,  Ã,  else
Ã,  Ã,  Ã,  Ã,  {
Ã,  Ã,  Ã,  Ã,  Ã,  Ã,  return error_code;
Ã,  Ã,  Ã,  Ã,  }
Ã,  Ã,  }
Ã,  
Ã,  Ã,  return FileIsError(fileHandle);
}

function load_level ( string filename )
{
	int level = FileOpen ( filename, FILE_READ);
	int error_check = FileReadRawString(level, map.name);
	if (error_check == 0)
		Display ("'%s': ERROR READING LEVEL DATA (NAME)!", filename);
	FileClose ( level );
	
	return level;
}


What is wrong here?

Any help please?

Scorpiorus

Animating cursor:
Try making a new test game and setting up a cursor animation there, see if it works.
I think, animation may not work if the cursor mode is constantly reset in repeatedly_execute(always). Because, it is possible that apart from changing a cursor mode SetCursorMode also resets a cursor animation frame back to 0 and thus it won't work if it's called each game loop. So, check it just in case.


FileReadRawString:
QuoteDoesnt give me any errors while compiling but then when I load a game it throws
me out with an error in the third line in that FileReadRawString func!
And what's this line, is it StrCopy(buffer, "")? (I assume StrCopy(bbuffer, ""); is just a typo?)


Quoteint error_check = FileReadRawString(level, map.name);
I see you are using map.name as a buffer. How did you declare .name?
It *must* be something like:

struct SOMESTRUCT {
...
char name[200]; // any AGS-compatible string must have a length of at least 200 characters;
...
};

DoorKnobHandle

Thank you so much, Scorpiorus.

You are right with the animating cursor thing... It's because I set it to Cursor Mode
0 every game loop and so I guess it keeps showing the first frame...

That BBuffer is a typo, and I forgot to put the [200] behind that map.name string,
now everything works...

One more thing left open:

Is there a way to read the second line out of the file now?


EDIT: Well it does not work entirely correct now...

I added the [200] to map.name and now it returns me the error message:
TESTLEVEL.TLV: ERROR READING LEVEL DATA (NAME)
and when I check the map.name contents in the game it is filled with the first four characters
from that TESTLEVEL.TLV file and nothing more... That is really strange...

Scorpiorus

Quote from: mamarulez on Fri 11/02/2005 18:36:16Is there a way to read the second line out of the file now?
Yep, just call it multiple times, so omting error checks:

FileReadRawString(file, buffer1); // read first line
FileReadRawString(file, buffer2); // read second line
FileReadRawString(file, buffer3); // read third line

QuoteEDIT: Well it does not work entirely correct now... and when I check the map.name contents in the game it is filled with the first four characters...
Hmm, and what's the 5th character in TESTLEVEL.TLV? Seems like FileReadRawChar I used can't read it for some reason.
Can you provide the text file (TESTLEVEL.TLV) you're trying to read?

DoorKnobHandle

Thanks,

yes, of course I can reveal it :D

it is:

Testlevel

That's it... nothing special with the forth or fifth character but it just saves
"Test" in map.name...

Scorpiorus

Quotefunction load_level ( string filename )
{
int level = FileOpen ( filename, FILE_READ);
int error_check = FileReadRawString(level, map.name);
if (error_check == 0)
Display ("'%s': ERROR READING LEVEL DATA (NAME)!", filename);
FileClose ( level );

return level;
}
You see, checking for (error_check == 0) means that you'll get a error message each time a string has been read correctly, since error_check == 0 means there is no error.
By the way, why do you return level? It can't be used further because you close a file with a line above.
So try the following code:

function load_level ( string filename )
{
int level = FileOpen ( filename, FILE_READ);
int error_check = FileReadRawString(level, map.name);
if (error_check != 0) // or just if (error_check)
Display ("'%s': ERROR READING LEVEL DATA (NAME)!", filename);
FileClose ( level );
//Test:
Display("the string is:%s", map.name);


return level;
}

What does Display show?

DoorKnobHandle

Thank you so much for all this work...

No that is strange...

With your functions, it says: THE STRING IS: TESTLEVEL (coreectly) but when the game runs and
I press TAB to test it again with this code:

Code: ags

 if (keycode==9) Display (map.name);  // Tab, show mapname


it displays only "Test"... Wierd isn't it..?

Scorpiorus

Quote from: mamarulez on Fri 11/02/2005 20:10:15
it displays only "Test"... Wierd isn't it..?
Yep, that means the string is indeed read correctly but there is something that corrupts the map.name variable later (possibly in repeatedly_execute).

First of all, what does the struct (one that's used to declare map) look like?

DoorKnobHandle

That's the struct MAP:

Code: ags

struct structMap {
	string name [50]; // official name of current map
	int scenario; // 0: europe, 1: africa
};


The second parameter "int scenario" is not used right, but it is going to be the content
of that second line...

I searched through the whole 1000-line script for "map.name", but it is only mentioned in your function and in my keycode check code part. Nowhere else...

Scorpiorus

Let's see...

Replace:

struct structMap {
string name [50]; // official name of current map
int scenario; // 0: europe, 1: africa
};

with:

struct structMap {
char name [200]; // official name of current map
int scenario; // 0: europe, 1: africa
};

This is very important, otherwise you can experience stange things.

Also, look into other structs (if you have any other) and replace string with char and always use 200 characters.

See if it helps...

SMF spam blocked by CleanTalk