Vote count:
0
Im using Unity 4.3.3f1 and currently have a problem with C# generic delegates.
I am using generic delegates as an Order system for telling GameObjects what to do. The idea is that anywhere in my project, i can give specific order to groups and they will carry them out without having to get into a nightmare of if-statements for AI logic.
public class AIOrder<T>
{
T m_orderee;
public T Orderee
{
get { return m_orderee; }
set { m_orderee = value; }
}
GameObject m_objectOfInterest;
public GameObject ObjectOfInterest
{
get { return m_objectOfInterest; }
set { m_objectOfInterest = value; }
}
Vector2 m_positionOfInterest;
public Vector2 PositionOfInterest
{
get
{
if (m_positionOfInterest != null)
return m_positionOfInterest;
else
return ObjectOfInterest.transform.position;
}
set { m_positionOfInterest = value; }
}
/// <summary>
/// Checks whether order has been completed
/// </summary>
/// <returns></returns>
public bool Completed()
{
return hasCompleted(Orderee, ObjectOfInterest, PositionOfInterest);
}
public delegate bool HasCompleted(T objectToActUpon, GameObject objectOfInterest, Vector3 positionOfInterest);
HasCompleted hasCompleted;
/// <summary>
/// Attaches conditions to the class for checking whether it has completed
/// </summary>
/// <param name="func"></param>
public void AttachCondition(HasCompleted func)
{
hasCompleted = func;
}
/// <summary>
/// Applies function to variables
/// </summary>
public void Activate()
{
onActivate(Orderee, ObjectOfInterest, PositionOfInterest);
}
public delegate void OnActivate(T objectToActUpon, GameObject objectOfInterest, Vector3 positionOfInterest);
OnActivate onActivate;
/// <summary>
/// Attaches generic delegates to the class for invokation
/// </summary>
/// <param name="func"></param>
public void AttachAction(OnActivate func)
{
onActivate += func;
}
}
In one class, the main user of this order system called EnemyGroup, i have been using the order class as such. These orders are being kept in a list, and once the order returns true with .Completed(), i remove it from the list and start with the next order.
AIOrder<EnemyGroup> order = new AIOrder<EnemyGroup> { Orderee = this, PositionOfInterest = orderPositions };
order.AttachAction(delegate(EnemyGroup group, GameObject objectOfInterest, Vector3 pointOfInterest)
{
group.RotateTowards(pointOfInterest);
group.SetBehaviour(GroupBehaviour.StayWithSlowest);
foreach (List<EnemyScript> tier in group.Children)
{
foreach (EnemyScript ship in tier)
{
int indexOfShip = group.Children[(int)ship.ShipSize].IndexOf(ship);
ship.SetMoveTarget((Vector2)pointOfInterest + ship.GetLocalFormationPosition());
}
}
});
order.AttachCondition(delegate(EnemyGroup group, GameObject objectOfInterest, Vector3 pointOfInterest)
{
return Vector2.Distance((Vector2)group.transform.position, (Vector2)pointOfInterest) < 0.8f;
});
AddOrder(order);
Now this does not throw any errors and works completely as intended, however if i take this exact code into another class it starts giving me compile errors such as "Argument 1: cannot convert from 'anonymous method' to 'AIOrder.OnActivate'" for both the action and condition.
Am i missing something minute, or am i simply being an idiot? Perhaps there is a better place to post this? I was under the impression that this code would work in any of the classes since the AIOrder is its own seperate class.
This class also does not work if i change the generic type to that of another class, as i intended it to do.
Aucun commentaire:
Enregistrer un commentaire