Reset saved list to default values

Question:
I’m adding an ascend feature in my game, and when you ascend the data for certain variables (game.score, game.clickValue, building.count, upgrade.purchased), when I call the function ascend(), it turns game.score and the CPS to NaN, but when I reload it goes back to how it was before the ascend, for now all I need is all the stats expect for achievement.earn, game.totalScore and game.totalClicks to be reset to their default values

Repl link:
https://replit.com/@JohnnySuriano/Cycle-Clicker-Beta-02

//Doesn't work, line 1350
  function ascendButton() {
    let acsendConfirm = game.score / acsendCost;
    acsendConfirm = Math.round(acsendConfirm);
    console.log(acsendConfirm + 'yo')
    let acsendResult = confirm('Are you sure you want to ascend? Ascending will reset your progress, but you will receive a permentant profit boost, if you ascend now, you will be granted a permentant ' + acsendConfirm + 'x CPS booster')
    if (acsendResult == true) { //Works until here
      game.score = 0;
      game.clickValue = 0;
      building.count = 0;
      loadgame()
    }
  }

This is JavaScript, not Python, so I don’t know why you’re seeking Python code help. Anyway, you can try and use this code:

function ascendButton() {
  let ascendConfirm = game.score / ascendCost;
  ascendConfirm = Math.round(ascendConfirm);
  console.log(ascendConfirm + 'x CPS booster');
  
  let ascendResult = confirm('Are you sure you want to ascend? Ascending will reset your progress, but you will receive a permanent profit boost. If you ascend now, you will be granted a permanent ' + ascendConfirm + 'x CPS booster.');
  
  if (ascendResult) { // You can simplify this condition since it's already a boolean.
    // Reset the variables you mentioned.
    game.score = 0;
    game.clickValue = 1; // You may want to set it to a default value, like 1.
    building.count = 0;
    
    loadgame();
  }
}

Must have clicked the wrong tag :joy:

When I ascend, the values become NaN instead of 0

1 Like

Is that with the code I sent?

hmm, try this code instead:

function ascendButton() {
  let ascendConfirm = game.score / ascendCost;
  ascendConfirm = Math.round(ascendConfirm);
  console.log(ascendConfirm + 'x CPS booster');
  
  let ascendResult = confirm('Are you sure you want to ascend? Ascending will reset your progress, but you will receive a permanent profit boost. If you ascend now, you will be granted a permanent ' + ascendConfirm + 'x CPS booster.');
  
  if (ascendResult) {
    game.score = isNaN(game.score) ? 0 : game.score;
    game.clickValue = isNaN(game.clickValue) ? 1 : game.clickValue; // Set it to a default value like 1 if it's NaN.
    building.count = isNaN(building.count) ? 0 : building.count;
    
    loadgame();
  }
}

It has the same result as last time, but I added

console.log(game.score + 'Score After')

At the end of the if statement, it logs the value of game.score before you ascend. Also for some reason the loadgame function is not defined

Even though loadgame was defined earlier in the program

//Earlier in the code
  function loadGame() {
    let savedGame = JSON.parse(localStorage.getItem('gameSave'))
    if (localStorage.getItem('gameSave') != null) {
      if (typeof savedGame.score !== 'undefined') game.score = savedGame.score;
      if (typeof savedGame.totalScore !== 'undefined') game.totalScore = savedGame.totalScore;
      if (typeof savedGame.totalClicks !== 'undefined') game.totalClicks = savedGame.totalClicks;
      if (typeof savedGame.achievementsEarned !== 'undefined') game.achievementsEarned = savedGame.achievementsEarned;
      if (typeof savedGame.completionPercentage !== 'undefined') game.completionPercentag = savedGame.completionPercentag;
      if (typeof savedGame.clickValue !== 'undefined') game.clickValue = savedGame.clickValue;
      if (typeof savedGame.buildingCount !== 'undefined') {
        for (i = 0; i < savedGame.buildingCount.length; i++) {
          building.count[i] = savedGame.buildingCount[i]
        }
      }
      if (typeof savedGame.buildingCount !== 'undefined') {
        for (i = 0; i < savedGame.buildingCount.length; i++) {
          building.count[i] = savedGame.buildingCount[i]
        }
      }
      if (typeof savedGame.buildingIncome !== 'undefined') {
        for (i = 0; i < savedGame.buildingIncome.length; i++) {
          building.income[i] = savedGame.buildingIncome[i]
        }
      }
      if (typeof savedGame.buildingCost !== 'undefined') {
        for (i = 0; i < savedGame.buildingCost.length; i++) {
          building.cost[i] = savedGame.buildingCost[i]
        }
      }
      if (typeof savedGame.upgradePurchased !== 'undefined') {
        for (i = 0; i < savedGame.upgradePurchased.length; i++) {
          upgrade.purchased[i] = savedGame.upgradePurchased[i];
        }
      }
      if (typeof savedGame.achievementAwarded !== 'undefined') {
        for (i = 0; i < savedGame.achievementAwarded.length; i++) {
          achievement.awarded[i] = savedGame.achievementAwarded[i];
        }
      }
    }
  }

You’re defining two different loadgames, loadgame and loadGame. In the code I sent change loadgame() to loadGame(), since loadgame() isn’t defined and loadGame() is.

1 Like

That seemed to fix that issue, but now when I ascend it gives me all of these achievements


(Except the first 2)
The starts mean getting a certain amount of cycles and the cursors mean getting a certain amount of cursors, even when I had 0 cursors and about 5 cycles when I ascended and the values are still NaN
I have no idea what is causing this cause there’s nothing to do with upgrades in the ascendButton function

You would have to debug, etc. I can’t really help that much since your code was too large for me to go through and check it all, sorry.

1 Like

Anyone wondering or just veiwing this topic I fixed this issue

And the other ones now i’m just trying to work out how to reset the building count when ascending

Fixed everything I wanted to do, thanks to @Sky for the help

1 Like

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