- Home /
Construct class with enum parameter (javascript)
I declare my enum and my class constructor to take an int and an enum as parameters. Then i try to construct the class and I get the message:
The type 'ItemStackStruct' does not have a visible constructor that matches the argument list '(int, ItemType)'.
It works fine when I change the constructor to just take an int! What's wrong with this code?
enum ItemType
{
DIRT,
SEED,
MAX_ITEMTYPES
};
class ItemStackStruct extends System.ValueType
{
//fields
var type : ItemType;
var amount : int;
//Constructor
public function ItemStackStruct(amount:int, type:ItemType )
{
this.type = type;
this.amount = amount;
}
}
.. Later:
inventory = new ItemStackStruct[8];
inventory[0] = new ItemStackStruct(32, ItemType.DIRT);
EDIT: Also, I should mention that the creation of the class is done in a different script. It's not something stupid to do with compile order or something?
If it were C#, I could help you here- I can't see any problems with it, but I can only assume that it is something to do with the exact syntax. I can't see a problem with this code! $$anonymous$$aybe you need to remove the 'function' from between the public and the ItemStackStruct? In C# that would be how one would define a constructor, but I don't know JS well enough to be sure.
Ok for whatever reason, using "public" in front of the constructor causes this weird error, removing it seems to have fixed it for now.
I would be grateful if any javascript experts could explain whether using "public" in front of a constructor is good or bad practice and what it actually does, since many examples in the forums use that syntax.
I'm not sure why it causes problems because functions are public by default in UnityScript (where they're private by default in C#).
You might get some hints if you use #pragma strict on the top of your document, try it out if you aren't already doing it.