HKI Core
Container.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Xml;
3 using System.Collections.Generic;
4 
5 namespace HKI.Core.Variables
6 {
10  [System.Serializable]
11  [CreateAssetMenu(fileName = "Container", menuName = "HKI/Variables/Container", order = 0)]
12  public class Container : HKIVar
13  {
14  // Public variables
15  [SerializeField] List<HKIVar> container = new List<HKIVar>();
16 
17  // Getter
18  public int Count { get { return container.Count; } }
19 
20  // Interface functions
21  public override void Save(XmlNode parentNode)
22  {
23  XmlNode containerNode = parentNode.OwnerDocument.CreateElement(name.Replace(" ", ""));
24  parentNode.AppendChild(containerNode);
25 
26  for(int i = 0; i < container.Count; i++)
27  container[i].Save(containerNode);
28  }
29 
30  public override void Load(XmlNode node)
31  {
32  XmlNodeList childNodes = node.ChildNodes;
33  for(int i = 0; i < childNodes.Count; i++)
34  {
35  XmlNode childNode = childNodes[i];
36  HKIVar variable = GetHKIVarByNameWithoutSpace(childNode.Name);
37  if(variable != null)
38  variable.Load(childNode);
39  else
40  Debug.LogWarning("(Container) Loading failed in the container with the name >" + name + "< faild to find the variables with the name given by the XmlDocument >" + childNode.Name + "<");
41  }
42  }
43 
44  public override string ToString()
45  {
46  return "This container (" + name + ") has " + container.Count + " object(s).";
47  }
48 
49  public HKIVar GetHKIVarByName(string varName)
50  {
51  for(int i = 0; i < container.Count; i++)
52  {
53  if (varName == container[i].name)
54  return container[i];
55  }
56 
57  Debug.LogError("Container >" + name + "< failed to get HKIVar by name (varName = " + varName + ")");
58  return null;
59  }
60 
61  public HKIVar GetHKIVarByNameWithoutSpace(string varName)
62  {
63  for (int i = 0; i < container.Count; i++)
64  {
65  if (varName == container[i].name.Replace(" ", ""))
66  return container[i];
67  }
68 
69  Debug.LogError("Container >" + name + "< failed to get HKIVar by name (varName = " + varName + ")");
70  return null;
71  }
72 
73  public HKIVar GetHKIVarByIndex(int index)
74  {
75  if(index < 0 || index >= container.Count)
76  {
77  Debug.LogError("Container >" + name + "< failed to get HKIVar by index (index = " + index.ToString() + ")");
78  return null;
79  }
80 
81  return container[index];
82  }
83  }
84 }
HKIVar GetHKIVarByIndex(int index)
Definition: Container.cs:73
This abstract class of a ScriptableObject is provides child classes with on value changed callback ca...
Definition: HKIVar.cs:12
override string ToString()
Definition: Container.cs:44
HKIVar GetHKIVarByNameWithoutSpace(string varName)
Definition: Container.cs:61
This container is itself a HKIVar but it purpose is to hold other HKIVars together at one place...
Definition: Container.cs:12
override void Save(XmlNode parentNode)
Definition: Container.cs:21
override void Load(XmlNode node)
Definition: Container.cs:30
abstract void Load(XmlNode node)
HKIVar GetHKIVarByName(string varName)
Definition: Container.cs:49