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 jrbailey1977 · Jan 01, 2011 at 03:45 AM · 2darrayscomparison

array and 2d array comparison in javascript problem

I have a function that has an array (String) and 2d array variables (gridMapData and gridMap respectively).

I've spent a lot of time debugging this script tonight and as far as I can tell with exception of performing comparisons on the 2d array as shown below, all other operations I am performing on the 2d array (in this function only assignments and Debug.Log prints).

The below snippet from the script illustrates that a character comparison in a while loop against the array (string) works fine, but I never enter the the if statement when performing a comparison against the 2d array. Performing a Debug.Log shows that gridMap[row,col] in fact == "B".

... // EDIT - I left out how they are declared; gridMapData is actually declared a String, but // I still do not understand why this will not work.

var gridMapData : String = gridMapFile.text; gridMap = new Array (gridSizeRow); gridMap[row] = new Array(gridSizeCol);

... while ((gridMapData[num] == "\n")||(gridMapData[num] == "\r") )

{

// works fine

}

...

if ((gridMap[row][col] == "B"))

{

// never enters

}

...

Debug.Log(gridMap[row][col]); // prints out a "B"

...

Basically, is there anything peculiar about 2d array comparisons in Unity javascript?

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

2 Replies

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

Answer by Eric5h5 · Jan 01, 2011 at 06:07 AM

You should use an actual 2D String array, not dynamic arrays, which require casting and are slow. Better yet, something more efficient than strings, such as chars or ints. You can make a 1D array behave like a 2D array using simple math, which is the most efficient, though actual 2D arrays in Javascript can be initialized with this, which are slightly less efficient but usually easier to understand in code.

Comment
Add comment · Show 1 · 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 jrbailey1977 · Jan 01, 2011 at 05:00 PM 0
Share

Eric, thanks for the advice! I'll put some thought into this for optimization.

avatar image
0

Answer by jrbailey1977 · Jan 01, 2011 at 04:33 AM

My last edit cued a suspicion that I may have to type cast the gridMap[row][col] to a String before doing a comparison to "B".

After trying different methods to type cast (the syntax is a bit counter intuitive to this C/C++ programmer) all resulting in compiler errors, I came across the ToString() member function for the int class. It works for Array too, though I couldn't find it in the reference material.

This may not be the best way to do this, but it works -

if (gridMap[row][col].ToString() == "B")

{

// now enters the statement correctly

}

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

No one has followed this question yet.

Related Questions

2D Animation does not start 1 Answer

Changing images using a shader on a 2D canvas? 0 Answers

How to Integrate a .JSON text array into the program? 1 Answer

[2D] Array Sorting / Sequential Arrays 1 Answer

2D Platformer (Unity 4.3) - How are the AudioClip arrays initialized? 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