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 shadowsaint669 · Jun 11, 2012 at 03:49 PM · c#gridfor loop

Setting up an 2d grid in C#

Here is the basic situation. I want to set up a 2d grid format for a tactical RPG so I have a coordinate system. My intent is to input into a 2d 10x12 array that I have passing fine a position with in that array, for example player is located at 3,3. From there I take the movement range of the player, in this example 6 (the value will always be a number divisible by 2) and draw a diamond shape around the player based off that number.

Anyone who has played a tactical RPG is familiar with this system.

My trouble is coming in when trying to figure out the best way to build a temporary array that I am going to use to change the tiles textures to make it so the player can see this movement area.

I can set up an array just fine that has the right amount of length in the 1 position of the array.

For example:

for (int i = 0; i < moveDistance; i++)

{

tempArray[i]= new int[k]{?};

}

This will obviously draw the array fine for the [i] position.

What I need is for k to increase from 1 to my moveDistance-1 and back down to 1.

So my arrays would look something like:

tempArray[0] = [1] {yPosition};

tempArray[1] = [3] {(yPosition-1),yPosition,(yPosition+1)};

tempArray[2] = [5] {(yPosition-2),(yPosition-1),yPosition,(yPosition+1),(yPosition+2);

tempArray[3] = [3] {(yPosition-1),yPosition,(yPosition+1)};

tempArray[4] = [1] {yPosition};

For further detail if our player was sitting at position 3,3 my temp array would look like:

tempArray[0] = [1] {3};

tempArray[1] = [3] {2,3,4};

tempArray[2] = [5] {1,2,3,4,5};

tempArray[3] = [3] {2,3,4};

tempArray[4] = [1] {3};

I realize this will just return the Y coordinates for the movement. But it is a start for me.

Sorry for being so long winded. I can visualize and understand the logic behind how to do this I just don't know how really to set up the conditional case where K will increase by K+2 until it reaches moveDistance-1 and then start going K-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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Datael · Jun 11, 2012 at 04:22 PM

You seem to already be on the right track so I'm going to assume that you don't need that much help so instead of a complete answer I'm going to give this to you as a suggestion of where to start:

 for (int x = 0; x <= moveDistance; x++) {
     for (int y = 0; y <= moveDistance - x; y++) {
         // let's assume that the function Position represents a point on your map
         Position(x,y);
     }
 }

By taking the current value of x from moveDistance, this limits the value that y can be, limiting the distance from (0, 0)

In the case that moveDistance is 2 this will return this following Positions:

(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (2, 0)

Things to consider:

  • You're going to want negative X values and negative Y values.

  • You probably don't want to list (-0, 0) and (0, 0) and (0, -0) since they are all the same thing. You can do this with an if statement.

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 shadowsaint669 · Jun 11, 2012 at 04:36 PM 0
Share

I understand the logic of how to do the system.

I don't know how to set up a for loop where the int k increases up to a certain point then decreases from there.

avatar image shadowsaint669 · Jun 11, 2012 at 04:45 PM 0
Share

Actually I figured that out. Never$$anonymous$$d.

avatar image Datael · Jun 11, 2012 at 04:46 PM 0
Share

It's not possible to do a for loop where a number increases and then decreases unless you use an interim variable. Is there a reason you want something other than positions relative to the current position to which the player can move? The positions you get from the double for loop that I suggested (plus the negated versions) should be all you need to work out where the player can move

avatar image
0

Answer by shadowsaint669 · Jun 11, 2012 at 04:44 PM

I think I am going to go about this in a different manner.

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 ppiatek · Oct 22, 2014 at 11:40 PM

It's old post, but maybe it will be helpful for somebody. This code draws diamond on grid in specific position.


int x = (int)position.x;
int y = (int)position.y;
int range = 5;
for( int i = 1; i <= range; i++ )
{
    for( int j = 0; j < i; j++ )
    {
        if( j == 0 ) {
            tab[ x - j, y + i - j ] = 1;
            tab[ x - j, y - i + j ] = 1;
            tab[ x + i - j, y + j ] = 1;
            tab[ x - i + j, y + j ] = 1;
        } else {
            tab[ x - j, y + i - j ] = 1;
            tab[ x - j, y - i + j ] = 1;
            tab[ x + j, y + i - j ] = 1;
            tab[ x + j, y - i + j ] = 1;
        }
    }
}

I hope there are no bugs. If so, write it below where they are.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I need to get a Vector3 of the mouse position while i'm editing in the scene view. 2 Answers

How to Save an multidimensional array trough editor script 1 Answer

2-Dimensional Array Error [CLOSED] 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