简体   繁体   中英

c# strange classes behavior

so, im making small unity game and have some classes for working with big nubmer's

here is the code for class CurrLevel

public class CurrLevel {

    public CurrLevel (int levelNum, string id) {
        valuesLevel = "pow" + levelNum.ToString();
        if(levelNum != 0){
            numberFormatSci = "10^" + levelNum.ToString();
        } else {
            numberFormatSci = "";
        }
        identificator = id;
    }
    public string valuesLevel;
    public string numberFormatSci;
    public string identificator;

    public int getValue(){
        return PlayerPrefs.GetInt(identificator+"-"+valuesLevel);
    }

    public void setValue(int value){
        PlayerPrefs.SetInt(identificator+"-"+valuesLevel, value);
    }

    public void add(int value){
        PlayerPrefs.SetInt(identificator+"-"+valuesLevel, PlayerPrefs.GetInt(identificator+"-"+valuesLevel) + value);
    }

    public void substract(int value){
        PlayerPrefs.SetInt(identificator+"-"+valuesLevel, PlayerPrefs.GetInt(identificator+"-"+valuesLevel) - value);
    }
}

here is the code for class SomeCurrency

  public class SomeCurrency {
    public string identificator;
    public  CurrLevel[] levels = new CurrLevel[10];     

    public SomeCurrency(string id){
        identificator = id;
        for(int i = 0; i < 30; i=i+3){
            levels[i/3] = new CurrLevel(i, identificator);
        }
    }

    public void add(int power, double value){
        int full = (int) value;
        int leftover = (int) (value*1000 - full*1000);
        if(power >= 3){
            levels[power/3-1].add(leftover);
        }
        levels[power/3].add(full);
        updateValues();
    }

    public SomeCurrency copy(SomeCurrency CurrToCopy){
        SomeCurrency copy = new SomeCurrency(CurrToCopy.identificator);
        for(int i = 0; i < 30; i++){
            copy.levels[i/3] = CurrToCopy.levels[i/3];
        }
        return copy;
    }
    public void addAnotherCurrency(SomeCurrency anotherCurr){
        for(int i = 0; i < 30; i=i+3){
            this.add(i, anotherCurr.levels[i/3].getValue());
        }
        updateValues();
    }

    public bool substractAnotherCurrency(SomeCurrency anotherCurr){
        SomeCurrency buffer = copy(anotherCurr);
        Debug.Log(anotherCurr.levels[1].getValue());
        if(canSubstract(buffer)){
            Debug.Log(anotherCurr.levels[1].getValue());
            // for(int i = 27; i >= 0; i-=3){
            //  levels[i/3].substract(anotherCurr.levels[i/3].getValue());
            // }
            return true;
        } else {
            return false;
        }
    }

    public bool canSubstract(SomeCurrency fromWhereSubstract){
        bool possible = false;
        for(int i = 0; i < 30; i+=3){
            fromWhereSubstract.levels[i/3].substract(levels[i/3].getValue());
            if(i != 27){
                if(fromWhereSubstract.levels[i/3].getValue() < 0){
                    fromWhereSubstract.levels[i/3+1].substract(1);
                    fromWhereSubstract.levels[i/3].add(1000);
                }
            }
        }
        if(fromWhereSubstract.levels[9].getValue() < 0){
            possible = true;
        }
        return possible;
    }
    public void setValue(int power, double value){
        int full = (int) value;
        int leftover = (int) (value*1000 - full*1000);
        if(power >= 3){
            string beforeid = identificator+"-"+levels[power/3-1].valuesLevel;
            PlayerPrefs.SetInt(beforeid,leftover);
        }
        string thisid = identificator+"-"+levels[power/3].valuesLevel;
        PlayerPrefs.SetInt(thisid,full);
        updateValues();
    }

    public string getStringValue(){
        int maxlvl = 0;
        for(int i = 27; i >= 0; i=i-3){
            if(levels[i/3].getValue() > 0){
                maxlvl = i/3;
                break;
            }
        }
        string result = levels[maxlvl].getValue().ToString();
        if(maxlvl > 0){
            string leftover = levels[maxlvl-1].getValue().ToString();
            while(leftover.Length != 3){
                leftover = "0"+leftover;
            }
            result += "." + leftover + "*" + levels[maxlvl].numberFormatSci;
        }
        return result;
    }

    public void resetValues(){
        for(int i = 0; i < 30; i+=3){
            levels[i/3].setValue(0);
        }
    }

    private void updateValues(){
        for(int i = 0; i < 27; i=i+3){
            levels[i/3] = new CurrLevel(i, identificator);
            if(levels[i/3].getValue() >= 1000){
                levels[i/3].setValue(levels[i/3].getValue()-1000);
                levels[i/3+1].setValue(levels[i/3+1].getValue()+1);
            }
        }
    }
}

So basicly, in the code i create to new variables type of SomeCurrency

  public NumberFormatting.SomeCurrency playerScore = new NumberFormatting.SomeCurrency("playerScore");
  public NumberFormatting.SomeCurrency playerClickValue = new NumberFormatting.SomeCurrency("playerClickValue");
    playerScore.resetValues();
    playerScore.add(6, 1.32);
    playerClickValue.resetValues();
    playerClickValue.add(3, 103.831);

And later, when player clicks the button i try to substract one from another

    Debug.Log(playerClickValue.levels[1].getValue());
    Debug.Log(playerScore.substractAnotherCurrency(playerClickValue));

Debugger firstly print 103 (original value of playerClickValue.levels[1].getValue() from click function), then it prints 103 again (from the function substractAnotherCurrency before if(canSubstract(buffer)), but printing the same variable after this canSubstract shows the value of 783. So, my functions somehow change original value of playerClickValue every time i call substractAnotherCurrency. What should i change to keep the playerClickValue same, but still checking can i suubstract it from another SomeCurrency, and after checking if i can - do so.

In C#, objects are passed by reference, meaning that if you modify an object in a fonction, it will be modified everywhere. You can read more about it, it will be important when coding. It seems you tried to do something like a copy somewhere, but you don't use the copy at all.

Also, are you sure you want to edit a variable in canSubstact ?

The name suggest if will just return a bool and not change anything but you actually call substact in it

fromWhereSubstract.levels[i/3+1].substract(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM