Search code examples
c#arraysunity-game-enginemultidimensional-arrayposition

Unity C# Checking if position occupied before spawning element


hello everyone i'm new to c# and i'm trying to make a 2D infinite like game. At this point i created a canvas that holds a panel "map" in witch im spawning cells that are map elements. When clicking on one them the whole map panel moves to the center of the screen (i have a empty object on the center of the screen and it moves the map panel to it) When clicking on a element i spawns 4 more elements around it. the elements are 100x100pixels so lets say i have one at 0,0 the others will spawn at 0,100 0,-100 100,0 and -100,0 (so like a plus shape) Question 1: is there a way for me to check if there is already a object at one of these positions ? so i can stop the creation of a object.

Or should store everything in a array or some kind of db? And every time i try to spawn a element should i check the db there is a element already spawned there?

Question 2: Considering ill have a ton of stored game-data (position of the objects x,y,star names etc) is there a way to do a array in a array in a array like in php and have it accept different types of parameters(string, float, int)?

obj1
like lets say x = -100 y = -100 
array z [x,y,name] = "C92Z"
array z [x,y,visited] = "true"
array z [x,y,celestial_objects] = 4
array z [x,y,spaceports] = 1

obj2
like lets say x = -100 y = 200 
array z [x,y,name] = "C22Z"
array z [x,y,visited] = "false"
array z [x,y,celestial_objects] = 10
array z [x,y,spaceports] = 2

and one more thing. for faster searching though the data (i'm thinking ahead like ~20.000+ objects) should i create 2 arrays 1 for positive x and 1 for negative x and store and search for data in them like that ?

Thank you !


Solution

  • A1: It is long ago that I have used unity 3d but I think it is better when you store it somewhere. I think a list is good because it is easier to add something to it and the performance is better.

    A2: You can use Tupels (good) in C# or store everything in objects and then cast it to the type (not a good idea) For example:

    array Tupel<int, int, string>[]
    

    Then you can access the elements like this:

    array[0].Item1
    array[0].Item2
    array[0].Item3
    

    and so on. In C# 7 you can give the properties of the tupel names. But because you are new to C# I don't think that you need that.