- Home /
Check if values exists in an int[,] array
I am writing code to check if an object can be placed on a grid.
int[,] floorMap;
public Vector2[] objectMap;
int floorWidth = 30;
int floorHeight = 30;
void GenerateMap() {
for (int x = 0; x < floorWidth; x++) {
for(int y = 0; y < floorHeight; y++) {
floorMap[x,y] = 0;
}
}
}
public bool CheckSafePlace(int offsetX, int offsetY) {
bool allowPlacement = true;
for(int i = 0; i < objectMap.Length; i++) {
// Check each position the object takes up and reference it against the offset to determine if there is a floorMap value of 1 - which would not allow placement. Then exit the loop.
}
return allowPlacement;
}
I can't work out what to put in the commented out line to get this to work.
The the reason for the object map is that an object may is built out of up to a 5x5 arrangement of cubes. These can be any shape. I am updating the floor$$anonymous$$ap (displayed in game by an arrangement of 30x30 cubes). I want to place the objects on top of the floor and then flag the spots it takes up in floor$$anonymous$$ap with a 1. So that no other objects can be dropped there and display to the user it's not a valid place anymore.
If there is a more effective way of doing this I am very much open to suggestions.
Here is some information on arrays :
1D array : https://www.dotnetperls.com/int-array
2D array : https://www.dotnetperls.com/2d
Try and imagine your floor tiles AS the 2D array :
// 0 1 2 3
// +---+---+---+---+
// 0 | | F | | |
// +---+---+---+---+
// 1 | | | | |
// +---+---+---+---+
// 2 | | | | |
// +---+---+---+---+
// 3 | | | G | |
// +---+---+---+---+
F is located in position (1,0) of the array, and
G is located in position (2,3) of the array
So let's do this in code!
First, declare the variable :
floor$$anonymous$$ap = new int[ floorWidth, floorHeight ];
By default all values in the int array will be set to zero.
Then, to check the value at a specific position in the array :
int myValue = floor$$anonymous$$ap[ offsetX, offsetY ];
Then simply check
if ( myValue == 0 ) {
// this tile is free \o/
DoObjectPlacement();
// don't forget to set the value in the array now it has been used
floor$$anonymous$$ap[ offsetX, offsetY ] = 1;
}
I cannot guess what you are trying to do with the object$$anonymous$$ap loop, so I cannot help further. Good Luck =]
p.s. here's another good resource on arrays/collections : http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?#2D_Array
agh, just saw your comment about the object$$anonymous$$ap. (been away from UA for quite a while, didn't see the comments collapse)
I'll assume the object has already been picked, and you know how many tiles the object will take up. Here is a really basic method, and assumes the object will be aligned from the top-left, not the center. Totally untested, but hopefully get you started on how to implement this check yourself :
public bool CheckSafePlace(int offsetX, int offsetY) {
// object has been chosen, and the space object uses is known
int objectWidth = 2;
int objectHeight = 2;
// first check if the object will be out-of-bounds
if ( offsetX + objectWidth > floorWidth || offsetY + objectHeight > floorHeight ) {
return false;
}
// now check each tile the object will occupy
for (int x = 0; x < objectWidth; x++) {
for (int y = 0; y < objectHeight; y++) {
if ( floor$$anonymous$$ap[ offsetX + x, offsetY + y ] != 0 )
{
return false;
}
}
}
return true;
}
The object might be in a T shape for example. This is why I moved to an object$$anonymous$$ap as a Vector2 array so that I can have say the following values:
Vector2(0,0)
Vector2(1,0)
Vector2(2,0)
Vector2(1,0)
Vector2(1,1)
Vector2(1,2)
So then I just loop through the array of Vector2's to check if the space below each off them offset by the currently highlighted tile and see if they are already taken up.
Your comment led me to this solution:
public Vector2[] object$$anonymous$$ap; // This is set on the prefab of the object. I set the values to the offset of each part of it and what tile it will cover. Eg. Vector2(0,0), Vector2(1,0), Vector2(2,0),Vector2(1,1),Vector2(1,2) makes a T shape
public bool CheckPlacement() {
bool result = true;
// room$$anonymous$$anager.floor$$anonymous$$ap is an int[30,30] where 30 is the room$$anonymous$$anager.roomSize in tiles
int[,] floor$$anonymous$$ap = room$$anonymous$$anager.floor$$anonymous$$ap;
foreach(Vector2 objectPoint in object$$anonymous$$ap) {
int xPoint = $$anonymous$$athf.FloorToInt(objectPoint.x) + interaction$$anonymous$$anager.currentX;
int yPoint = $$anonymous$$athf.FloorToInt(objectPoint.y) + interaction$$anonymous$$anager.currentY;
if((xPoint >= 0 && xPoint < room$$anonymous$$anager.roomSize) && (yPoint >= 0 && yPoint < room$$anonymous$$anager.roomSize)) {
if(floor$$anonymous$$ap[xPoint,yPoint] != 0) {
result = false;
}
} else {
result = false;
}
}
return result;
}
Could you please post this as an answer so I may select it as the correct answer.
@alucardj Better not go again or Imma gonna catchya ;)
haha, depends on if everyone has forgotten me and/or my rep has reset...
Answer by seandolan · Mar 22, 2018 at 06:53 PM
public Vector2[] objectMap;
// This is set on the prefab of the object. I set the values to the offset of each part of it and what tile it will cover. Eg. Vector2(0,0), Vector2(1,0), Vector2(2,0),Vector2(1,1),Vector2(1,2) makes a T shape
public bool CheckPlacement() {
bool result = true;
// roomManager.floorMap is an int[30,30] where 30 is the roomManager.roomSize in tiles
int[,] floorMap = roomManager.floorMap;
foreach(Vector2 objectPoint in objectMap) {
// interactionManager.currentX and interactionManager.currentY are the x and y int that my mouse is pointing to via a raycast
int xPoint = Mathf.FloorToInt(objectPoint.x) + interactionManager.currentX;
int yPoint = Mathf.FloorToInt(objectPoint.y) + interactionManager.currentY;
if((xPoint >= 0 && xPoint < roomManager.roomSize) && (yPoint >= 0 && yPoint < roomManager.roomSize)) {
if(floorMap[xPoint,yPoint] != 0) {
result = false;
}
} else {
result = false;
}
}
return result;
}
Answer by Rugbug_Redfern · Mar 22, 2018 at 01:55 AM
int[,] floorMap;
public Vector2[] objectMap;
int floorWidth = 30;
int floorHeight = 30;
void GenerateMap() {
for (int x = 0; x < floorWidth; x++) {
for(int y = 0; y < floorHeight; y++) {
floorMap[x,y] = 0;
}
}
}
public bool CheckSafePlace(int offsetX, int offsetY) {
bool allowPlacement = floorMap[offsetX, offsetY] != 1;
return allowPlacement;
}
Try that. Not sure why you are using a for loop.
The object being placed may be in like a T shape. So I am using a for loop to check each of the sections of the object by storing its shape in an array.
1 1 1 1 1
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
So I loop through the object and compare each of the spaces it takes up against the tile on the floor beneath it.
What are your variables int offsetX and int offsetY for? I'm confused on what you are using them for. I can see you doing
public bool CheckSafePlace(int checkX, int checkY) {
}
to check if the space is available, but offset?
The are the x and y co-ordinate added by the mouse as you move the object around the grid that objects can be placed on.
Your answer
Follow this Question
Related Questions
Variable values not available or they lost their value 1 Answer
Search an array? 2 Answers
Have a problems with a values 1 Answer
Masking using Vector2 array 0 Answers
pick a random int with the value of 1 from an array 2 Answers