Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by seandolan · Mar 21, 2018 at 10:48 PM · arrayintvector2values

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image seandolan · Mar 21, 2018 at 11:21 PM 0
Share

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.

avatar image seandolan · Mar 21, 2018 at 11:22 PM 0
Share

If there is a more effective way of doing this I am very much open to suggestions.

avatar image AlucardJay · Mar 22, 2018 at 01:59 AM 0
Share

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

avatar image AlucardJay · Mar 22, 2018 at 02:37 AM 0
Share

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;
 }
avatar image seandolan AlucardJay · Mar 22, 2018 at 04:08 AM 0
Share

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.

avatar image seandolan AlucardJay · Mar 22, 2018 at 06:49 PM 0
Share

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.

avatar image meat5000 ♦ AlucardJay · Apr 08, 2018 at 05:39 PM 0
Share

@alucardj Better not go again or Imma gonna catchya ;)

avatar image AlucardJay meat5000 ♦ · Apr 10, 2018 at 09:32 AM 0
Share

haha, depends on if everyone has forgotten me and/or my rep has reset...

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image seandolan · Mar 22, 2018 at 04:03 AM 0
Share

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.

avatar image Rugbug_Redfern seandolan · Mar 22, 2018 at 02:12 PM 0
Share

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?

avatar image seandolan Rugbug_Redfern · Mar 22, 2018 at 03:59 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges