0

I have class ShapeMap which contains 2D int array shapeMap. in my main script I reference my class as ShapeMap map is there a way I can get value using int value = map[x,y]?

My ShapeMap class is

using UnityEngine;
using System.Collections;
using System;

public class ShapeMap {

private int[,] shapeMap;

public ShapeMap(int width, int height, string seed, int randomFillPercent, int smoothAmount) {

    shapeMap = new int[width, height];

    RandomFillMap (randomFillPercent, seed);

    for (int i = 0; i < smoothAmount; i++) {

        Smooth ();

    }

}

private void RandomFillMap() {

}

private void Smooth() {

}

}

and my Main Script is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapGenerator : MonoBehaviour {

ShapeMap map;

public int width, height, fillPercent, smoothAmount;
public string seed;
public bool useRandomSeed;

private void Awake() {

    if (useRandomSeed) {

        seed = Time.time.ToString ();

    }

    map = new ShapeMap (width, height, seed, fillPercent, smoothAmount);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int value = map [x, y];

            if (value == 1) {

                // Do This

            } else {

                // Do That

            }

        }

    }

}
}

1 Answers1

3

All you have to do is to modify your ShapeMap adding indexer:

public int this[int x, int y]
{
    get { return shapeMap[x, y]; }
    set { shapeMap[x, y] = value; }
}

Try this online


More information about indexers you can find here

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
  • Thank you, I was wondering how to do this and had no idea where to start looking for or what to call it, this is exactly what I wanted. –  Feb 01 '18 at 09:26