Revisiting generic parameters

Hey all. After coming back to this class after a few months. I got the generic parameters for TakeAction working and figured I would share. :slight_smile:

Here is what I did.

In BaseAction I added the following (some of my method and variable names are different than CMs due to my own preferences)

public abstract class BaseAction : MonoBehaviour
{
    public partial class ActionArgs {}
    public abstract ActionArgs GenerateParameters();
...snip...
}

In each Action I define its own kind of ActionArgs and GenerateParameters method. For example, in the MoveAction I have

public class MoveAction : BaseAction
{
    public class MoveArgs : ActionArgs
    {
        public GridPosition targetPosition;
    }

    public override ActionArgs GenerateParameters() {
        var mousePos = LevelGrid.instance.GetGridPosition(MouseWorld.GetPosition());
        return new MoveArgs() { targetPosition = mousePos };
    }

...snip...

    public override void TakeAction(ActionArgs parameters, Action onActionComplete) {
        var moveParameters = (MoveArgs)parameters;
        _targetPos = LevelGrid.instance.GetWorldPosition(moveParameters.targetPosition);
       ...snip...
    }
}

and in the UnitActionSystem this is used in the HandleSelectedAction as follows:

private bool HandleSelectedAction() {
...snip...
        var param = _selectedAction.GenerateParameters();
        SetBusy();
        _selectedAction.TakeAction(param, NotBusy);
...snip...
}

Sorry, but I kinda fail to see the point of this. You are asking an action to give you parameters that you will be passing to the same action. Why? The action already know these values; it generated it for you. There’s no need to be passing it out so that it can be passed back in.

1 Like

I think it would be easier to visualize what’s going on here with a class that takes a parameter other than the target GridPosition

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms