Page 1 of 1

Text strings

PostPosted: Tue Aug 31, 2010 5:34 am
by Leif
Hi :)

How can i load text strings into text actors from simple .txt file ?
And can GE make copy/paste actions with text data ?

Re: Text strings

PostPosted: Tue Aug 31, 2010 5:58 am
by skydereign
To do all of this you need to know how to use strings pretty efficiently. Also it really depends on what is in the .txt file that you want to read.

This is how to load text from a file into your text actor.
Code: Select all
FILE * textFile = fopen("filename.txt", "r");
fgets(text, 255, textFile);
fclose(textFile);


And to copy/paste strings you can use strcpy, which literally copies the second argument into the first. If you want to add the text onto the end of another string, you would use strcat... and so on.
Code: Select all
strcpy(string_0, string_1); // copies contents of string_1 into string_0

Re: Text strings

PostPosted: Tue Aug 31, 2010 6:21 am
by Leif
what is "r" in == FILE * textFile = fopen("filename.txt", "r"); == ?

i want to do:
.txt file with 150 strings
load: 1-st string into array[1], 2-nd string into array[2] etc.

About copy/paste
i mean - Ctrl+c copy in another place ( microsoft word for example) an Ctrl+p into text actor.

Re: Text strings

PostPosted: Tue Aug 31, 2010 6:30 am
by skydereign
fopen takes a mode, which is the "r" in this case.
"r" opens a text file for reading
"w" opens/creates a text file (it will discard previous contents)
"a" opens/creates a text file and places you at the end (append)
"r+" opens a text file for read and write
"w+" create a text file for read and write (it will discard previous contents)
"a+" opens/create a file for update and places you at the end

If you want that then I would use a for loop. The fgets will stop when the string it is reading is terminated. So just do this.
Code: Select all
FILE * textFile = fopen("filename.txt", "r");
int i;
for(i=0;i<150;i++)
{
    fgets(array[i], 255, textFile);
}
fclose(textFile);


You can't access the clipboard so you won't be able to copy things into a game, but you can simulate copy/paste within your game... but only in it.

Re: Text strings

PostPosted: Tue Aug 31, 2010 6:42 am
by Leif
Ok
thanks a lot )