Use unknown number of "or's ||" in if statements

Hey i’m looking for advice for the code section down below. What i want to do is: A mechanic that spawnes “straight rivers” inside my grid, but the nummer on how much rivers should be spawn dependes on how many updates the player already bought. So first randomly decide if the River is vertical or horizontal, when i create 4 unique numbers and when i set the 4 variables. I did this, because the variables should be accessed from outside the script and also in the second if statement in the in this code-block (GenerateGrid).

    [SerializeField] public int _waterVertical_1,_waterVertical_2,_waterVertical_3,_waterVertical_4 ;
    [SerializeField] public int _waterHorizontal_1,_waterHorizontal_2,_waterHorizontal_3,_waterHorizontal_4;
    [SerializeField] private TileScript _grassTile, _waterTile, _mountainTile;

 public void GenerateGrid () {

        for (int x = 0; x < _width; x++){ 
            for (int y = 0; y < _height; y++) {
            
            TileScript choosenTile;

            ///This is the importened Part
            //* Grid Spawn Logic 
            if (RiverVertical) {
                if (x == _waterVertical_1 ||x == _waterVertical_2 ||x == _waterVertical_3 ||x == _waterVertical_4) {
                    choosenTile = _waterTile;
                } else {
                    var randomTile = UnityEngine.Random.Range(1,11) <= 9 ? _grassTile : _mountainTile;
                    choosenTile = randomTile;
                }
            } else {
                if (y == _waterHorizontal_1 ||y == _waterHorizontal_2 ||y == _waterHorizontal_3 ||y == _waterHorizontal_4) {
                    choosenTile = _waterTile;
                } else {
                    var randomTile = UnityEngine.Random.Range(1,11) <= 9 ? _grassTile : _mountainTile;
                    choosenTile = randomTile;
                }
            }

            var spawnedTile = Instantiate(choosenTile,new Vector3(x,y),Quaternion.identity);
            spawnedTile.name = $"Tile {x} {y}";

What I use to set this _waterVertical-Numbers is this. It works like this, but it isn’t scaleable and every time i see this part of my code i think where must be a better way in doing this.



    private void RandomizeGrid() {
        if (RiverCount > 4) { RiverCount = 4;}

        if (RiverVertical == false) {
            switch (RiverCount) {
                case 1:
                    _waterHorizontal_1 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_2 = _waterHorizontal_1;
                    _waterHorizontal_3 = _waterHorizontal_1;
                    _waterHorizontal_4 = _waterHorizontal_1;
                    break;
                case 2:
                    _waterHorizontal_1 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_2 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_3 = _waterHorizontal_1;
                    _waterHorizontal_4 = _waterHorizontal_1;
                    break;
                case 3: 
                    _waterHorizontal_1 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_2 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_3 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_4 = _waterHorizontal_1;
                    break;
                case 4:
                    _waterHorizontal_1 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_2 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_3 = UnityEngine.Random.Range(1,6);
                    _waterHorizontal_4 = UnityEngine.Random.Range(1,6);
                    break;
                
            }
        }else {
            switch (RiverCount) {
                case 1:
                    _waterVertical_1 = UnityEngine.Random.Range(1,6);
                    _waterVertical_2 = _waterVertical_1;
                    _waterVertical_3 = _waterVertical_1;
                    _waterVertical_4 = _waterVertical_1;
                    break;
                case 2:
                    _waterVertical_1 = UnityEngine.Random.Range(1,6);
                    _waterVertical_2 = UnityEngine.Random.Range(1,6);
                    _waterVertical_3 = _waterVertical_1;
                    _waterVertical_4 = _waterVertical_1;
                    break;
                case 3: 
                    _waterVertical_1 = UnityEngine.Random.Range(1,6);
                    _waterVertical_2 = UnityEngine.Random.Range(1,6);
                    _waterVertical_3 = UnityEngine.Random.Range(1,6);
                    _waterVertical_4 = _waterVertical_1;
                    break;
                case 4:
                    _waterVertical_1 = UnityEngine.Random.Range(1,6);
                    _waterVertical_2 = UnityEngine.Random.Range(1,6);
                    _waterVertical_3 = UnityEngine.Random.Range(1,6);
                    _waterVertical_4 = UnityEngine.Random.Range(1,6);
                    break;
                
            }
        }
        
    }

Generally when you see yourself write code like this, the solution is usually to either use a collection, or use a loop, usually both.

1 Like

Your instincts are correct… I don’t even know what you are trying to do but I know there is a better way to do it :slight_smile:

Fundamentally (I believe) you are trying to generate a “random” tile and you don’t have a method that returns such a tile. That you are eventually trying to build a grid isn’t relevant in this case. GetTile() should smart enough to return the tile you need based upon some rules/inputs.