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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by CoreyNW · May 07, 2014 at 01:38 AM · javascriptarrayobjectsobject-oriented-programming

How do you debug arrays of objects?

I'm trying to debug this script to see if it is actually creating an array of cards with real values. I gave the deck a debug function that prints the amount of cards and, supposedly, the card values. With this setup, newDeck.debug() will return

52--singleCard,singleCard,singleCard.....

*Updated code will return error

(138,74): BCE0019: 'cardToString' is not a member of 'Object'.

 #pragma strict
 
 //    Based off of a tutorial by Mike Hall on http://www.brainjar.com/
 
 function Start () {
     var newDeck = new Deck();
 //    newDeck.stack(1);
     newDeck.debug();
 }
 
 function Update () {
     
 }
 
 class singleCard {
 
     var rank : int;
     var suite : String;
     
     var cardRank;
     var cardSuit;
     function singleCard (cardRank, cardSuit) {
         this.rank = cardRank;
         this.suite = cardSuit;
     }
     
     function cardToString () {
 
         switch (this.rank) {
             case 0 :
                 cardRank = "Ace";
                 break;
             case 1 :
                 cardRank = "Two";
                 break;
             case 2 :
                 cardRank = "Three";
                 break;
             case 3 :
                 cardRank = "Four";
                 break;
             case 4 :
                 cardRank = "Five";
                 break;
             case 5 :
                 cardRank = "Six";
                 break;
             case 6 :
                 cardRank = "Seven";
                 break;
             case 7 :
                 cardRank = "Eight";
                 break;
             case 8 :
                 cardRank = "Nine";
                 break;
             case 9 :
                 cardRank = "Ten";
                 break;
             case 10 :
                 cardRank = "Jack";
                 break;
             case 11 :
                 cardRank = "Queen";
                 break;
             case 12 :
                 cardRank = "King";
                 break;
             default :
                 cardRank = null;
                 break;
         }
     
         switch (this.suite) {
             case "C" :
                 cardSuit = "Clubs";
                 break;
             case "D" :
                 cardSuit = "Diamonds";
                 break;
             case "H" :
                 cardSuit = "Hearts";
                 break;
             case "S" :
                 cardSuit = "Spades";
                 break;
             default :
                 cardSuit = null;
                 break;
         }
         
         if (cardRank == null || cardSuit == null) {
             return "Null Card";
         } else {
             return cardRank + " of " + cardSuit;
         }
     }
 }
 
 class Deck {
     
     var cardDeck = new Array();
     var n;
     
     function stack (n : int) {
         var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9",
                         "10", "J", "Q", "K");
         var suits = new Array("C", "D", "H", "S");
         var i : int;
         var j : int;
         var k : int;
         var m : int;
 
         m = ranks.length * suits.length;
 
         // Set array of cards.
 
         this.cardDeck = new Array(n * m);
 
         // Build Array
 
         for (i = 0; i < n; i++){
             for (j = 0; j < suits.length; j++){
                 for (k = 0; k < ranks.length; k++){
                     this.cardDeck[i * m + j * ranks.length + k] = 
                     new singleCard(k, suits[j]);
                 }
             }
         }
     }
     
     function debug () {
     
         var stringDebug : String = "Deck--";
     
         var i : int;
         for (i = 0; i < cardDeck.length; i++){
             stringDebug = stringDebug + " || " + cardDeck[i].cardToString();
         }
         Debug.Log(stringDebug);
     }
 }
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
2
Best Answer

Answer by robertbu · May 07, 2014 at 03:24 AM

Implement a ToString() function for each card that outputs a single card. Next implement a ToString() function for the deck. It calls the ToString() function for each card in the deck, and builds a string with each entry separated by a "\n". Then when you want to dump the deck, just do:

  Debug.Log(cardDeck.ToString());

Only the first entry or two will appear in the console, but if you click on the dropdown in the upper-right corner of the Console window, you can select "Open Editor Log" and see the full dump. Or you could use the debugger:

http://unitygems.com/debugging-game-monodevelop/

Comment
Add comment · Show 2 · 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 CoreyNW · May 08, 2014 at 12:38 PM 0
Share

I'm trying to have it go through the deck and convert each card to a string and add it to another string variable, but it doesn't want to use cardToString() on the singleCard at index i. I get:

(138,74): BCE0019: 'cardToString' is not a member of 'Object'.

avatar image robertbu · May 08, 2014 at 01:31 PM 0
Share

Don't use the Array class. Use the built-in array ins$$anonymous$$d. The Array class is untyped or slow. The error comes from it being untyped. You can cast the Array entry to solve the problem as well. Something like:

 var st = (cardDeck[i] as singleCard).cardToString();

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

20 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

Related Questions

How to Create Dynamic Multidimensional Array? 1 Answer

Array - insert item in the middle 1 Answer

Error creating 5-dimensional array 1 Answer

Multidimensional array variable size (JS) 1 Answer

Getting a variable from a GameObject inside a 2d array? 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