4 Replies - 287 Views - Last Post: 21 July 2013 - 08:32 PM Rate Topic: -----

#1 Sleete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-July 13

Trouble getting array from custom class

Posted 21 July 2013 - 04:35 AM

I made this class to handle generating a "galaxy map" but I can't seem to retreive the int[,] correctly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galactic_Battles
{
    class GalaxyMap
    {
        private int[,] map;
        private void GenerateGalaxyMap() //Generates a random galaxy map (10x10 grid, with 10 ~ 16 systems)
        {
            map = new int[,]{
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; //blank galaxy map

            Random rnd = new Random();
            int systems = rnd.Next(10, 16); //number of systems in the galaxy (10 ~ 16)
            int mapX;
            int mapY;

            for (int i = 0; i < systems; i++) //for each system, make a location
            {
                mapX = rnd.Next(9); //10x10 grid...
                mapY = rnd.Next(9);
                while (map[mapX, mapY] != 1) //used "while" statements in case the rnd made a repeat number
                {
                    map[mapX, mapY] = 1; //turn a blank location to a system
                }
                while (map[mapX, mapY] == 1) //if it already has a system in the location, reroll
                {
                    mapX = rnd.Next(9);
                    mapY = rnd.Next(9);
                }
            }
        }

        public int[,] galaxyMap
        {
            get { return map; }
            set { map = value; }
        }
    }
}



I try to get it from the Draw method of XNA

            spriteBatch.Begin();//draw the galaxy map
            for (int x = 0; x < gMap.galaxyMap.GetLength(1); x++) //error here
            {
                for (int y = 0; y < gMap.galaxyMap.GetLength(0); y++) //and here
                {
                    if (gMap.galaxyMap[x, y] == 0)
                        spriteBatch.Draw(spaceTexture, new Rectangle(x * 64, y * 64, 64, 64), Color.White);
                    if (gMap.galaxyMap[x, y] == 1)
                        spriteBatch.Draw(systemTexture, new Rectangle(x * 64, y * 64, 64, 64), Color.White);
                }
            }
            spriteBatch.End();



I have, as a global variable:
GalaxyMap gMap = new GalaxyMap(); //variable for the galaxy



Object reference not set to an instance of an object.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble getting array from custom class

#2 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Trouble getting array from custom class

Posted 21 July 2013 - 04:41 AM

Your GenerateGalaxyMap method is private and is never called, so the map variable is never initialized. You could perhaps use a constructor to call the method?

As a side note, you can initialize a multidimensional array like so
new int[x,y]
without needing to set all the values. They will all be zero by default.

This post has been edited by Witchking: 21 July 2013 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,508
  • Joined: 08-April 09

Re: Trouble getting array from custom class

Posted 21 July 2013 - 04:46 AM

Put the line 25 outside of the method in order to use only one instance of the random class.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Trouble getting array from custom class

Posted 21 July 2013 - 05:55 PM

View PostSleete, on 21 July 2013 - 04:35 AM, said:

    ...
            int systems = rnd.Next(10, 16); //number of systems in the galaxy (10 ~ 16)
    ...
            mapX = rnd.Next(9); //10x10 grid...


Neither of the comments for these lines is true. Random.Next(x,y) is inclusive x and exclusive y. So your first one is from 10 to 15, and the second is 0 to 8.

This is actually a good thing as it saves you from having to think about array limits (no really). When you define an array of 10 elements, you can select one with Random.Next(10). No need to do math and stuff :)

And don't use magic numbers. Define readonly or constant values for your limits so you only have to change things in one place if you decide to increase/decrease them.

This post has been edited by Momerath: 21 July 2013 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 Sleete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-July 13

Re: Trouble getting array from custom class

Posted 21 July 2013 - 08:32 PM

I figured it out. I moved the blank map out side of the method then made the method public static. Now when I run the program I get a black map with 10 ~ 16 systems on it :)

Thank you everyone ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1