How do you fill an entire object/class with empty data?

Hello again, I’m working with xml data and an xml reader.

If I have an object like this:

public class RPG_Item
{
    public string Name;
    public bool Useable_OnField;
    public bool Useable_InBattle;
    public int Effect_Base;
    public enum StatusEffect { Dodge, Mana, DodgeEachTurn, ManaEachTurn }
    public StatusEffect statusEffect;
    public int Effect_EachTurn_NumberOfTurns;
    public enum Range { Close, Far, Any }
    public Range range;
    public enum UsedOn { OneAlly, AllAllies, Enemy, AllEnemies, Everyone }
    public UsedOn usedOn;
    public List<string> AnimationEcmds = new List<string>();                        //example will be like: PlayAni(40,pos goes here) where 40 is ID and pos could be a battle character or o_object on the field.
}

Is there a way to fill all of these values with empty data when initializing the object? Here’s an example:

RPG_Item rpg_TestItem = new RPG_Item ();

I then have to populate it for it to show up in the xml file, which means I have to create dummy values like so:

        rpg_TestItem.Name = "Potion";
        rpg_TestItem.Useable_OnField = true;
        rpg_TestItem.Useable_InBattle = true;
        rpg_TestItem.AnimationEcmds.Add ("Ani_Play(PotionAnimation1,LocationGoesHere,DoWait)");
        rpg_TestItem.AnimationEcmds.Add ("Wait(150)");
        rpg_TestItem.Effect_Base = 10;
        rpg_TestItem.statusEffect = RPG_Item.StatusEffect.Mana;
        rpg_TestItem.Effect_EachTurn_NumberOfTurns = 1;
        rpg_TestItem.range = RPG_Item.Range.Any;
        rpg_TestItem.usedOn = RPG_Item.UsedOn.OneAlly;

After creating these values I can then save the file, then open my xml editor and add new entires, remove entries etc… to make the basic data for the game.

I’m wondering if I can do something like:

RPG_Item rpg_TestItem = new RPG_Item ();

rpg_TestItem - > Fill all entires with null/blank data

Just to make it simpler, so that when I add a new entry to the class itself I don’t have to keep going back and forth and add values to the initial dummy item.

You need to learn about Constructors

1 Like

Those fields will get initialized with default values automatically. If it’s not saving, then there’s a bug in your serialization code.

1 Like

If your class does not contain any defined constructors ( eg new RPG_Item(blah, 1, 1,…):wink: then a default constructor will be supplied which will initialize all instance variables to their appropriate default values (eg ints are set to 0, floats to 0f, and objects are set to null). If you have a constructor in your class, you will need to explicitly define a constructor “public RPG_Item()” and within that constructor you must set all the values to their default values.

Also you should use the “private” modifier for all instance variables. Unity tends to have fits with modifying public variables at times, so it’s best to make them private. You should then use getters and setters for each instance variable, which also allows you to put in conditions for changing said variables (eg say an int can’t be less than 0. Then calling setInt(-5) would not do anything if you code it to not)

Following this should allow you to be able to do that which you desire

1 Like

As @huelsman said, look at constructors. I’m not entirely sure what you’re looking to do but if you’re going to store class data in a file then you will need to allow your class to be serialised.

[Serializable]
public class RPG_ITEM
{

//...

}

If you’re looking to populate a file with a “template” for data then you may need to reconsider your approach. If you know how your class objects are structured, it shouldn’t be hard to define a template for them without creating a file of default values.

1 Like

What are you using to write the XML? It probably has a flag you can set named something like “emit defaults” which will make it write out values for fields that are equal to their default value.

1 Like

Thanks for the replies guys, I got it working. : p Much appreciated!