MV Setting in-game variables to mouse cursor's x/y positions (without clicking)

Hello, I am using information from this thread:


JavaScript:
    TouchInput._onMouseMove = function(event) {
            var x = Graphics.pageToCanvasX(event.pageX);
            var y = Graphics.pageToCanvasY(event.pageY);
            this._onMove(x, y);
            console.log(x+' '+y);
    };

I have tried using $gameVariables.setValue(id, value); but keep getting 0's. I am trying to do it within the "Script" event command. I tried pasting it in a plugin but couldn't get it to work.

How am I able to set in-game variables to the mouse cursor's x/y positions (without clicking)?
I tested this code in MZ and it worked as is. I also looked at the MV code, and this plugin should also work in MV.

You say you "tried using $gameVariables.setValue(id, value);", but I don't see it in your code, so I have to assume that you might have used it wrong.

JavaScript:
TouchInput._onMouseMove = function (event) {
    var x = Graphics.pageToCanvasX(event.pageX);
    var y = Graphics.pageToCanvasY(event.pageY);
    this._onMove(x, y);
    console.log(x+' '+y);
    $gameVariables.setValue(1, x);
    $gameVariables.setValue(2, y);
};
would store the value of x in Variable #0001 and the Value of y in variable #0002.


EDIT:
I just realized that maybe the problem is unrelated to the plugin itself.
Have you actually enabled the plugin when you were testing it?
Thanks, it works now!

However, sometimes when I boot up the game in test mode, it gives the error on the startup:

"Error
Uncaught TypeError: Cannot read property 'setValue' of null"

When I refresh with F5, it proceeds to the title screen normally.

Is this because the plugin is trying to assign a value to "$gameVariables" right on startup when the game hasn't started yet? How can I only have it trigger only when the game has started (past the title screen)? Not sure if thats even why its happening.

EDIT:
Tried using information from this thread but can't get it to work:


Code:
var Mouseloc = Mouseloc || {};    // namespace for your plugins, equal to itself if already defined or an empty object otherwise
Mouseloc.eleCalc = {};        // namespace for persistent properties of this plugin

(function($) {            // add parameter $ to represent the plugin namespace (see final line)

    "use strict";

    // Stuff here will run on game start, when index.html loads.

    var params = PluginManager.parameters('Mouselocation3');
    // define persistent stuff as properties of the namespace, e.g.
    $.test = params['test number to display'];
    // Other parameter stuff here...

    // Define a function on your global namespace.
    $.TouchInput._onMouseMove = function (event) {
        // the code in here will run when this function is called
    var x = Graphics.pageToCanvasX(event.pageX);
    var y = Graphics.pageToCanvasY(event.pageY);
    this._onMove(x, y);
    console.log(x+' '+y);
    $gameVariables.setValue(1, x);
    $gameVariables.setValue(2, y);
    };

})(Mouseloc.eleCalc);        // pass the plugin's namespace in as the first parameter, $

I used the script call below but it did nothing:

Code:
Mouseloc.eleCalc.TouchInput._onMouseMove();
Is this because the plugin is trying to assign a value to "$gameVariables" right on startup when the game hasn't started yet?
Almost. When the game hasn't started yet, $gameVariables is still null. The problem isn't because you're trying to assign a value to it, but because you're trying to execute a function (setValue()). Since null doesn't have any functions, the code crashes.

The easiest way to fix this, is to only call that function when $gameVariables is not null:
JavaScript:
if (gameVariables) {
    $gameVariables.setValue(1, x);
    $gameVariables.setValue(2, y);
}

The full code would be:
JavaScript:
TouchInput._onMouseMove = function (event) {
    var x = Graphics.pageToCanvasX(event.pageX);
    var y = Graphics.pageToCanvasY(event.pageY);
    this._onMove(x, y);
    console.log(x+' '+y);
    if (gameVariables) {
        $gameVariables.setValue(1, x);
        $gameVariables.setValue(2, y);
    }
};
Awesome, that's a lot easier! Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/setting-in-game-variables-to-mouse-cursors-x-y-positions-without-clicking.143201/