Is it possible to use a switch inside a class? (JavaScript)

I am trying to make a item-system to expand my knowledge a bit.
But I wonder if something like this is possible, if so, how?
You see, I want to hide unnecessary variables for the user what he adds all the items in the array of items.

enum Type { Weapon=0, Shield=1, Armor=2, Consumable=4 }
enum wepType { Sword=0, Axe=1, Staff=2 }
enum armType { Light=0, Medium=1, Heavy=2 }

var Items : getItems[];
class getItems {
    var Name : String;
    var ID : int;
    var Model : Transform;
    var Icon : Texture;
    var itemType : Type = Type.Weapon;

    switch(itemType) {
        case Weapon:
            var weaponType : wepType = wepType.Sword;
            var damage : int;
        break;

        case Shield:
            var blockAmount : int;
        break;

        case Armor:
            var armorType : armType = armType.Light;
            var defence : int;
        break;
		
        case Consumable:
            var stackAble : boolean;
            var maxAmount : int = 10;
        break;
    }
}

So my question was, is this possible in some way?
Or should i just create a class for each item type and make it read from that:

enum Type { Weapon=0, Shield=1, Armor=2, Consumable=4 }
enum wepType { Sword=0, Axe=1, Staff=2 }
enum armType { Light=0, Medium=1, Heavy=2 }

var Items : getItems[];
class getItems {
    var Name : String;
    var ID : int;
    var Model : Transform;
    var Icon : Texture;
    var itemType : Type = Type.Weapon;
    var Weapon : getWeapon;
    var Shield : getShield;
    var Armor : getArmor;
    var Consumable : getConsumable;
}

class getWeapon {
    var weaponType : wepType = wepType.Sword;
    var damage : int;
}
class getShield {
    var blockAmount : int;
}
class getArmor {
    var armorType : armType = armType.Light;
    var defence : int;
}
class getConsumable {
    var stackAble : boolean;
    var maxAmount : int = 10;
}

You could inherit:

class Item {
  var value : int;
 function Use() {}
 function AddToInventory() {}
 function Drop() {}
}

class Weapon extends Item {
 var damage : int;
}

Weapon has the Use, AddToInventory, and Drop functions and the value variable in addition to the damage variable. Item objects don’t have the damage variable, but have the rest.

The point was to only show the needed variables inside the inspector as if:
Lets say i choose

I want to see weapon variables and hide shield, armor and consumable.

Is this possible?
since I want to be able to adjust variables inside my item array.

ATM I have all classes in the array, but only use 1 for each weapon, would look better if the other where hidden (since i am not the one who will create/add items in the array and want it to look cleaner

Edit.
I don’t really understand why and how you use functions inside a class, never done before.
If you have some time, maybe add a short note? =)
Will try to look up how exactly you meant and see if it helps.

There’s not a way to choose which vars show up based on a variable automagically; you’ve got to script it in. It’s not too terribly nasty, though. :slight_smile:

You could hide optional vars and write a custom Inspector to only display them when you want -

 class Item {
var itemType : ItemType;
@HideInInspector
var damageAmount : int;
@HideInInspector
var armorDensity : int;
}
function OnInspectorGUI() {
  DrawDefaultInspector();
  if ( target.itemType == ItemType.Weapon ) {
   target.damageAmount = EditorGUILayout.IntField( target.damageAmount );
 } // etc
}

I’m not sure there’s an IntField function but that’s the general idea. You hide the vars that only show up sometimes, draw all the others, and draw the remaining ones only if their condition is met.

Class functions -

A function in a class is used to keep things object-oriented - meaning, easier to understand and easier to debug. The main concept of object-oriented is that nothing may modify an object except that object. You call one of the object’s functions, and the object determines whether or not you’re being sane.

Rather than having -

player.health -= 5;
if ( player.armor > 2 ) player.health += 1;

You’d have the much nicer-to-read -

player.DealDamage( 5 );

Then the player could determine how much of that 5 it wants to deal with -

function DealDamage( int v ) {
  v -= armor;
  v *= weakness;
}

There will always be multiple different things dealing damage to the player. If at any point in the development process you change how damage is dealt or what can affect the total damage (which WILL happen ;)), you only have to change the DealDamage function and everything will automagically work. If you used the non-function-calling way instead, you’ll have to search for every single use of player.health, and rewrite many pieces of code to work with the new functionality - a highly error-prone process.

Thanks for the tip about making functions like that, I recently started making functions like that for example for GUIButtons and other to shorten the code instead of having the same thing multiple times.

Also, didn’t know of the editor classes, thanks for that too, you made my day, and probably upcoming week, now i have something new to do.