I have a large Quad Tree structure, made from generic lists (List<T>) pointing to more lists and so on. These lists are contained in a class: TreeNode<T>
In one such tree I store objects of type GameObject.
In order to make tree updates faster, I want to store a pointer / reference to the containing TreeNode on each GameObject, however… I’m coming across issues caused by the generic type <T> of the TreeNode.
Here is where I have the issue (inside the TreeNode class):
contents.Add(typedObject);
if (typedObject is GameObject)
{
GameObject gameObject = (GameObject) (object) typedObject;
gameObject.treeNode = this;
}
I get the error:
Cannot implicitly convert type 'TreeNode<T>' to 'TreeNode<GameObject>'
Any help or suggestions would be greatly appreciated. Thanks.
Edit –>
The node is stored on the GameObject as follows:
public TreeNode<GameObject> treeNode;
If you don’t know the type of the
TreeNode<T>i would suggest to create a non-generic base class. Then you can simply store the reference in the GameObject.Casting
I would avoid it but you can cast the
TreeNode<T>toTreeNode<GameObject>by using the operatorasas follows: