So I am trying to make a system that has a class that gets a type of node from a looping system. There will be a few types of nodes so I am trying to use a generic pooling class, but the problem is to have a dictionary of these generic poolers I had to make an interface that has a generic method, but isn’t generic itself. So the class passed to the generic class will always be the same as the class passed to the method, but I am getting the error “error CS0029: Cannot implicitly convert type ‘CommandNode’ to ‘CommandNode’”
using System;
using System.Collections.Generic;
using StarProductions.Interactables;
public class CommandNodeAbstractPooler
{
private Dictionary<Type, ICommandNodePooler> _commandNodePoolers;
/// <summary>
/// Gets a node that uses the TYPE of InteractableMonoBase.
/// </summary>
/// <typeparam name="TYPE">The type of node to return.</typeparam>
/// <param name="target">The interactable to make the node from.</param>
/// <returns>The node that holds the command values.</returns>
public CommandNode<NODE_TYPE> GetCommandNode<NODE_TYPE>(InteractableMonoBase target) where NODE_TYPE : CommandNode<NODE_TYPE>
{
var type = typeof(NODE_TYPE);
if (_commandNodePoolers.ContainsKey(type))
return _commandNodePoolers[type].GetCommandNode<NODE_TYPE>(target);
_commandNodePoolers.Add(type, new CommandNodePooler<NODE_TYPE>());
return _commandNodePoolers[type].GetCommandNode<NODE_TYPE>(target);
}
}
public interface ICommandNodePooler
{
CommandNode<NODE_TYPE> GetCommandNode<NODE_TYPE>(InteractableMonoBase command/*, CommandNodeBuilder nodeBuilder*/) where NODE_TYPE : CommandNode<NODE_TYPE>;
}
public class CommandNodePooler<NODE_TYPE2> : ICommandNodePooler where NODE_TYPE2 : CommandNode<NODE_TYPE2>
{
private CommandNode<NODE_TYPE2> CommandsEnd;
public CommandNode<NODE_TYPE> GetCommandNode<NODE_TYPE>(InteractableMonoBase command/*, CommandNodeBuilder nodeBuilder*/) where NODE_TYPE : CommandNode<NODE_TYPE>
{
if (CommandsEnd != null)
{
var holder = CommandsEnd;
CommandsEnd = CommandsEnd.Last;
if (holder is CommandNode<NODE_TYPE2>)
// Error here.
return holder;
}
return null;
}
}
public abstract class CommandNode<TYPE>
{
public TYPE Last;
public TYPE Next;
}