- Home /
Arrays In Classes?
I have a class within unity, however it will not allow me to define arrays.
This is my class:
class WeaponInfo
{
//Name/Model
var name = "";
var model:Mesh;
//FireRate
var frn:int;
var fr = frn;
//ReloadRate
var rrn:int;
var rr = rrn;
//Ammo
var maxMags:int;
var ammo = new int[4];
ammo[0] = 0;
ammo[1] = 0; //Here is the problem
}
I get the errors:
Error BCE0044: expecting }, found 'ammo'. (BCE0044) (Assembly-UnityScript)
Error BCE0044: expecting EOF, found '}'. (BCE0044) (Assembly-UnityScript)
It works complketely fine when i remove the bottom two ammo variables, so i dont know what the problem is.
Any help is appreciated, thanks!
I am not 100% sure of the details but I know there is something like you cannot assign values to a staticly instantiated array (static as opposed to dynamic). You have to assign those values outside the class.
Is there any way to assign arrays within the class? Thanks for the help
Answer by Eric5h5 · Aug 01, 2012 at 08:22 AM
You should only define the types of the variables outside of the constructor. You can do whatever manipulation you need in the constructor:
...
var ammo : int[];
function WeaponInfo () {
ammo = new int[4];
ammo[0] = 0; // not necessary, it's 0 by default anyway
}
Likewise, instead of this:
var frn:int;
var fr = frn;
Do this:
var frn : int;
var fr : int;
Use the constructor as necessary. Also, do yourself a big favor and use legible variable names; "frn" and "fr" don't mean anything by themselves, and you WILL later wonder what they mean. Just write it out: "fireRate". You don't gain anything by using cryptically short variable names. If you find yourself writing comments to explain them, that's a huge warning sign. Code should be self-documenting as much as possible.
See the problem with assigning them outside the function is another extrerior array... var weapon = new WeaponInfo[3];
Im startting to think just using variables ins$$anonymous$$d of arrays will be easier and more ledgible, thanks for your time however :)
Eh, it's your funeral. I think you'd be better off understanding how classes and constructors work though. I have no idea what you mean by "assigning them outside the function is another extrerior array".
I sorta do understand how classes work. Im currently upgrading my code from $$anonymous$$ANY $$anonymous$$ANY $$anonymous$$ANY arrays and variables to Classes ins$$anonymous$$d.
Thank you anyhow, i have fixed it all now :)
I may be wrong but IU think you misunderstand the principle of constructor. A constructor is a special function inside a class that is launched when you create an instance of a class (object). The constructor does not return any value and is named the same as the class.
class WeaponClass{
var ammo = new int[4];
function WeaponClass(){
ammo[0] = 0;
ammo[1] = 5;
ammo[2] = 10;
ammo[3] = 15;
}
}
function Update(){
var weapon = new ClassWeapon()
}
Now your object has the appropriate values in the array. You do not need to call the function as it is called automatically with new.
Answer by tobiass · Aug 01, 2012 at 08:27 AM
You can define a variable in the class scope and directly initialize it. Assign values in methods. A static initialization method (matches the name of the class) is called when the class is instanced:
function WeaponInfo()
{
ammo[0] = 0;
ammo[1] = 0; // not needed. initialized to 0 anyways
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Creating a class wide array (c#) 1 Answer
Issue with some GUITextures and an Array [SOLVED] 1 Answer
String.Split problem 1 Answer
C# Incrementing a String Array 1 Answer