MV Game variable call

Hey folks,

I've seemed to have hit a dead end on one of my scripts.

I'm trying to declare a variable as a game variable but the console keeps kicking it out as a null value.

Before I ruin my face further by smashing it off the keyboard, could someone possibly offer a hand here?

I have the code set as follows:

Code:
var timeDisplay = $gameVariables.value(8);

So that my var, "timeDisplay" is set to the same value as the in game variable #8.

Any thoughts as to why this is kicking out null?

Thank you in advance for any and all help!
(i asume that your $gamevariable stores a number and not a text)
perhaps "var timeDisplay" needs to be a number first..


JavaScript:
var timeDisplay = 1;
var timeDisplay = $gameVariables.value(8);

(i am not sure, just an idea)
perhaps it needs to be a number first..

JavaScript:
var timeDisplay = 1;
var timeDisplay = $gameVariables.value(8);

(i am not sure, just an idea)

I have tried that, but every time it gets to the $gameVariables it resets to null.

I had thought of maybe setting the variable to positive number in the script before hand, but I'm not sure how to do that to be honest.
I have tried that, but every time it gets to the $gameVariables it resets to null.

I had thought of maybe setting the variable to positive number in the script before hand, but I'm not sure how to do that to be honest.
what does the gamevariable store?
and could you show the plugin itself?
what does the gamevariable store?
and could you show the plugin itself?

The gamevariable stores time data in seconds. Essesntially creating a displayed clock for game time.

I haven't gotten very far due to this set back but here is what I have started with:

Code:
(function() {
    
    var params = PluginManager.parameters("NSS-IGT_System");
    
    var select = params["Visibility"];
    var select2 = params["Options Display"];
    var select3 = params["Format"];
    
    console.log(select);
    console.log(select2);
    console.log(select3);
    
    var timeDisplay = $gameVariables.value(8);
    
    var lh = Window_Base.prototype.lineHeight()*2;
    

    
    function My_Window() {
        this.initialize.apply(this, arguments);
    }
    
    My_Window.prototype = Object.create(Window_Base.prototype);
    My_Window.prototype.constructor = My_Window;
    
    My_Window.prototype.initialize = function() {
        Window_Base.prototype.initialize.call(this, 0, 0, 400, lh);
        this.refresh();
    }
    
    My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText(timeDisplay, 0, 0);
    }
    
    var smstart = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        smstart.apply(this, arguments);
        this.my_window = new My_Window();
        this.addChild(this.my_window);
    }
    
 })();
The gamevariable stores time data in seconds. Essesntially creating a displayed clock for game time.

I haven't gotten very far due to this set back but here is what I have started with:

Code:
(function() {

    var params = PluginManager.parameters("NSS-IGT_System");

    var select = params["Visibility"];
    var select2 = params["Options Display"];
    var select3 = params["Format"];

    console.log(select);
    console.log(select2);
    console.log(select3);

    var timeDisplay = $gameVariables.value(8);

    var lh = Window_Base.prototype.lineHeight()*2;



    function My_Window() {
        this.initialize.apply(this, arguments);
    }

    My_Window.prototype = Object.create(Window_Base.prototype);
    My_Window.prototype.constructor = My_Window;

    My_Window.prototype.initialize = function() {
        Window_Base.prototype.initialize.call(this, 0, 0, 400, lh);
        this.refresh();
    }

    My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText(timeDisplay, 0, 0);
    }

    var smstart = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        smstart.apply(this, arguments);
        this.my_window = new My_Window();
        this.addChild(this.my_window);
    }

})();

i cant see a part where you store the plugin Variables.. give me 15-30min i will look into some other plugins and compare some stuff.. i am pretty sure we can figure this out..


Edit
also for making new plugins, you can use this blank plugin as base..


I will edit your plugin with that blank plugin and add plugin variables.. perhaps that helps.. but it might take a bit time.. i am slow^^
@ mods
sry for double posting i know its bad.. i wanted to make sure its recognized..

@NoShirtstann

I think this should solve it.. but you have to insert the $gamevariable_id into the plugin param in your pluginmanager

i edited mostly 2 things.. added your variable into the param..
and added your plugin name into the standart parameters which i use for plugins..
In other words ,i combined your plugin with my "Blank plugin"..

you still have to insert your autor name & edit whatever is needed
(asuming that the variable works now.. i didnt test it)


edit


let me know if it works^^
@ mods
sry for double posting i know its bad.. i wanted to make sure its recognized..

@NoShirtstann

I think this should solve it.. but you have to insert the $gamevariable_id into the plugin param in your pluginmanager

i edited mostly 2 things.. added your variable into the param..
and added your plugin name into the standart parameters which i use for plugins..
In other words ,i combined your plugin with my "Blank plugin"..

you still have to insert your autor name & edit whatever is needed
(asuming that the variable works now.. i didnt test it)


edit

let me know if it works^^

Just had to change some declarations further down in the code. But it did work.

That worked very well actually. Only issue now is the refresh as the variable changes. But that I can figure out. Thank you very much!
The gamevariable stores time data in seconds. Essesntially creating a displayed clock for game time.

I haven't gotten very far due to this set back but here is what I have started with:

Code:
(function() {
 
    var params = PluginManager.parameters("NSS-IGT_System");
 
    var select = params["Visibility"];
    var select2 = params["Options Display"];
    var select3 = params["Format"];
 
    console.log(select);
    console.log(select2);
    console.log(select3);
 
    var timeDisplay = $gameVariables.value(8);
 
    var lh = Window_Base.prototype.lineHeight()*2;
 

 
    function My_Window() {
        this.initialize.apply(this, arguments);
    }
 
    My_Window.prototype = Object.create(Window_Base.prototype);
    My_Window.prototype.constructor = My_Window;
 
    My_Window.prototype.initialize = function() {
        Window_Base.prototype.initialize.call(this, 0, 0, 400, lh);
        this.refresh();
    }
 
    My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText(timeDisplay, 0, 0);
    }
 
    var smstart = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        smstart.apply(this, arguments);
        this.my_window = new My_Window();
        this.addChild(this.my_window);
    }
 
})();
this happen because when you declare
Code:
var timeDisplay = $gameVariables.value(8);
the $gameVariables(8) haven't even created yet...

so like @Dopan said you need to store variable id in plugin parameter instead and then when you need that value... just use:
Code:
$gameVariables.value(timeDisplay);
to access it as variable value.

for example using example in your code:
Code:
   My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplay), 0, 0);
    }

hope this help.
this happen because when you declare
Code:
var timeDisplay = $gameVariables.value(8);
the $gameVariables(8) haven't even created yet...

so like @Dopan said you need to store variable id in plugin parameter instead and then when you need that value... just use:
Code:
$gameVariables.value(timeDisplay);
to access it as variable value.

for example using example in your code:
Code:
   My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplay), 0, 0);
    }

hope this help.

So I've done that and adjusted my calls, and set my plugin parameters up but my screen is only displaying 1 variable, and its constantly set at zero. It seems to not be grabbing the value of the variable in game.

Code:
//=============================================================================
// NSS-IGT_System.js
//=============================================================================
/*:
* @plugindesc This plugin, is designed to be an In Game Time system.
*
* @author NoShirtStan
*
* @help
*
* ============================================================================
* Introduction
* ============================================================================
*
* TERMS OF USE
* What people who use your plugin are allowed to do with it.
*
* COMPATIBILITY
*
* @param Visibility
* @desc Choose to display the time in game
* @type select
* @option Display
* @option Do not Display
* @default Display
*
*
* @param Options Display
* @desc Choose to display in options menubar
* @type select
* @option Display
* @option Do Not Display
* @default Display
*
* @param Format
* @type select
* @option 12-Hour
* @option 24-Hour
* @default 24-Hour
*
* @param Default Time ID Minutes
* @desc Variable ID for Minutes
* @type variable
* @default 8
*
* @param Default Time ID Hours
* @desc Variable ID for Hours
* @type variable
* @default 9
*
*
*/

(function() {
  
    var parameters = PluginManager.parameters("NSS-IGT_System") || $plugins.filter(function (plugin) { return plugin.description.contains('<NSS-IGT_System>'); });

    var timeDisplayM = Number(parameters['Default Time ID Minutes'] || 0);
    var timeDisplayH = Number(parameters['Default Time ID Hours'] || 0);
  
    var lh = Window_Base.prototype.lineHeight()*2;

    console.log(timeDisplayH);
    console.log(timeDisplayM);
  
    function My_Window() {
        this.initialize.apply(this, arguments);
    }
  
    My_Window.prototype = Object.create(Window_Base.prototype);
    My_Window.prototype.constructor = My_Window;
  
    My_Window.prototype.initialize = function() {
        Window_Base.prototype.initialize.call(this, 0, 0, 400, lh); 
                this.refresh();
                console.log(timeDisplayH);
                console.log(timeDisplayM);
    }
  
    My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplayH), 0, 0);
    }
  
    My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplayM), 100, 0);
    }
  
    var smstart = Scene_Map.prototype.start;
    Scene_Map.prototype.start = function() {
        smstart.apply(this, arguments);
        this.my_window = new My_Window();
        this.addChild(this.my_window);
    }
  
})();

1610311890177.png
You have defined the refresh method twice: the second definition will replace the first one. That's why you're only seeing one value.

Also, I don't think the window will automatically refresh every frame: you have to tell it to refresh when appropriate. Example:
JavaScript:
var gmrefresh = Game_Map.prototype.refresh;
Game_Map.prototype.refresh = function() {
  gmrefresh.apply(this, arguments);
  var w = SceneManager._scene.my_window;
  if (w) w.refresh();
};
yes like @caethyril said... you define refresh method twice...
you could just combine it to:
Code:
   My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplayH), 0, 0);
    this.drawText($gameVariables.value(timeDisplayM), 100, 0);
    }
also... if you want it to update all the time... i think scene map update method is where you put that code instead...
but some extra check would be required... so if the value of the variable did not change... don't refresh...
and only refresh the window when the variable changed. if you always refresh it will cause lag and at least fps drop.
for example in your code it will be something like:
Code:
   var smupdate = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function() {
        smupdate.call(this);
        if(!this.my_window) return;
        if(this._oldTimeH && this._oldTimeH !== $gameVariables.value(timeDisplayH)){
            this.my_window.refresh();
        };
        if(this._oldTimeM && this._oldTimeM !== $gameVariables.value(timeDisplayM)){
            this.my_window.refresh();
        };
        this._oldTimeH = $gameVariables.value(timeDisplayH);
        this._oldTimeM = $gameVariables.value(timeDisplayM);
    }
above code have some several 'check'
if the this.my_window not exist (not created yet) it will return
if this._oldTimeH have not assigned yet it will skip the refresh (first time)
if this._oldTimeH have value but different than variable timeDisplayH it will then refresh the window
if this._oldTimeM have not assigned yet it will skip the refresh (first time)
if this._oldTimeM have value but different than variable timeDisplayM it will then refresh the window
then the logic will be like this... it will check does the variable change or not. if changed refresh window.

i did not test above code btw... sorry if there's error.
hope this help.
@estriole: yea, that's why I suggested refreshing via the map rather than the scene's update method, since the map refreshes when event page conditions have changed (most often because a switch/variable has changed).
yes like @caethyril said... you define refresh method twice...
you could just combine it to:
Code:
   My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplayH), 0, 0);
    this.drawText($gameVariables.value(timeDisplayM), 100, 0);
    }
also... if you want it to update all the time... i think scene map update method is where you put that code instead...
but some extra check would be required... so if the value of the variable did not change... don't refresh...
and only refresh the window when the variable changed. if you always refresh it will cause lag and at least fps drop.
for example in your code it will be something like:
Code:
   var smupdate = Scene_Map.prototype.update;
    Scene_Map.prototype.update = function() {
        smupdate.call(this);
        if(!this.my_window) return;
        if(this._oldTimeH && this._oldTimeH !== $gameVariables.value(timeDisplayH)){
            this.my_window.refresh();
        };
        if(this._oldTimeM && this._oldTimeM !== $gameVariables.value(timeDisplayM)){
            this.my_window.refresh();
        };
        this._oldTimeH = $gameVariables.value(timeDisplayH);
        this._oldTimeM = $gameVariables.value(timeDisplayM);
    }
above code have some several 'check'
if the this.my_window not exist (not created yet) it will return
if this._oldTimeH have not assigned yet it will skip the refresh (first time)
if this._oldTimeH have value but different than variable timeDisplayH it will then refresh the window
if this._oldTimeM have not assigned yet it will skip the refresh (first time)
if this._oldTimeM have value but different than variable timeDisplayM it will then refresh the window
then the logic will be like this... it will check does the variable change or not. if changed refresh window.

i did not test above code btw... sorry if there's error.
hope this help.
@estriole: yea, that's why I suggested refreshing via the map rather than the scene's update method, since the map refreshes when event page conditions have changed (most often because a switch/variable has changed).

Yeah i get where you're coming from with the map update, but im not sure how to get the map scene to refresh the window.

I adjusted the coding to suit what I need, but the refresh is still not working. The displayed variables are still not changing.

Im thinking there has to be another way to force the window to refresh on timer basis.

I ran a seperate instance, where it refreshes if the value is greater than or equal to 0 and its kicking out the vars timeDisplayH & M as constantly 0.

I ended up getting it to work. (Kind of lol)

So the values now change no problem in the window. Just a few Minor details I need to work on to get it what I want.

Thank you guys for all your help!

For reference, here is the final working(kind of) code.

Code:
(function() {
    
    var parameters = PluginManager.parameters("NSS-IGT_System");

    var timeDisplayM = Number(parameters['Minutes Variable'] || 0);
    var timeDisplayH = Number(parameters['Hours Variable'] || 0);
    
    var lh = Window_Base.prototype.lineHeight()*2;

    console.log(timeDisplayH);
    console.log(timeDisplayM);
    
    function My_Window() {
        this.initialize.apply(this, arguments);
    }
    
    My_Window.prototype = Object.create(Window_Base.prototype);
    My_Window.prototype.constructor = My_Window;
    
    My_Window.prototype.initialize = function() {
        Window_Base.prototype.initialize.call(this, 0, 0, 400, lh);   
                this.refresh();
    }
    
    My_Window.prototype.refresh = function() {
        this.contents.clear();
    this.drawText($gameVariables.value(timeDisplayH), 0, 0);
    this.drawText(":", 50, 0);
    this.drawText($gameVariables.value(timeDisplayM), 100, 0);
        if(this.timeDisplayM <= 9) {
            this.drawText("0", 90, 0);
        }
    }
    
    var smstart = Scene_Map.prototype.start;
    
    Scene_Map.prototype.start = function() {
        smstart.apply(this, arguments);
        this.my_window = new My_Window();
        this.addChild(this.my_window);
    }
    
    var smupdate = Scene_Map.prototype.update;
    
    Scene_Map.prototype.update = function() {
        smupdate.call(this);   
        if(!this.my_window) return;
        if(this._oldTimeH >= 0) {
            this.my_window.refresh();
            console.log(timeDisplayH);
        };
        if(this._oldTimeM >= 0) {
            this.my_window.refresh();
            console.log(timeDisplayM);
        };
        this._oldTimeH = $gameVariables.value(timeDisplayH);
        this._oldTimeM = $gameVariables.value(timeDisplayM);
    }
    
 })();

Original thread: https://forums.rpgmakerweb.com/threads/game-variable-call.131830/