Home>

I am creating a roguelike game. A method that determines the position of the line that divides the map into n equal parts, and the argument is an array that stores the minimum value, maximum value, and the position of the line that divides the map into n equal parts.
When the if (pointMin

void GetSplitPoint (int min, int max, int [] stack_Pos) {
        // Create a starting position to split
        // Constant to confirm at least 4 squares of room compartment
        const int MIN_POS = 5;
        const int WALL_POS = 5;
        const int LAST_INDEX = -1;
        int splitNum = 0;
        int index = 0;
        int pointMin = min;
        int pointMax = max;
        int calcMin = min;
        int calcMax = max;
        // Here is back
        if (pointMin<pointMax) {
            splitNum = Random.Range (calcMin, calcMax);
            calcMin = splitNum --pointMin;
            calcMax = pointMax --splitNum;
            if (calcMax>calcMin) {
                stack_Pos [index] = splitNum;
                pointMin = splitNum + MIN_POS;
                index ++;
            } else {
                stack_Pos [index] = splitNum;
                pointMax = splitNum --WALL_POS;
                index ++;
            }
        }
        stack_Pos [index] = LAST_INDEX;
    }
  • Answer # 1

    I'd like you to process it with pointMax or pointMin updated.

    As a method argumentrefYou can use. Narrow down the points,void GetSplitPoint (int min, int max, int [] stack_Pos)This is an example of changing and updating each variable.

    // void GetSplitPoint (int min, int max, int [] stack_Pos)
    void GetSplitPoint (ref int pointMin, ref int pointMax)
    {
        // +2 each
        pointMin + = 2;
        pointMax + = 2;
    }

    If you try to execute this with the following test code

    // initial value
     int min = 1;
     int max = 2;
     System.Diagnostics.Debug.WriteLine ($"0: min = {min}, max = {max}");
     GetSplitPoint (ref min, ref max);
     System.Diagnostics.Debug.WriteLine ($"1: min = {min}, max = {max}");
     GetSplitPoint (ref min, ref max);
     System.Diagnostics.Debug.WriteLine ($"2: min = {min}, max = {max}");
     GetSplitPoint (ref min, ref max);
     System.Diagnostics.Debug.WriteLine ($"3: min = {min}, max = {max}");


    The following output results will be obtained. You can see that the variable on the calling side has been updated.

    0: min = 1, max = 2
    1: min = 3, max = 4
    2: min = 5, max = 6
    3: min = 7, max = 8

    I would appreciate it if you could let me know if there is a reference site.

    C #refOrout outYou should learn about.

    ref (C # reference) --Microsoft Docs
    out parameter modifier (C # reference) --Microsoft Docs

  • Answer # 2

    I think you can use it if you use pass by reference.
    If you make it like this, the price will change.

    public void sample () {
        int i = 5;
        value1 (i);// pass by value
        Console.WriteLine (i);// =>5
        value2 (ref i);// pass by reference
        Console.WriteLine (i);// =>10
    }
    // Pass by value type
    public void value1 (int x) {
        x = 10;
    }
    // Pass value type by reference
    public void value2 (ref int x) {
        x = 10;
    }