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 SuperBiasedMan · Jan 03, 2015 at 01:31 AM · javascriptfor loop2d arraycell

Checking an expression against 2D array elements behaving erratically

I'm working on a simple rogue-like game where I have a 2D array of integers, where each number corresponds to a different object.

0 - Empty Space

1 - Wall

2 - The player

3 - An enemy

I've set up the system so that when the level starts, the whole array is iterated through and one of these numbers is assigned. I wanted an algorithm to detect if the current cell of the array was a viable location for an enemy. Basically the algorithm would start from the current position and first go up until it hits a wall, and then go left from the current position until it hits the wall. If it didn't find any other enemies in both these lines, then it's a viable location.

But for some reason this system doesn't always work. When I load up a new level there's always some enemies in the same row/column between walls, but based on debug.Log calls I have determined that it prevents enemies spawning in nonviable spots sometimes. I'm kinda baffled about what could be screwing with this, so any help would be much appreciated.

Here's the code:

 function Start () {
     for (var i: int = 0; i < size; i++){
         for (var j: int = 0; j < size; j++){
             if ((calculateWallChance(i,j) + Random.Range(0.0,1.0)) > wallChance){
                 cells[i,j] = 1;        
             } else if ((calculateChaserChance(i,j) + Random.Range(0.0,1.0)) > chaserChance){
                 cells[i,j] = 3;                        
             } else {
                 cells[i,j] = 0;
             }
         }
     }
     cells[1,1] = 2;
 }
 
 function calculateChaserChance(i: int, j: int){
     var curr: int;
 
     //By row 
     for (curr = i; curr > 0; curr--){
         if (cells[curr,j] > 1){
             return -5;
         } else if (cells[curr,j] == 1){
             break;
         }
     }
     
     //By column 
     for (curr = j; curr > 0; curr--){
         if (cells[i,curr] > 1){
             return -5;
         } else if (cells[i,curr] == 1){
             break;
         }
     }
     //If not already returned, return a low chance
     return 0.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

0 Replies

· Add your reply
  • Sort: 

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

25 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

Related Questions

increasing or decreasing i in a for loop depending on value x? 1 Answer

2D array not working- Null Reference Exception: A null value was found where an object instance was required. 1 Answer

BCE0053: Property 'UnityEngine.Component.light' is read only. 1 Answer

JavaScript 3 Arrays questions 1 Answer

OnPointerClick function not triggering 5 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