Zero94

Villager
Member
Joined
Mar 28, 2023
Messages
21
Reaction score
1
First Language
Polish
Primarily Uses
RMMZ
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
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
8,896
Reaction score
6,700
First Language
English
Primarily Uses
RMMV
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.
 

Zero94

Villager
Member
Joined
Mar 28, 2023
Messages
21
Reaction score
1
First Language
Polish
Primarily Uses
RMMZ
I can't even open console :(
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,646
Reaction score
3,777
First Language
EN
Primarily Uses
RMMZ

Zero94

Villager
Member
Joined
Mar 28, 2023
Messages
21
Reaction score
1
First Language
Polish
Primarily Uses
RMMZ
It's the same :/

1680375478413.png
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
38,490
Reaction score
10,908
First Language
German
Primarily Uses
RMMV
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.
 

Zero94

Villager
Member
Joined
Mar 28, 2023
Messages
21
Reaction score
1
First Language
Polish
Primarily Uses
RMMZ
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
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,646
Reaction score
3,777
First Language
EN
Primarily Uses
RMMZ
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. :kaoslp:
 

Latest Threads

Latest Posts

Latest Profile Posts

This could probably be an entire thread, but it’s really interesting how replaying a game several years later can change how you relate to a character. I think Tidus from FFX got such a bad rap. I getchu. Completely different reaction as an adult now.
As you see, I still enjoy writing tutorials. Is there anything specific you want to see? (I know mapping and editing/resource making is usually popular, but those are very broad topics)
Well, I wanted to expand player battlers visually and now have 3 sheets and counting for each of my players party.
1. Regular sheet
2. The character has turned stone sheet.
3. Using potions sheet.

Technically the main hero has 4 since he starts with a wooden sword, and I felt that the battler should reflect that until he gets a metal one.

Right back to the RM game dev grind in about 15 minutes. :LZSexcite:
Days ago I gave someone the sage advice not to steer away from your main project or take up another one to abandon the first. After figuring a way that would save me years of development, I am forced to redo a lot of stuff on my own turf, near to remake half of what I have already made.
Somewhere down the line I must have crossed paths with bad karma.
:kaomad2:

Forum statistics

Threads
131,733
Messages
1,222,756
Members
173,484
Latest member
sewamobilbali
Top