Reading text question

You must understand the Game Editor concepts, before post here.

Reading text question

Postby Murd-Machine[_] » Wed May 31, 2006 8:55 pm

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.
User avatar
Murd-Machine[_]
 
Posts: 72
Joined: Fri Nov 04, 2005 7:04 pm
Location: Moscow-city
Score: 1 Give a positive score

Postby makslane » Wed May 31, 2006 9:44 pm

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
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Novice » Wed May 31, 2006 9:47 pm

Nice, this will be put to good use with dialogs.
Why do i always get stuck?
User avatar
Novice
 
Posts: 399
Joined: Mon Aug 29, 2005 10:54 am
Location: Relative
Score: 5 Give a positive score

Postby Murd-Machine[_] » Fri Jun 30, 2006 10:06 pm

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.
User avatar
Murd-Machine[_]
 
Posts: 72
Joined: Fri Nov 04, 2005 7:04 pm
Location: Moscow-city
Score: 1 Give a positive score

Postby frodo » Fri Jun 30, 2006 11:43 pm

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?
User avatar
frodo
 
Posts: 127
Joined: Tue Mar 21, 2006 6:53 pm
Location: universe
Score: 2 Give a positive score

Postby Murd-Machine[_] » Wed Aug 02, 2006 10:32 pm

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.
:?: :?: :?:
User avatar
Murd-Machine[_]
 
Posts: 72
Joined: Fri Nov 04, 2005 7:04 pm
Location: Moscow-city
Score: 1 Give a positive score

Postby Murd-Machine[_] » Fri Aug 04, 2006 9:35 pm

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!
User avatar
Murd-Machine[_]
 
Posts: 72
Joined: Fri Nov 04, 2005 7:04 pm
Location: Moscow-city
Score: 1 Give a positive score

Postby makslane » Sat Aug 05, 2006 2:32 pm

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);
 }
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Murd-Machine[_] » Sat Aug 05, 2006 9:08 pm

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). :?
User avatar
Murd-Machine[_]
 
Posts: 72
Joined: Fri Nov 04, 2005 7:04 pm
Location: Moscow-city
Score: 1 Give a positive score

Postby makslane » Sat Aug 05, 2006 10:08 pm

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
}
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby Murd-Machine[_] » Sun Aug 06, 2006 10:25 pm

The last but not the least:
How do I forbid to draw '*'? I don't want to draw it.
User avatar
Murd-Machine[_]
 
Posts: 72
Joined: Fri Nov 04, 2005 7:04 pm
Location: Moscow-city
Score: 1 Give a positive score

Postby makslane » Sun Aug 06, 2006 10:42 pm

In this case, you can ignore the '*' by starting copy after first char:

Code: Select all
strcpy(textArray[nText], &line[1]);
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest