• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Check for a string in a text file?

X

XirmiX

Guest
So, I looked through some text file and list commands and read a bit about them. For some reason I can't figure out how to code it so that the program checks whether the 0th, 1st or whichever line has a certain text in it. I know that's not what text files are really for in and of themselves, but there should be some way to do this with the text files right?
 
R

renex

Guest
Code:
var found=0,file,compare="i want to find this",line=1;

file=file_text_open_read(filename)
while (!file_text_eof(file)) {
    if (file_text_read_string(file)==compare) {
        found=line
        break
    }
    file_text_readln(file)
    line++
}
file_text_close(file)

if (found) show_message("found string at line "+string(found)+".")
 
X

XirmiX

Guest
Code:
var found=0,file,compare="i want to find this",line=1;

file=file_text_open_read(filename)
while (!file_text_eof(file)) {
    if (file_text_read_string(file)==compare) {
        found=line
        break
    }
    file_text_readln(file)
    line++
}
file_text_close(file)

if (found) show_message("found string at line "+string(found)+".")
Ugh... Why does it have to be so complicated? Why can't it just be something like:

file_text_open_read_line(filename,line)

With "filename" being the name of the file you're reading and "line" being the line which you're reading FROM that file? That would make things much more simple! Or perhaps something like:


file_text_open_read_text(filename,text)

With "text" being the text which the program would read from the file. Like, cmon GLM devs, git unto that :p

But if there really isn't any simpler way, I guess I'll try to understand every part of the code you've provided me to apply it to whatever I'm doing.
 
Last edited by a moderator:
B

bojack29

Guest
Thats a good point you make.

But what if suppose there isnt a second line of text in a text file your trying to access? You'd need something to catch that.

You could create a script, if one doesn't exist already, to do something like that for you.
 

Surgeon_

Symbian Curator
It works like that because reading from a text file (or any other file for that matter) is like reading from a buffer. Basically you have a pointer which moves along the buffer as you read data. And while you can peek at any position within the buffer, it's not as fast as reading the data as it goes, one byte at a time. And since text files are not ordered, and there's no concept of "lines" in them other than in the program you use to open said text files (there's a NEWLINE character), your script which would allow you to read an arbitrary line within a text file would have to begin at the very start of the text file, and read characters until it reads a sufficient amount of NEWLINE characters, the process the line it happens to be on (provided it did not hit EOF before that). This would be fine if you only needed to read one line and you knew which line it was, but for iterating through the file - terrible - and it would only get worse with larger text files.
 
B

bojack29

Guest
@Surgeon_ is right. For the purposes of speed and convenience, text files, like buffers are read sequentially from one end to the other. It is for this reason that if you want one particular line of text, make sure that line of text is toward the top of the file. Peeking and jumping all over a text file or buffer is terrible practice and should be avoided at all cost.
 
X

XirmiX

Guest
I think I know a way to make things a tad bit easier and avoid the player having to type in the directory from which their account would be read (client-account-data-holding is what I'm trying to achieve here); scripts. Although, the problem is, I don't think I can create a script whilst the game is running. I know I can do that with, say text files, but scripts?

If that is possible, then how? And I've tried to create a global list in a script and then call out that list in a piece of code in another object, but for some reason, it doesn't like it when i put "global." in front of a list function nor does it like it when...
Never mind, i found a solution to the problem myself whilst writing this message :D so... I only need to find out how and even whether is it possible to create a script whilst the game is running. Google searches unfortunately don't have anything related to it that could help me out here :/ I hope I can create scripts whilst the game is running from within the game, so I don't have to fumble in the jumble of "ugh" that is text file reading via code >_<
 
Top