JS Class question (Automatic variable calculation)

Hi,
I’m pretty new to unity and scripting in general, the problem I’m currently facing is having a class automatically calculate a variables value for me.

class Weapon_Info {
	var Fire_Rate : float;					     //Time between shots (in seconds)
	var DPS : float;						        //Damage per second
	var Damage : float = (DPS * Fire_Rate;)	// <- This is where I'm not sure	
}

class Weapons {
	var Basic : Weapon_Info; 	
	var Canon : Weapon_Info; 
}

var weapon : Weapons;

function Start(){
	print(weapon.Basic.Fire_Rate);
	print(weapon.Basic.DPS);
	print(weapon.Basic.Damage);
}

A sample of my current code.


What it looks like in my editor

At the moment function Start() will print out:

0.5
10
0    //this should equal (10 * 0.5 = 5)

So its not calculating the Damage correctly.

What would be the best way for me to accomplish the automatic calculation of ‘Damage’, if it could somehow update on the fly inside the editor that would be even better.

Thanks in advance.

Your best option is to simply make an accessor function:

class Weapon_Info {
   var Fire_Rate : float;                    //Time between shots (in seconds)
   var DPS : float;                          //Damage per second
  
   function GetDamage()
   {
       return (DPS * Fire_Rate);
   }
}

It won’t show in the inspector (without a custom editor GUI, which is a bit of a pain).

Would I then find the weapon ‘Damage’ by using

weapon.Basic.Damage();instead ofweapon.Basic.Damage;?

no, GetDamage()

Ah yep, of course. Thats what I meant to write.

Excellent.

Thank-you both for the help and quick replies, much appreciated. :smile:

[edit: they solved it!]