Answer the question
In order to leave comments, you need to log in
How to normally forward data to child classes using OOP?
Good time of the day! I'm trying to make AI conscientiously, so that both OOP is and universal, but it doesn't work. Stupidly C# won't let me do this even with generics. The goal is to pass custom information to the implementation of the IBotBehaviour interface that is needed only for this behavior. I would like to forward it through a constructor or method. But so that it was obligatory to forward.
Here is an example of interfaces and implementations:
IBotBehaviour is a behavior interface that should oblige information to be passed to it.
IBot - uses IBotBehaviour.
IdleBehaviour - does nothing, so it doesn't need any info.
FollowBehaviour - requires 2 arguments of type Position
and BotTest - uses alternately different behaviors and forwards completely different parameters to them.
public interface IBotBehaviour
{
void Process();
void Setup(); // ?
}
public interface IBot
{
void SetBehaviour(IBotBehaviour behaviour);
}
public class IdleBehaviour : IBotBehaviour
{
public void Process()
{
// wait 1s
}
public void Setup()
{
}
}
public class FollowBehaviour : IBotBehaviour
{
Position myPosition;
Position targetPosition;
public void Process()
{
// move my pos to target pos
}
public void Setup(Position myPosition, Position targetPosition)
{
this.myPosition = myPosition;
this.targetPosition = targetPosition;
}
}
public class BotTest : IBot
{
protected IBotBehaviour currentBehaviour;
public void SetBehaviour(IBotBehaviour behaviour)
{
currentBehaviour = behaviour;
}
// call currentBehaviour.Process() in game cycle
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question