2

I have this function.

How do I change only the parameters that aren't zero when calling the function? This is what I did

function changeParameters(uint newAnte, uint8 newNumberOfPlayers, uint newWinnerPart) {
    if (msg.sender == owner) {
         if (newAnte != 0) {
            ante = newAnte;
         }
         if (newNumberOfPlayers != 0) {
            required_number_players = newNumberOfPlayers;
         }
         if (newWinnerPart != 0) {
            winner_part = newWinnerPart;
        }
        }
}

I'm certain there has to be a more efficient way

Thanks

Anto
  • 311
  • 1
  • 3
  • 9

2 Answers2

2

You can use require for input validation, it is more efficient than using if.

Please check this answer for more details.

Difference between require and assert and the difference between revert and throw

Sanchit
  • 3,482
  • 1
  • 12
  • 29
1

I added a constant None as suggested by another answer that was deleted. That solved my issue:

uint80 constant None = uint80(0); //null constant


function changeParameters(uint newAnte, uint8 newNumberOfPlayers, uint newWinnerPercentage) {
    // Only the creator can alter this
    if (msg.sender == owner) {
     if (newAnte != None) {
        ante = newAnte;
    }
    if (newNumberOfPlayers != None) {
        required_number_players = newNumberOfPlayers;
    }
    if (newWinnerPercentage != None) {
        winner_percentage = newWinnerPercentage;
    }
}
Anto
  • 311
  • 1
  • 3
  • 9