Looking for explanation of returning a GridPosition list

Would anyone be able to explain the approach of creating a list in this function within the SpinAction script? I’ve never seen, nor used this way before.

    public override List<GridPosition> GetValidActionGridPositionList()
    {
        GridPosition unitGridPosition = unit.GetGridPosition();

        return new List<GridPosition>
        {
            unitGridPosition
        };
    }

Am I right in thinking that it’s another form of:

listName.Add(value);

Any help would be appreciated, and I apologise if this is such a basic thing.

1 Like

Yup exactly, that’s just the syntax for creating and immediately adding elements to the list, it constructs the object then adds any elements inside the { } code block, you can even add multiple and separate them with a comma { element1, element2 }

The end result is the exact same as

List<GridPosition> gridPositionList = new List<GridPosition>();
gridPositionList.Add(unitGridPosition);
return gridPositionList;
4 Likes

Perfect, thank you! I’ll be using the new way now in future.

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

Privacy & Terms