Jump to content


Photo

CVS Files


  • Please log in to reply
4 replies to this topic

#1 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 15 December 2010 - 05:42 AM

I've been Google-ing and searching the forums but haven't found anything concrete on reading CSV files. Is it possible to do so in GML? I know I can use arrays to store and manipulate the values within the game, but I'm not sure what to do when it comes to reading/writing to/from external CSV files.

[edit] Oops, I mean CSV files, not CVS.

Edited by SHiLLySiT, 15 December 2010 - 05:45 AM.

  • 0

#2 sigonasr

sigonasr

    Hard Code that Works

  • GMC Member
  • 412 posts
  • Version:GM8

Posted 15 December 2010 - 05:49 AM

I have been using this script ever since I made it for many of my games and it does the job well.

If I may direct you to the website I have posted this script at:

http://sigonasr2.servegame.org/zgamers/v5/forum/index.php?topic=16.msg77#msg77

How you would use it is as follows. Let's say you had a list of numbers and Strings that you would like to parse. Let's say they're separated by commas, like most CSV files. "Bob,21,1,16,1975"

Now, using this script, you can do this:
string="Bob,21,1,16,1975"
open_variables(string,",")
name=Var[1]
age=real(Var[2])
bornmonth=real(Var[3])
bornday=real(Var[4])
bornyear=real(Var[5]

Notice that I converted the numbers to real? This script returns all values as strings. The first argument of my custom script is the string itself to parse. The second argument is what the values are separated by. In this case, a comma. It has to be a string, so I wrap it in quotes. The script returns each value in an array in Var[X] format, so you can then extract everything that way!

It can return the number of values it parsed and also where it parsed them, but I doubt you will need those unless you are doing some serious automation work. Hope that helps! If you need me to elaborate more, I can. There is an example gmk on the site where the script is located also.
  • 0

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14445 posts
  • Version:GM:Studio

Posted 15 December 2010 - 05:52 AM

GM7 can handle space delimited values + newlines.

GM8 can't

if gm8 or commas are used you would have to read the file with file_text_read_string entirely and parse it.
  • 0

#4 SHiLLySiT

SHiLLySiT

    GMC Member

  • New Member
  • 78 posts
  • Version:GM:Studio

Posted 15 December 2010 - 05:58 AM

I have been using this script ever since I made it for many of my games and it does the job well.

If I may direct you to the website I have posted this script at:

http://sigonasr2.servegame.org/zgamers/v5/forum/index.php?topic=16.msg77#msg77

How you would use it is as follows. Let's say you had a list of numbers and Strings that you would like to parse. Let's say they're separated by commas, like most CSV files. "Bob,21,1,16,1975"

Now, using this script, you can do this:

string="Bob,21,1,16,1975"
open_variables(string,",")
name=Var[1]
age=real(Var[2])
bornmonth=real(Var[3])
bornday=real(Var[4])
bornyear=real(Var[5]

Notice that I converted the numbers to real? This script returns all values as strings. The first argument of my custom script is the string itself to parse. The second argument is what the values are separated by. In this case, a comma. It has to be a string, so I wrap it in quotes. The script returns each value in an array in Var[X] format, so you can then extract everything that way!

It can return the number of values it parsed and also where it parsed them, but I doubt you will need those unless you are doing some serious automation work. Hope that helps! If you need me to elaborate more, I can. There is an example gmk on the site where the script is located also.


Thank you so much! I'm working on a rouge-like and I've been trying to load stats from files but nothing was working the way I wanted it. If I have problems I'll post on this thread.
  • 0

#5 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14445 posts
  • Version:GM:Studio

Posted 15 December 2010 - 06:02 AM

There is also
http://www.gmlscript...pt/string_parse
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users