- Home /
Finding the length of an enum in a different class
I've started the Bergzerg Arcade tutorial, and just like the 3D Buzz tutorial I am converting it to UnityScript (because like a turtle hiding in his shell I am more comfortable with uJS ). He did his best to bamboozle me with a Delegate-Sort (which I got around with a bubble-sort), and all these classes that extend classes nearly did my head in, I also rewrote the getters and setters the only way I could understand. But finally I have been caught out trying to get the lengths of different enums on these classes.
Here is the C# version :
_primaryAttribute = new Attribute( Enum.GetValues( typeof(AttributeName) ).Length );
and here's what I did after a search ( Eric is there with master smarts as always : http://answers.unity3d.com/questions/149520/javascript-checking-enum-length.html ) and the conversion :
_primaryAttribute = new Attribute( System.Enum.GetValues( AttributeName ).Length );
Note : I have import System; at the top of the script (apparently this is to access the enum class, but I havn't a clue....)
This is the script I am working on (BaseCharacter):
#pragma strict
import System; // added to access the enum class
public class BaseCharacter extends MonoBehaviour
{
private var _name : String;
private var _level : int;
private var _freeExp : uint;
private var _primaryAttribute : Attribute[];
private var _vital : Vital[];
private var _skill : Skill[];
function Awake()
{
_name = "";
_level = 0;
_freeExp = 0;
//_primaryAttribute = new Attribute( Enum.GetValues( typeof( AttributeName ) ).Length );
_primaryAttribute = new Attribute( System.Enum.GetValues( AttributeName ).Length );
//_vital = new Vital( Enum.GetValues( typeof( VitalName ) ).Length );
_vital = new Vital( System.Enum.GetValues( VitalName ).Length );
//_skill = new Skill( Enum.GetValues( typeof( SkillName ) ).Length );
_skill = new Skill( System.Enum.GetValues( SkillName ).Length );
}
}
Now these are the errors :
Assets/_Scripts/CharacterClasses/BaseCharacter.js(23,41): BCE0085: Cannot create instance of abstract class 'System.Attribute'.
Assets/_Scripts/CharacterClasses/BaseCharacter.js(27,30): BCE0024: The type 'Vital' does not have a visible constructor that matches the argument list '(int)'.
Assets/_Scripts/CharacterClasses/BaseCharacter.js(30,30): BCE0024: The type 'Skill' does not have a visible constructor that matches the argument list '(int)'.
What I find odd is that the errors are the same for both the C# and the uJS versions. I am totally lost, please consider helping, simplify the thought (dumb it down), then explain if you would like. As ever I am in awe of the talented minds here and am always grateful for advice and guidance.
Fix My Script ! No, sorry, that's just the brain melting, please educate me (in laymans terms), many thanks =]
Here is the Attribute Class :
#pragma strict
public class Attribute extends BaseStat
{
public function Attribute()
{
SetExpToLevel( 50 );
SetLevelModifier( 1.05 );
}
}
public enum AttributeName
{
Might,
Constitution,
Nimbleness,
Speed,
Concentration,
Willpower,
Charisma
}
Here is the Vital Class :
#pragma strict
public class Vital extends ModifiedStat
{
private var _currValue : int;
public function Vital()
{
_currValue = 0;
SetExpToLevel( 50 );
SetLevelModifier( 1.1 );
}
public function GetCurrValue() : int
{
if ( _currValue > GetAdjustedBaseValue() )
{
_currValue = GetAdjustedBaseValue();
}
return _currValue;
}
public function SetCurrValue( theValue : int )
{
_currValue = theValue;
}
}
public enum VitalName
{
Health,
Energy,
Mana
}
Here is the Skill Class :
#pragma strict
public class Skill extends ModifiedStat
{
private var _known : boolean;
public function Skill()
{
_known = false;
SetExpToLevel( 25 );
SetLevelModifier( 1.1 );
}
public function GetKnown() : boolean
{
return _known;
}
public function SetKnown( theBool : boolean )
{
_known = theBool;
}
}
public enum SkillName
{
Melee_Offense,
Melee_Defense,
Ranged_Offense,
Ranged_Defense,
Magic_Offense,
Magic_Defense
}
I can also supply my BaseStat and ModifiedStat classes if required. Thanks for reading this far !
Edit : and the BaseStat class :
#pragma strict
public class BaseStat extends MonoBehaviour
{
private var _baseValue : int; // base value of this stat
private var _buffValue : int; // buff to add to stat
private var _expToLevel : int; // exp needed for next level
private var _levelModifier : float; // modifier applied to exp needed for next level
public function BaseStat()
{
_baseValue = 0;
_buffValue = 0;
_levelModifier = 1.1;
_expToLevel = 100;
}
// #region Basic Getters and Setters
public function GetBaseValue() : int
{
return _baseValue;
}
public function SetBaseValue( theValue : int )
{
_baseValue = theValue;
}
public function GetBuffValue() : int
{
return _buffValue;
}
public function SetBuffValue( theValue : int )
{
_buffValue = theValue;
}
public function GetExpToLevel() : int
{
return _expToLevel;
}
public function SetExpToLevel( theValue : int )
{
_expToLevel = theValue;
}
public function GetLevelModifier() : float
{
return _levelModifier;
}
public function SetLevelModifier( theValue : float )
{
_levelModifier = theValue;
}
// #endregion
private function CalculateExpToLevel() : int
{
return parseInt( _expToLevel * _levelModifier );
}
public function LevelUp()
{
_expToLevel = CalculateExpToLevel();
_baseValue ++;
}
// --
public function GetAdjustedBaseValue() : int
{
return _baseValue + _buffValue;
}
public function SetAdjustedBaseValue()
{
}
}
Since the errors are co$$anonymous$$g from BaseStat, yes please post that too. BTW Using 'Attribute' as a class name may be problematic as that's also a system class name (or whatever reserved word). Try rena$$anonymous$$g that class to '$$anonymous$$yAttribute' or something.
I don't understand why you do this: _vital = new Vital( System.Enum.GetValues( VitalName ).Length ); Why do you need the enum length, what purpose does that serve? If you just typed _vital = new Vital(); that would work as you dont have any arguments in your Vital constructor.
Y'know, I just sat back and slowly read the question when I realized 'I am setting the size of an array here' , and changed the () to [] , and the errors disappeared. Then kicked myself .... hard =]
_primaryAttribute = new Attribute[ System.Enum.GetValues( AttributeName ).Length ];
Was just hung up on finding the length of an enum correctly. I should've just Debugged System.Enum.GetValues( AttributeName ).Length to check .
I must admit this tutorial quickly turned into a monkey-see, monkey-do affair, this guy already has the roadmap for how this project is going to be built, so just reeling out these character classes without explanation on how they are accessed and implemented is frustrating. I am hoping to get to the aah moment when the pieces fit and I can see how flexible this type of class system can be.
In the meantime I shall plow on and see if this is now going to work as the C# version in the videos does. Think I should remove this question?
And yes, I totally agree about using na$$anonymous$$g that is the same as inbuilt classes ( there is alot of underscores and theFoo and myBAr going on). Great advice, thanks.
I have edited the question to include the BaseStat class. $$anonymous$$any thanks for your observations and advice Dave.
@Vonni this is the beauty of a flexible system. If I was to add states to any enum, this code looks after itself by building the correct size array. As to you dont have any arguments in your Vital constructor, well I am on video 14 of 300! See my last comment about not knowing what the tutors vision or roadmap is as it hasn't been described or defined (unlike the blackboard theory videos at 3D Buzz before the coding process, there I knew exactly what I was building with my code, sometimes enough to predict what would come next).
Glad you saw that. You can close or delete it. If you care to accept my answer first for the little karma kick, that's ok too.
Answer by DaveA · Jan 08, 2013 at 10:49 PM
private var _vital : Vital[]; // ARRAY of Vital
// a new Vital. Just one! and it doesn't have a constructor that takes one int!
_vital = new Vital( System.Enum.GetValues( VitalName ).Length );
Try this:
_vital = new Vital[ System.Enum.GetValues( VitalName ).Length ];
yep, realized it was defining the length of an array, was just hung up on finding the length of an enum correctly. I should've just Debugged System.Enum.GetValues( AttributeName ).Length to check .
Hi again, thought I'd try my luck with another small question here. How can I return an enum value from an int ? Here's my attempt. I have written (uJS) :
for ( var i : int = 0; i < System.Enum.GetValues( AttributeName ).Length; i ++ )
{
var enumValue : AttributeName = i;
GUI.Label( Rect( 10, i * 40, 100, 25 ), enumValue.ToString );
}
but this throws the error : BCE0023: No appropriate version of 'UnityEngine.GUI.Label' for the argument list '(UnityEngine.Rect, function(): String)' was found.
it seems for C# this can be done with :
GUI.Label( Rect( 10, i * 40, 100, 25 ), ((AttributeName)i).ToString );
where am I going wrong?
It's ok, fafase gave me the answer on another post =]
in uJS it is :
var str : String[] = System.Enum.GetNames( AttributeName );
for ( var s : String in str )
Debug.Log( s );
Your answer
Follow this Question
Related Questions
Enum style variable 2 Answers
Construct class with enum parameter (javascript) 0 Answers
Access Variable in a class from another script 4 Answers
How to Typecast JS Variables as C# Classes? 0 Answers
Array error - Index is less than 0... 3 Answers