- Home /
I neet to make like a Hyper Procedural Dynamic List on Unity
I need a super dynamic data struct, a list that saves any type of structure or a class. Or a struct that saves any kind of data;
how to make this work ?
list <class1> list_class_one; list <class2> list_class_two; list<> Dynamic_Struct
list_class_two(2).a = list_class_one(4)-> b; //add item to list form other class and conect
Dynamic_Struct.add(list_class_one); // or a struct that save any type of class
Dynamic_Struct.add(list_class_two);
Answer by Mouton · Sep 28, 2019 at 10:34 PM
Use a generic List using an Interface.
public interface ISuperClass
{
}
public class SuperClass : ISuperClass
{
}
public class ChildClassA : SuperClass
{
}
public class ChildClassB : SuperClass
{
}
public class AnotherClass : MonoBehaviour
{
public List<ISuperClass> globalList = new List<ISuperClass>();
private void Start()
{
globalList.Add(new ChildClassA());
globalList.Add(new ChildClassB());
globalList.Add(new SuperClass());
// ..;
}
}
Although, you won't be able to add any kind of instance in this List because existing class inheritance cannot be changed.
An interface is not required to do this, so you could make a List of Object, which is the base class of many other C#/Unity classes
List<Object> globalList;
Edit
To access the items of these lists, you need to typecast the elements to your needs.
public class AiBehaviour : Object
{
public void Disable()
{
// ...
}
}
public class EnvironmentBehaviour : Object
{
public void Trigger()
{
// ...
}
}
public class Example : MonoBehaviour
{
private void Start()
{
List<Object> globalList = new List<Object>();
globalList.Add(new AiBehaviour());
globalList.Add(new EnvironmentBehaviour());
foreach (Object el in globalList)
{
// `el` is an Object, we need to convert it to the correct type before calling a method,
// `Object` class provides a `GetType` method to retrieve in-memory type
switch (el.GetType())
{
case AiBehaviour:
((AiBehaviour)el).Disable();
break;
case EnvironmentBehaviour:
((EnvironmentBehaviour)el).Trigger();
break;
default:
Debug.Log("No valid type cast for this object");
break;
}
}
}
}
As you can see, this result in an undesirable degree of complexity, and should be avoided. To reduce the complexity, you could create an interface class with methods to do the typecasting and use a messaging system to make generic calls to the children, then let the children either valid or discard the message, but this will just move the complexity to the children and make the calling code not understandable:
_privateGlobalList.SendMessage(MessageType.Disable, typeof(AiBehaviour));
In conclusion: just don't try to make a list of everything, with a basic setup you will get better readability/performances with the FindObjectsOfType
method.
Answer by JuanForero · Sep 29, 2019 at 04:13 AM
Hi @Mouton thanks for your answer, I need a very dynamic data struct, but for now I think this is the thing that I looking for.
List<Object> globalList;
globalList.Add(node);
but this general list type Object, how you can acces to a list element data
gloabalList[i].node.current_pos; // dont work when you access like a normal list, list<normal_class> normal_list
Hi, I have updated the answer with details on how to access list elements. If it answers your issue, please accept the answer. If it doesn't please add details with comments on my answer and not by creating an answer. Thank you ! :)