C
C
ColdSpirit2019-02-09 02:51:25
OOP
ColdSpirit, 2019-02-09 02:51:25

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.

Code Example
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

1 answer(s)
G
Griboks, 2019-02-09
@ColdSpirit

An interface allows you to set a method signature. Therefore, arguments can be written in the interface itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question