Classes and subclasses typecasting in unityscript

Hi !

Code first, so i don’t get lost into useless specifications:

class mydevice
{
var integrity:float=100;
var deviceName:String;
}

class battery extends mydevice
{
var storage_power:float
}

class generator extends mydevice
{
var recharge_rate:float=25;
}

Now, since my goal is to add mixed types of device subclasses (batteries and generators) into a list, in order to compute, for example, the sum of storage_power or recharge_rate

And I succeded doing that, creating a list of type “mydevice”. But this way, i can only see in the inspector, the variables belonging to the “mydevice” class, but not the subclass variables (and that definitively makes sense).

My unelegant solution was to put an enum “tagging” the mydevice, and checking for it’s original subclass

	for(var i=0;i<ship_devices.Count;i++)
	{
	if ((ship_devices*.type == ShipDeviceType.Battery))*
  •  {*
    

var typecast = ship_devices as battery;
_
print("this is a battery, with storage = "+typecast.storage);
_
* }*
* }*
I’d like to know if there is a better method than this, which by the way surprisingly works in retrieving the subclass variables.
Basically what i’m looking for, is being able to add different derived subclasses to a single list, without “flatting” them to the class they extend, without having to tag them or put enums just to mark a difference between them.
Thank you and have a good day !

There isn’t a single techincal solution to design questions, but here goes my attempt…

The whole point in inheritance is that you can use multiple different types as an instance of their super class

The simplest solution would be to not derive any classes from the Device class, instead have an enum field within that class which separates the objects. Doing it this way will bite you in the arse if you end up having a lot of devices that have different variables

public enum Device_Type{
    Battery,
    Generator
}

public class Device{

    var deviceType : Device_Type;
    var storage_power : float = 0.0;
    var recharge_rate : float = 0.0;
    var integrity : float = 100;
}

The second option that came to me off the top of my head is to use virtual methods… which in UnityScript means just having methods with the same name.

    class MyDevice
    {
        private var integrity:float=100;
        var deviceName:String;

        function GetStoragePower( ) : float { return 0; }
        function GetRechargeRate( ) : float { return 0; }
        function GetIntegrity() : float { return integrity; }
    }
     
    class Battery extends mydevice
    {
        private var storage_power:float
        public function GetStoragePower(){ return storage_power; }
    }
     
    class Generator extends mydevice
    {
        private var recharge_rate:float=25;
        public function GetRechargeRate(){ return recharge_rate; }
    }

The reason you can only see it like that in the inspector is because the inspector instantiates variables for you. Because of that, it has no concept of the inherited values. However, it should be noted that if the base class inherits from MonoBehaviour, then it will not create new variables for you, and instead give you a reference that you can point to an object. This will allow you to point it to any child object of the base class. The downside here is that your scripts have to be defined on other objects in the scene, rather than straight into the inspector, but generally, this is a neater approach.

Thanks Jamora for your design ideas.
Since my original goal was adding all my subclasses to a list of type superclass, and be able to tell in the less expensive way, which subclass an object belongs to, here’s what I ended up using

	if (ship_device instanceof generator)
		{
		print(ship_devices+" is a generator");
		}

Thank you for your contribution.