dimanche 14 février 2021

How to create a generic class that can take many parameters

Okay, so I am creating a Utility AI framework. For this to work I need a class that can change a lot depending on the situation I am sure, and I hope that there is a way to use polymorphism or some sort of design pattern to solve my issue.

Let me show you what I mean

I have an action for the sake of example let's say I have the following action Attack Target

This action can have a number of considerations that will vary a lot but all implement the same interface:

   public interface IConsideration
{
    /// <summary>
    ///   A unique named identifier for this consideration.
    /// </summary>
    string NameId { get; }

    /// <summary>
    ///   The weight of this consideration.
    /// </summary>
    float Weight { get; set; }

    /// <summary>
    ///   If true, then the output of the associated evaluator is inverted, in effect, inverting the
    ///   consideration.
    /// </summary>
    bool IsInverted { get; set; }

    /// <summary>Calculates the utility given the specified context.</summary>
    /// <param name="context">The context.</param>
    /// <param name="value"></param>
    /// <returns>The utility.</returns>
    float Consider<T>(BaseAiContext context, T value);

}

The above is my current implementation it doesn't really solve the issue I have

As you can see the "most" important method of this Interface is the Consider

and here lies the issue preferably I should be able to pass data to this class in a way that I can control.

For the sake of example let's say one consideration I have is Move To Location here I want to send the following parameters:

  • Location of target
  • Weapon type (ranged / melee)
  • location list

The above is just an example to prove my point. There is another issue with this - how can I pass the correct parameters when I finally have them? say that I have the following code:

public List<IConsideration> considerations;
float targetDistance = 2;

for (int i = 0; i < considerations.Count; i++)
{
    float AxisScore = considerations[i].Consider(BaseAiContext,targetDistance );
}

Since I have to use the Interface type I am unable to know exactly which values to parse as parameters.

To sum it up:

How can i "parameterize" my class in a generic way?

How can I distinguish these parameterizations so I can provide a consideration with the correct values?

Aucun commentaire:

Enregistrer un commentaire