Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Oddity_Tom · Oct 28, 2014 at 11:09 PM · arraysgrid based gamehex

How to return the position of an object in a 2 dimensional array

Hi all.

So, I'm trying to create a functional hex grid for a game. I've successfully managed to create a script which creates the grid itself in the editor (not when the project runs) with instantiate, storing each hex object in a 2 dimensional array like so:

 hex[columnNumber,j] = (GameObject)Instantiate(hexObject, new Vector3((j * offsetRow) + rowAdjust, gridHeight, ((float)columnNumber * offsetColumn)), Quaternion.Euler(90, 0, 0));

The position of each hex in that array relates to its "address"; i.e. hex(0,0) is at the top left, the one below it is hex(0,1), the hex above and to the right of that is hex(1,1), and so on.

Now comes the tricky bit. When a hex is selected at runtime (i.e. using OnMouseDown), I want to be able to assign and pass to each hex object an integer value for how far away those other hexes are from the selected hex; i.e. the hexes directly joining the selected hex would have a value of 1, those one step on a value of 2, and so on. This should also be able to deal with missing/impassable hexes (e.g. hex(2,3) might not exist, which might effect what distance value a given hex gets).

My idea to achieve this (and I'm prepared to be told that there are better ways) is to check an object's position in the array (i.e the address of the hex) and then pass that address to some logic.

So:

  • Will the object retain its position in the array at runtime (given that I've placed it in an array in a editor script), or will that array disappear once the editor script finishes?
    • If so how can I find a given hex's position in each dimension of the array?

    • And then how can I assign a value to each hex in the array denoting its distance?

  • If not then how do I assign the address values to the object when I create it?

This is a learning project, so elegant answers are appreciated as much a functional ones.

Thanks in advance for your help.

P.S. For the sake of completeness, here is functional part of the script that creates the hexes:

  public static void ShowWindow()
     {
         EditorWindow.GetWindow(typeof(createHexGrid), true, "Hex Grid Creator");
     }
 
     void OnGUI()
     {
         GUILayout.Label("Base Hex Unit", EditorStyles.boldLabel);
         hexObject = EditorGUILayout.ObjectField(hexObject, typeof(Object), true);
 
         GUILayout.Label("Grid Settings", EditorStyles.boldLabel);
         numberOfRows = EditorGUILayout.IntField("Number of Rows:", numberOfRows);
         numberOfColumns = EditorGUILayout.IntField("Number of Columns:", numberOfColumns);
         topConvex = EditorGUILayout.Toggle ("Top Convex", topConvex);
         bottomConvex = EditorGUILayout.Toggle("Bottom Convex", bottomConvex);
 
         GUILayout.Label("Offsets", EditorStyles.boldLabel);
         gridHeight = EditorGUILayout.FloatField("Grid Height", gridHeight);
 
 
         if (GUILayout.Button("Create Grid"))
         {
             GameObject gridParent = new GameObject();
             gridParent.name = "Grid";
 
             hexesInOddColumns = numberOfRows - 1 + (topConvex ? 1 : 0) + (bottomConvex ? 1 : 0);
 
 
             for (int i = 0; i < (numberOfColumns / 2); i++)
             {
                 hexColumnCreation(numberOfRows, 2 * i, 0);
                 hexColumnCreation(hexesInOddColumns, (2 * i) + 1, ((offsetRow / 2) - (topConvex ? offsetRow : 0)));
             }
             if (numberOfColumns % 2 != 0)
             {
                 hexColumnCreation(numberOfRows, numberOfColumns - 1, 0);
             }
         }
     }
 
     //--FUNCTION TO CREATE HEXES--//
 
     void hexColumnCreation(int hexesInColumn, int columnNumber, float rowAdjust)
     {
         for (int j = 0; j < hexesInColumn; j++)
         {
             hex[columnNumber,j] = (GameObject)Instantiate(hexObject, new Vector3((j * offsetRow) + rowAdjust, gridHeight, ((float)columnNumber * offsetColumn)), Quaternion.Euler(90, 0, 0));
 
             hex[columnNumber, j].name = string.Format("Hex({0},{1})", columnNumber, j + ((topConvex ? 0 : 1) * (columnNumber % 2)));
 
         }
 
     }
 
 }





Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Oct 29, 2014 at 01:43 AM

Will the object retain its position in the array at runtime (given that I've placed it in an array in a editor script), or will that array disappear once the editor script finishes?

I see no reason why the array would "disappear" during runtime.

If so how can I find a given hex's position in each dimension of the array?

You could try implementing a custom pathfinding solution, or just do it like this: "Walk" through the hex coordinates from the hex1s x position towards the hex2s x position by increasing or decreasing the x value by 1 each step until hex2s x value is reached. then do the same for y. Check each coordinate on the way if it actually contains a hex, and if not, offset the x or y values by one, then go on but remember to reverse the offset again next step if it's possible, and so on. It should work in theory, at least :)

     And then how can I assign a value to each hex in the array denoting its distance?

See above, OR just take add the differences between the x and y values if no pseudo-pathfinding is needed.

 If not then how do I assign the address values to the object when I create it?

I don't know what you mean.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

For loop not running correctly inside start () 1 Answer

Add a copy of a object to Array 1 Answer

transform array in javascript 2 Answers

getting "null" when using array. (javascript) 1 Answer


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