Assigning generic parameters in HandleSelectedAction for use in TakeAction

I am looking to implement the example you talked about of having the generic TakeAction method take BaseParameters as one of its parameters.

To do this I got it working by writing my HandleSelectedAction method as follows:

private bool HandleSelectedAction() {
        if (!Input.GetMouseButtonDown(0)) return false;
        
        var mouseGridPos = LevelGrid.instance.GetGridPosition(MouseWorld.GetPosition());
        
        BaseParameters param = _selectedAction switch {
            MoveAction moveAction => new MoveParameters() { targetPosition = mouseGridPos },
            SpinAction spinAction => new SpinParameters(),
            _ => throw new ArgumentOutOfRangeException()
        };

        if (_selectedAction.IsValidGridPosition(mouseGridPos)) {
            IsBusy();
            _selectedAction.TakeAction(param, NotBusy);
            return true;
        }

        return false;
    }

However, I do not like using the switch to specify the parameter class to use since that means that I will need to change the code every time I want to add a new action. Is there a way of selecting the parameter class to use depending on what out selectedAction is without doing this?

2 Likes

I’m not up to where you are yet, but looking at your code/question, I suppose you could always use a state machine - it’s in the unity patterns course if you’re interested.

I suspect you could also use anonymous functions too.

Possibly. Another idea I am thinking of was to have a static dictionary that maps the type of XXXXAction (as the key) to the XXXXParameters class (as the value). However, then again there is the question of automatically populating this dictionary… Maybe if I put them in Resources and use the various methods to get things from there?

Hmmm… maybe I can put the actions as a kind of Scriptable Object and then instead of adding actions as components I populate a list of available actions? I am doing something like that in one of the projects I am working on to define a slew of different possible interactions with items in a scene.

Just spit-balling here.

1 Like

You could make a function on the Action class to GenerateParameters(); and each class would return its specific parameters.
That way all the Parameters logic would be on the same Action class so adding more Actions would just require building the Action class and not touching the UnitActionSystem
The one “issue” is for example you would have to get the mouse grid position in that function, alternatively you could always pass in the mouse grid position to the GenerateParameters();

Basically there’s many many ways to do it, which one is the best will depend on what you would like to do with the parameters, perhaps it makes sense for each Action to handle that logic, or perhaps it might make more sense for the UnitActionSystem to handle that logic.

2 Likes

Thanks! I have a few ideas now of how to handle this. I will play with them and see which one I like the best.

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

Privacy & Terms