Stamina System

Hi!

I've been creating an easy Stamina System that subtracts one point every single time that player make a move on map. If stamine reach 0 playar cannot move anymore. Every 5min stamina is regenereting by 20 points. Everything is based on Veriables and already had few bug that i solved. Buy i can't solve this one, so if someone may help me?

Script:
JavaScript:
// Stamina System

// Default values
$gameVariables.setValue(298, 100); // Max Stamina
$gameVariables.setValue(299, 300); // Time to regenerate stamina (in seconds)
$gameVariables.setValue(300, 20); // Amount of stamina regenerated

class MyGame_Player extends Game_Character {
  constructor() {
    super();
    this._stamina = $gameVariables.value(1);
    this._lastRegenerateTime = Date.now();
  }

  update() {
    super.update();
    this.updateStamina();
  }

  updateStamina() {
    if (this._stamina > 0) {
      this._stamina--;
    } else {
      this._moveSpeed = 0;
    }
    if (this._stamina < $gameVariables.value(298) && Date.now() - this._lastRegenerateTime > $gameVariables.value(299) * 1000) {
      this._stamina += $gameVariables.value(300);
      this._lastRegenerateTime = Date.now();
    }
    this._stamina = $gameVariables.value(298) > this._stamina ? $gameVariables.value(298) : this._stamina;
  }
}

class MyGame_Interpreter {
  changeMaxStamina(value) {
    $gameVariables.setValue(298, value);
  }

  changeStaminaRegenerationTime(value) {
    $gameVariables.setValue(299, value);
  }

  changeStaminaRegenerationAmount(value) {
    $gameVariables.setValue(300, value);
  }
}

class Window_Stamina extends Window_Base {
  constructor() {
    super(Graphics.width - 160, 0, 160, this.fittingHeight(1));
    this.opacity = 0;
    this.contents = new Bitmap(this.width - 32, this.height - 32);
    this.refresh();
  }

  refresh() {
    this.contents.clear();
    this.contents.drawText(`Stamina: ${$gamePlayer._stamina}/${$gameVariables.value(298)}`, 0, 0, this.contentsWidth(), this.lineHeight());
  }

  updateStamina(stamina) {
    this._stamina = stamina;
    this.refresh();
  }
}

class MyScene_Map extends Scene_Base {
  constructor() {
    super();
    this.createStaminaWindow();
  }

  start() {
    super.start();
  }

  createStaminaWindow() {
    this._staminaWindow = new Window_Stamina();
  }

  update() {
    this._staminaWindow.updateStamina($gamePlayer._stamina);
    super.update();
  }
}

And here is error massage:
1680371863654.png
You need to press F8 and go to the Console tab to see the actual error trace and which line is throwing the error.

But where is this code going? If you're plopping exactly what you typed into a plugin file and it's not inside of any other functions, then your first three lines are trying to manipulate $gameVariable objects before the game actually boots up and creates them.

That should be done by an Autorun event on the first screen of your game to initialize those variables. Or use your own variables that are properties of your custom classes instead of using the game variable array...there's rarely ever any reason to do that if you're writing your own code.
I can't even open console 🙁
Try F12 instead. F8 is assigned by the engine, but that doesn't work if the game errors on boot.

Just for the record, it is possible to create a stamina system without plugins, e.g.
There's probably some plugin for it, too, but I don't know of one off the top of my head.
It's the same :/

1680375478413.png
the error basically tells you that a data object does not exist.and since it's in line 4 of your script that probably means that the game variables don't exist (yet).

so when do you execute that script?
It cannot be executed at launch because the game variables are only defined after new game or load game, not on the title screen.
Okay so how to rapair it?
I thought that the engine would automatically start it only after starting a new game, where I also added an event that adds specific values to specific veriables
Plugins are loaded right after the core scripts, before the game boots up.

See how the console says "at Stamina.js:4"? That means the error happened on line 4 of the file Stamina.js.

As Turan and Andar have mentioned, it's because you're trying to access $gameVariables before that object is initialised. You could patch a core script method and add your statements there:
  • The start method of Scene_Boot runs once per game session.
  • The static createGameObjects method of DataManager runs once per new game.
I'd suggest the second one in this case, e.g.
JavaScript:
// IIFE for local scope.
;(() => {
  // Store original method.
  const alias = DataManager.createGameObjects;
  // Redefine method.
  DataManager.createGameObjects = function() {
    // Apply original method.
    alias.apply(this, arguments);
    // Do your stuff now that `$gameVariables` has been initialised.
    $gameVariables.setValue(298, 100); // Max Stamina
    $gameVariables.setValue(299, 300); // Time to regenerate stamina (in seconds)
    $gameVariables.setValue(300, 20); // Amount of stamina regenerated
  };
})();
There may be a way of patching methods that uses only ES6 class syntax, but I personally don't bother with that stuff. 😴

Original thread: https://forums.rpgmakerweb.com/threads/stamina-system.156324/