- Home /
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);
}
}
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:
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'.
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();