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 /
  • Help Room /
avatar image
0
Question by domigue · Jan 21, 2017 at 06:52 AM · minecraftstackoverflowneighbour

stackoverflow, minecraft terrain, finding neightbours

So... I have map like minecraft, but im making three dimension strategy game. Game start and generate map. Then player can change where he want his base. When player set his first base, its meant that the game will make area around base flat. And there is a problem. :)

 public World w;
 public bool CanGoDown;
 public int UpLevel = 0;

There is code variables. -World w is base code of world. -I will explain other later.

 public Vector3[] GetBlockNeighbours(int x, int y, int z, bool OnlyUp){
 
         List<Vector3> delete = new List<Vector3> ();
 
         if (!OnlyUp) {
             if (w.GetBlock (x, y - 1, z - 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x - 1, y - 1, z - 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x - 1, y - 1, z).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x - 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x, y - 1, z + 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x + 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x + 1, y - 1, z).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x + 1, y - 1, z - 1).GetType () == typeof(BlockAir)) {
 
                 if (CanGoDown == false) {
                     return delete.ToArray ();
                 }
 
                 Vector3[] pos = MakeNewRequest (x, y - 1, z, false);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 delete.Add (new Vector3 (x, y, z));
 
             } else

This is the part game call from World script! So... If there is no ground next where player want but his base the code will go one level down. If it go down Code will call MakeNewRequest(...)...

 Vector3[] MakeNewRequest(int x, int y, int z, bool OnlyUp){
         GrindMapBase g = new GrindMapBase ();
         Vector3[] pos;
         g.w = w;
         g.CanGoDown = false;
         if (OnlyUp)
             g.UpLevel += 1;
         if (UpLevel < 4)
             pos = g.GetBlockNeighbours (x, y, z, OnlyUp);
         else
             pos = new Vector3[0];
 
         return pos;
 
     }


So now CanGoDown is false, and next time if code find no ground next to base, code return delete.ToArray(), And i think the delete will be empty. UpLevel wont yeat rise, becouse (bool) OnlyUp was false. If code find ground next to base, code will skip going down part and it start really finding pieces next to it.

 else {
                 if (w.GetBlock (x, y, z - 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x, y, z - 1));
                 }
                 Vector3[] pos = MakeNewRequest (x, y, z - 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x - 1, y, z - 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x - 1, y, z - 1));
                 }
                 pos = MakeNewRequest (x - 1, y, z - 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }

Now every time code find neighbour code will add it in to delete.(). After that code will call MakeNewRequest(...), but this time OnlyUp(boolean) is true.

So now UpLevel int will grow and MakeNewRequest just cancel making loop when UpLevel is 3..

And end of Code

 return delete.ToArray ();



Stackoverflow error say error is when i call GetBlockNeighbours from MakeNewRequest.

And sorry my english skills :)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GrindMapBase{
 
     public World w;
     public bool CanGoDown;
     public int UpLevel = 0;
 
     Vector3[] MakeNewRequest(int x, int y, int z, bool OnlyUp){
         GrindMapBase g = new GrindMapBase ();
         Vector3[] pos;
         g.w = w;
         g.CanGoDown = false;
         if (OnlyUp)
             g.UpLevel += 1;
         if (UpLevel < 4)
             pos = g.GetBlockNeighbours (x, y, z, OnlyUp);
         else
             pos = new Vector3[0];
 
         return pos;
 
     }
 
     public Vector3[] GetBlockNeighbours(int x, int y, int z, bool OnlyUp){
 
         List<Vector3> delete = new List<Vector3> ();
 
         if (!OnlyUp) {
             if (w.GetBlock (x, y - 1, z - 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x - 1, y - 1, z - 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x - 1, y - 1, z).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x - 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x, y - 1, z + 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x + 1, y - 1, z + 1).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x + 1, y - 1, z).GetType () == typeof(BlockAir) ||
                 w.GetBlock (x + 1, y - 1, z - 1).GetType () == typeof(BlockAir)) {
 
                 if (CanGoDown == false) {
                     return delete.ToArray ();
                 }
 
                 Vector3[] pos = MakeNewRequest (x, y - 1, z, false);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 delete.Add (new Vector3 (x, y, z));
 
             } else {
                 if (w.GetBlock (x, y, z - 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x, y, z - 1));
                 }
                 Vector3[] pos = MakeNewRequest (x, y, z - 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x - 1, y, z - 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x - 1, y, z - 1));
                 }
                 pos = MakeNewRequest (x - 1, y, z - 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x - 1, y, z).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x - 1, y, z));
                 }
                 pos = MakeNewRequest (x - 1, y, z, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x - 1, y, z + 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x - 1, y, z + 1));
                 }
                 pos = MakeNewRequest (x - 1, y, z + 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x, y, z + 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x, y, z + 1));
                 }
                 pos = MakeNewRequest (x, y, z + 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x + 1, y, z + 1).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x + 1, y, z + 1));
                 }
                 pos = MakeNewRequest (x + 1, y, z + 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x + 1, y, z).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x + 1, y, z));
                 }
                 pos = MakeNewRequest (x + 1, y, z, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
                 if (w.GetBlock (x + 1, y - 1, z).GetType () != typeof(BlockAir)) {
                     delete.Add (new Vector3 (x + 1, y - 1, z));
                 }
                 pos = MakeNewRequest (x + 1, y, z - 1, true);
                 if (pos.Length != 0) {
                     foreach (Vector3 p in pos) {
                         delete.Add (p);
                     }
                 }
 
             }
         } else {
             if (w.GetBlock (x, y + 1, z).GetType () != typeof(BlockAir)) {
                 delete.Add (new Vector3(x, y + 1, z));
             }
             Vector3[] pos = MakeNewRequest (x, y + 1, z, true);
             if (pos.Length != 0) {
                 foreach (Vector3 p in pos) {
                     delete.Add (p);
                 }
             }
         }
 
         return delete.ToArray ();
     }
 
 }
 
Comment
Add comment · Show 1
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 Cherno · Jan 21, 2017 at 12:09 PM 0
Share

Well... That's a lot of code to read and understand. Have you tried inserting Debug.Log lines to see what happens in certain parts of your code? I see that $$anonymous$$akeNewRequest gets called in GetBlockNeighbours, which itself is called in $$anonymous$$akeNewRequest... $$anonymous$$aybe the stackoverflow error stems from an infinite calling of these methods?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 21, 2017 at 04:46 PM

It's hart do understand what your code acutally does or should do. Flatten the terrain in a certain area is just a couple of loops and block checks which can be done all in one method as long as you know the shape / size of your base. I can't really follow your logic here. If "UpOnly" is false you only check the neighbors that are below the block except the block directly below.

Since you do a recursive search here, you would need a way to remember which blocks you have already checked. Though it doesn't seem you have anything like that.

Also keep in mind that here:

      if (OnlyUp)
          g.UpLevel += 1;
      if (UpLevel < 4)

You "raise" UpLevel in your newly created "GrindMapBase" but you are checking UpLevel from the current GrindMapBase instance. When you create a new GrindMapBase you don't seem to initialize UpLevel, so it would starts with 0 in each new instance you create. If that's the only place where you increase the UpLevel, it will never be greater than "1" in each GrindMapBase instance.

Since we don't really understand what this code should do in the end i would suggest you rethink the task you're trying to complete here.

Apart from that you should always name your methods after what they actually do. GetBlockNeighbours does not simply return the neighbors of the block you pass as parameter but returns other blocks as well since you do a recursive search through the neighbors as well. So the method name is misleading. "MakeNewRequest" is also a bad name since it doesn't say anything about want it actually does. It's like naming it "Do()" or "A()" ^^. Most IDEs have refactoring tools for renaming methods easily. It's often the case that you have to rename a method or class a couple of times as it evolves when the concept isn't clear from the start.

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

6 People are following this question.

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

Related Questions

Why does my Chunk generator reset the vertex positions? 1 Answer

Detecting neighbour particles in the particle system 0 Answers

Stack Overflow error in recursive method 0 Answers

wierd lines appear when making a minecraft-like game 0 Answers

Stack Overflow error GetComponent 0 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