- Home /
Trying to create an class array with two properties
Hi:
I'm trying to create a javascript (not builtin) array that has two properties. The variable arrays are called p1 and p2. So I'm trying to add values to the properties in the array. However, when I'm adding values (num and suit) to the array (using push) I get the following error message:
MissingMethodException: card[].Push
Here's the following code. I'm sorry if I'm not being clear. If you have any questions, please ask. I'm trying to get this simple code to execute first before school tomorrow, so I'll be checking this page often.
Thanks!
//create number and suit classes for p1 and p2.
class card {
var num;
var suit;
function card (n,s) {
num = n;
suit = s;
}
}
var p1 : card[];
var p2 : card[];
//nu is number and su is suit.
var nu;
var su;
function shuffle (nu,su) {
p1.Push(nu,su);
}
function Start () {
Debug.Log ("shuffle start");
shuffle(1,"S");
Debug.Log ("shuffle end");
Debug.Log (p1.suit);
}
You need to define all variable types. "var num" isn't right; you need to use "var num : int" or whatever the type is, same in the function declaration. The convention is that classes are upper-case, variables are lower case. Don't use variable names like "nu"...just write them out; you don't gain anything by having obscure variable names except difficult-to-read code.
You aren't creating any arrays, you're just creating variable to hold arrays
When you Push you are trying to add two variables, not a single 'card'. $$anonymous$$aybe you mean to create a new card with 'nu' and 'su' and add that?
Variables need types (as mentioned by Eric5h5).
If you use arrays you need to specify their size on creation. If you don't know their size when you create the array you probably want a List (as described by rutter)
You can't reference an array as if it was an instance of the type it stores, so p1.suit will fail. You need to extract the object then use it... to get the first item use p1[0].
I'm sure there are more issues with your code but that's what I picked up with a quick parse. The overriding impression I get is: You need to spend some time learning to code properly first. It will be worth it in the long run (and even in the short term).
Thanks! I've defined the variables in the program.
I'm new to coding, and that is all I have in regards of coding because I want to solve this issue first.
I basically want to create two (p1 and p2) list variables (because their sizes will change) that will hold 2 properties (the number of the card and the suit of the card). I also need the 2 properties tied together, since they're suppose to be one card. The lists will grow as the cards are being "shuffled" and "given" to the player
So my questions are: 1. I read the wiki page that rutter sent to me, and I think I want the ArrayList or the 2D array. Which one would work, or does neither works for these purposes? 2. How can I add an item (with the two variables) to that list or 2D array?
Sorry for my lack of understanding and n00b-ness. I'm a type of "learn as I do" person, so please (pretty please) help me out!
Please see below for the updated code:
Thanks!
class card {
var num : int;
var suit : String;
function card (n : int, s : String) {
num = n;
suit = s;
}
}
var p1 : card[];
var p2 : card[];
function shuffle (n : int, s : String) {
p1.card (n, s);
}
function Start () {
Debug.Log ("shuffle start");
shuffle (1,"S");
Debug.Log ("shuffle end");
var firstCard : p1[0];
var firstSuit : String = firstCard.suit;
Debug.Log (firstSuit);
}
Answer by rutter · Nov 09, 2013 at 01:07 AM
A card[]
has fixed length. If you want a structure with dynamic length, you might instead use a List.
Handy reading on the Unity wiki, including sample code: Which Kind Of Array Or Collection Should I Use?
Hi:
Thanks for the recommendation! With an ArrayList; how will I declare the ArrayList variable as a class?
Thanks!