CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Array Selection

  1. #1
    Join Date
    Mar 2010
    Posts
    15

    Array Selection

    I am currently building a game and my map file contains all the data that specifies the attributes of the game field.

    How do I select a range in an array element? For example I have an array of 768 elements, each element contains a string of information which represents the logic of constructing the map and rules. The first 7 characters represent the tile png, the 8th character determines if that tile is passable, etc, etc.

    What method can I use to select only certain number of characters like a starting point to an ending point? Or would I want to convert to string and truncate it down?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Array Selection

    That would be the String.SubString method. However, I would note that this is a terrible design and is going to lead to a maintenance nightmare. Why don't you instead define a class that exposes properties which have to do with the game board, i.e.,

    Code:
    class Tile
    {
        public string ImageFileName { get; }
        public bool Passable( int x, int y ) { ... }
        // etc.
    }
    Now you can just maintain a 2d array of "Tile" objects and serialize them to disk when saving. So much nicer and so much more maintainable/extensible than dealing with raw strings everywhere.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  3. #3
    Join Date
    Mar 2010
    Posts
    15

    Re: Array Selection

    I think I explained it wrong.

    When I read in the file initially I store it in one array as raw data, I then proceed to cut up the large string of data into separate arrays (although now I think I could probably use a multidimensional array or something). Those arrays or that one array is where all the information is stored for the game board.

    Although I am liking this class idea.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Array Selection

    Yes, I got it, but I would still recommend using classes. .NET has built in serialization methods, so it is very easy to write them to disk and read them back again as complete objects, no parsing required.

    I suggested 2d array (or a List of Lists) because I assume they will be laid out on a map. It makes indexing much easier for you. You cold also create your own collection class, "TileMap" or something.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured