- Home /
Prepopulate Class Array
Hello,
I've got a class that defines all my game rules called 'GameConstants' (however, they're not actually of the Consts type as they need to be serialized).
For example, I have some ints like:
public class GameConstants{
public int BUILD_TIME = 10;
public int BUILD_COST = 300;
///etc...
But I also want to include another class within this class, as an array, and populate it, but I'm not sure how the syntax works for it:
public BuildingType[] BUILDING_TYPES = new BuildingType[]{
BuildingType.x = 0,
BuildingType.y = 0,
BuildingType.id = 12
};
This gives me the error 'A field initializer cannot reference the nonstatic field.
How can I pre-populate a class array without using a runtime void? (mono behaviour constructor)
unfortunately I think you'll have to do this during runtime or you'd have to change the class to static.
I am happy to change it to static, like I said - these are game rules, so there will only be one instance of this. Static works for me
What do you mean with pre-populate? populate it on inspector? Is BuildingType a monobehaviour?
Usually, I would create an empty array, and then in the Start void, I'll populate that array. But I don't want this. I want it so its predefined already, like how you can do with an int array:
int[] _myArray = new int[]{0,1,2,8}
I want to be able to do that, but with using a class ins$$anonymous$$d. Yeah BuildingType is a mono that I made too.
Is there some reason it is a monobehaviour? Any how the way you try to set the values is wrong because it expects either the fields to be static (meaning they are all the same for every instance of the class) or you need to define which instance you want to modify. Now in either case (if it is $$anonymous$$ono or not) the code would be like that:
buildingTypes = new BuildingType[]
{
new BuildingType(0,0,0),
new BuildingType(1,1,1),
new BuildingType(2,2,2)
};
[System.Serializable] //no need for that if it is $$anonymous$$ono
public class BuildingType : $$anonymous$$onoBehaviour
{
public int x;
public int y;
public int id;
public BuildingType(int x,int y,int id)
{
this.x = x;
this.y = y;
this.id = id;
}
}
The problem is if you run that you ll notice that in the inspector the Array would seem like it is null but actually it is populated and you can access each one of the new BuildTypes you created, also classes that inherit from $$anonymous$$ono don't like the constructor and you ll notice a message "You are trying to create a $$anonymous$$onoBehaviour with the "new"... this is not allowed" fear not the constructor is called any way, but the Start/Update and so on will not be called and that s the reason you don't want it to be a $$anonymous$$onoBehaviour. Lastly to define if you need to be a $$anonymous$$onoBehaviour or not answer the simple question if you ever going to attach it to a GameObject if not and it is meant as a Data container then remove the inheritance.(you could also make it a struct if it does not contain any functions). Cheers for clarifications ask away.