Page 1 of 1

Reading text question

PostPosted: Wed May 31, 2006 8:55 pm
by Murd-Machine[_]
How do I make GE read text file (e.g. "a.txt"), select random line there and copy line's text to variable for further use? Thanks in advance.

PostPosted: Wed May 31, 2006 9:44 pm
by makslane
In the Global Code, put this code:

Code: Select all
char textArray[10][256]; //Max 10 text of 255 characters
int nText = 0; //Number of texts read


void readText(char * fileName)
{
 
 char line[256];
 FILE *arq = fopen(fileName, "r");


 if(arq)
 {
  while(fgets(line, 255, arq) && nText < 10)
  {
   if(strlen(line) > 0) //Dont put empty lines
   {
    strcpy(textArray[nText], line);
    nText++;
   }
  }
 }
}



In the crete actor event (or other event), init the array:

Code: Select all
readText("mytext.txt");


Now, to use the text, try this:

Code: Select all
int n = rand(nText);
strcpy(randomText.text, textArray[n]);


Get the example here:
http://game-editor.com/examples/randomtext.zip

PostPosted: Wed May 31, 2006 9:47 pm
by Novice
Nice, this will be put to good use with dialogs.

PostPosted: Fri Jun 30, 2006 10:06 pm
by Murd-Machine[_]
How do I read text with:
/n - to change lines

And how do I make GE change variable to 1 if some symbol ("/a" for instanse) is in front of current line and change it back to 0 if not.

PostPosted: Fri Jun 30, 2006 11:43 pm
by frodo
I got the demo and I can't figure out where the code is to make it say a sensible phrase, instead of just something like "alkjfasd;lfkjsadfj". :shock: can someone tell me?

PostPosted: Wed Aug 02, 2006 10:32 pm
by Murd-Machine[_]
How do I change this example so that I get:

Text file e.g ("a.txt"):
Hi! /nHow are you?
*Hi! /nI'm fine

Actor "text1"
draw simple lines

Actor "text2"
draw lines which start with "*"

And make new line if "/n" is there, like in "strcpy" code.
:?: :?: :?:

PostPosted: Fri Aug 04, 2006 9:35 pm
by Murd-Machine[_]
2Makslane:
I know you can do it, because you are guru. I know that you have some better things to do, but don't leave your users without help!

PostPosted: Sat Aug 05, 2006 2:32 pm
by makslane
Murd-Machine[_] wrote:How do I read text with:
/n - to change lines


Use a single character to separate the lines, like |.
So, the text Hi! /nHow are you? , must be:
Hi! |How are you?

Try the code:

Code: Select all
char textArray[10][256]; //Max 10 text of 255 characters
int nText = 0; //Number of texts read


void readText(char * fileName)
{
 
 char line[256];
 int i;
 FILE *arq = fopen(fileName, "r");


 if(arq)
 {
  while(fgets(line, 255, arq) && nText < 10)
  {
   if(strlen(line) > 0) //Dont put empty lines
   {
 
    //Make multiline if found | char
    for(i = 0; i < strlen(line); i++)
    {
        if(line[i] == '|') line[i] = 10; //10 = Line-Feed, look the ASCII table
    }
 
    //Copy to array
    strcpy(textArray[nText], line);
    nText++;
   }
  }
  fclose(arq);
 }

PostPosted: Sat Aug 05, 2006 9:08 pm
by Murd-Machine[_]
Thank you very much! After some reconstruction it perfectly! :P
But what about "*" mark? I want to make something like this:
if "*" is in front of the current line, then variable=1, else variable=0
What should I add in the global code? (Of course "*" is just an example, I don't care what sign should it be). :?

PostPosted: Sat Aug 05, 2006 10:08 pm
by makslane
You can try this in the readText function:

Code: Select all
if(if(line[0] == '*')
{
  //set value if found}
else
{
  //set value if not found
}

PostPosted: Sun Aug 06, 2006 10:25 pm
by Murd-Machine[_]
The last but not the least:
How do I forbid to draw '*'? I don't want to draw it.

PostPosted: Sun Aug 06, 2006 10:42 pm
by makslane
In this case, you can ignore the '*' by starting copy after first char:

Code: Select all
strcpy(textArray[nText], &line[1]);