[JavaScript] Proper class declaration syntax?

Hello everybody,

I’m a Java programmer and I’d like to use JavaScript/UnityScript in Unity. I know, there ARE some differences (Java != JavaScript), but the syntax seemed simillar when I had a look at some tutorials.

However, none of the tutorials I came across ever covered the topic of properly declaring a class in Unity. So I tried to do it myself, here’s the result:

public class Card {

   public enum Category { CREATURE, EQUIP, ASSIST, ATTACK, TECH, DEFENSE }

   // field declarations
   protected var cardName : String;
   protected var cardCategory : Category; 

   // constructor declaration
   public function Card(cardName : String, cardCategory : Category){
   	 this.cardName = cardName;
   	 this.cardCategory = cardCategory;
   }

   // public method declaration
   public function toString() : String {
      return "Card '" + this.cardName + "' [" + this.cardCategory + "]";
   }

}

That would be my class declaration. I assigned the following script to an object in my scene:

var tempCard : Card = new Card("SomeCardName", Card.Category.CREATURE);

function Update () {
   print(tempCard.toString());
}

… which produced the correct output on the Unity console. So basically - it works.

However, in MonoDevelop, I have no code completion for neither fields/attributes nor functions/methods for the “Card” class - which tells me that apparently I did something wrong (or at least not the way it is supposed to be).

I’d really appreciate it if somebody could tell me if I made a syntax mistake or did something “unusual” in my class declaration above.

Thanks,

Alan47

Nope, it’s perfect. (Well, personally I’d use “ToString” instead of “toString” to make it consistent with other classes.) It’s probably MonoDevelop.

–Eric

Hi,

thanks, Eric :slight_smile: But why does MonoDevelop then refuse to show me any code completion for this class? I do have code completion for Unity default classes… is the feature simply not implemented for user-defined classes? In that case, can you advise me a good UnityScript editor which does have this feature?

Thanks,

Alan