Hello Guest

Author Topic: Tilemap LoadFromCSV method  (Read 2805 times)

0 Members and 1 Guest are viewing this topic.

ericmbernier

  • Member
  • Posts: 35
  • Sometimes I make games.
    • View Profile
Tilemap LoadFromCSV method
« on: February 03, 2014, 05:28:13 PM »
While working on the code for my tutorial series, I wrote a quick method that is inspired by the fromString() method in FlashPunk's Tilemap class. It can take a character delimited string, perhaps comma delimited like a CSV file, and loads a Tilemap accordingly. I currently have it in my Tilemap class in my Otter project, and figured it might be a good idea to have it put into the actual Otter codebase. Anyway, here it is, if anyone wants it:

Code: [Select]
       
public void LoadFromCSV(string str, char columnSep = ',', char rowSep = '\n')
{
        bool u = UsePositions;
        UsePositions = false;

        string[] row = str.Split(rowSep);
        int rows = row.Length;
        string[] col;
        int cols;
        int x;
        int y;
         
        for (y = 0; y < rows; y ++)
        {
            if (row[y] == "")
            {
                continue;
            }
                   
            col = row[y].Split(columnSep);
            cols = col.Length;
            for (x = 0; x < cols; x ++)
            {
                if (col[x].Equals("") || Convert.ToInt32(col[x]) < 0)
                {
                    continue;
                }

                SetTile(x, y, Convert.ToInt16(col[x]));
            }
        }
                       
        UsePositions = u;
   }

Sorry for the ugly formatting, it didn't paste over as well as I expected.
« Last Edit: February 04, 2014, 06:39:45 AM by ericmbernier »

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Tilemap LoadFromCSV method
« Reply #1 on: February 04, 2014, 02:38:37 AM »
Awesome! I'll throw this in to the main project when the next round of updates goes in.

ericmbernier

  • Member
  • Posts: 35
  • Sometimes I make games.
    • View Profile
Re: Tilemap LoadFromCSV method
« Reply #2 on: February 11, 2014, 08:45:15 PM »
Wrote a similar method for the GridCollider class, as I am loading solids via a CSV in my tutorial series:

Code: [Select]
public void LoadFromCSV(string source, string empty = "0", string filled = "1", char columnSep = ',', char rowSep = '\n')
{
    string[] row = source.Split(rowSep);
    int rows = row.Length;
    string[] col;
    int cols;
    int x;
    int y;

    for (y = 0; y < rows; y++)
    {
        if (row[y] == "")
        {
            continue;
        }

        col = row[y].Split(columnSep);
        cols = col.Length;
        for (x = 0; x < cols; x++)
        {
            if (col[x].Equals("") || !col[x].Equals(filled))
            {
                continue;
            }

            SetTile(x, y, col[x].Equals(filled));
        }
    }
}

I plopped this right in my GridCollider class, in my Otter project. Perhaps it will be useful for others as well!

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Tilemap LoadFromCSV method
« Reply #3 on: February 12, 2014, 04:47:34 PM »
Cool, I'll toss this in as well.

If you want, you can fork Otter from bitbucket and then submit your own changes via pull requests, then I can just merge them in to the main branch.

ericmbernier

  • Member
  • Posts: 35
  • Sometimes I make games.
    • View Profile
Re: Tilemap LoadFromCSV method
« Reply #4 on: February 12, 2014, 07:02:55 PM »
Will do!