Jump to content


Photo

Game Maker Lists/Split String


  • Please log in to reply
7 replies to this topic

#1 dudeishfish

dudeishfish

    GMC Member

  • New Member
  • 93 posts

Posted 23 July 2010 - 10:27 PM

Greetings Game Makers,

I'm making a program that goes into a text file, creates a new entry in a list for each line, and than splits each line based on the commas.

So I have this code so far:

glist = ds_list_create();
xd=file_text_open_read("gl.txt");
while(!file_text_eof(xd)){
    ft =file_text_readln(xd);
    ds_list_add(glist,ft);
}
bob=ds_list_read(glist,0)
show_message(bob)

Any idea what could be wrong with it? Also, how do you split a string in Game Maker?

Thanks,
-Fish

Edited by dudeishfish, 24 July 2010 - 02:17 PM.

  • 0

#2 ParodyKnaveBob

ParodyKnaveBob

    theUndiscovered

  • GMC Member
  • 1230 posts
  • Version:GM:Studio

Posted 23 July 2010 - 11:22 PM

Hard to see what's wrong with it when you haven't told the result it produces. $:^ ]

But I can see one potential issue:

ft =file_text_readln(xd);
The manual says this "Skips the rest of the line in the file and starts at the start of the next line." I'm guessing you want:

ft = file_text_read_string(xd);
The manual says this function "Reads a string from the file with the given file id and returns this string. A string ends at the end of line."

The Game Maker Language (GML)
- Files, registry, and executing programs
- - Files

As for string-splitting,

The Game Maker Language (GML)
- Computing things
- - String handling functions

You should be able to use a combination of string_pos() and string_copy() and arrays (or data structures), methinks.

I hope this helps,

Edited by ParodyKnaveBob, 23 July 2010 - 11:25 PM.

  • 0

#3 dudeishfish

dudeishfish

    GMC Member

  • New Member
  • 93 posts

Posted 24 July 2010 - 12:48 AM

Thanks, man.

EDIT:

Ok, so now I'm using the following code:
glist = ds_list_create();
xd=file_text_open_read("gl.txt");
while(!file_text_eof(xd)){
    ft = file_text_read_string(xd);
    ds_list_add(glist,ft);
   
}
show_message(string(ds_list_read(glist,0)))

I run it, and the program stops, displaying only black. I have a feeling that it might be stuck in the while loop. I tried inserting the show message into the while loop, but I got an unexpected crash error.

Ideas?

Edited by dudeishfish, 24 July 2010 - 12:53 AM.

  • 0

#4 ParodyKnaveBob

ParodyKnaveBob

    theUndiscovered

  • GMC Member
  • 1230 posts
  • Version:GM:Studio

Posted 24 July 2010 - 01:38 AM

You're welcome. Super fast reply (at least for now)

Debug mode.

In your while loop, give some show_message() pop-ups of another sort. A simple counter or something. See what happens each iteration of the while() loop before it explodes. And if you have to, put a counter_1 before the file & ds stuff, and a counter2 after. Don't forget to differentiate between your pop-ups, such as show_message("counter 1: "+string(counter1)); etc.

And you probably shouldn't need the string() function in this case because you're already storing nothing but strings into glist (through the file_text_read_string() function).

I hope this helps, too, heheh,

Edited by ParodyKnaveBob, 24 July 2010 - 01:41 AM.

  • 0

#5 dudeishfish

dudeishfish

    GMC Member

  • New Member
  • 93 posts

Posted 24 July 2010 - 02:23 AM

Ok, the counter really helped narrow down the problem. The while loop never finishes, and the counter goes to infinity. How do I make the while loop stop when it reaches the end?
  • 0

#6 ParodyKnaveBob

ParodyKnaveBob

    theUndiscovered

  • GMC Member
  • 1230 posts
  • Version:GM:Studio

Posted 24 July 2010 - 02:36 AM

Just on a whim, try this out...

glist = ds_list_create();
xd=file_text_open_read("gl.txt");
while(!file_text_eof(xd)){
    ft = file_text_read_string(xd);
    ds_list_add(glist,ft);
    file_text_readln(xd);
}
show_message(ds_list_read(glist,0));
I'm thinking, maybe _read_string is doing something unexpected when it hits the final end of line? I don't know, like I said, just something to try. The manual makes one think what you have should work fine as-is.

And whether you try that or not, I'd do another show_message() in your while() loop: show_message(ft); . See what it's actually reading, and what it does when it goes bonkers.

Regards,

Edited by ParodyKnaveBob, 24 July 2010 - 02:38 AM.

  • 0

#7 dudeishfish

dudeishfish

    GMC Member

  • New Member
  • 93 posts

Posted 24 July 2010 - 02:44 AM

Right on, ParodyKnaveBob! It worked! Thank you very much, and thanks to everyone in this topic who helped.
  • 0

#8 ParodyKnaveBob

ParodyKnaveBob

    theUndiscovered

  • GMC Member
  • 1230 posts
  • Version:GM:Studio

Posted 24 July 2010 - 02:51 AM

Right on, ParodyKnaveBob! It worked! Thank you very much, and thanks to everyone in this topic who helped.

You're very welcome; I'm glad it works now. $:^ ]

...and you're only thanking yourself and me! Lol!! $E^ b
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users