How to include/show variables from Plugin into ingame?

● ARCHIVED · READ-ONLY
Started by Flicker789 6 posts View original ↗
  1. Hi guys, so I managed to create an "test.js" file.

    In this JS file, I have a function "function go(){ var test = "message";}

    My problem now is, I don't get it how can I show this content of my variable "test" ingame?

    Example: Hello $test how are you today?

    How can I print or show this variable which is in my plugin into my game?

    Thanks in advance <3
  2. PHP:
    function go(){ var test = "message"; return test;};
    console.log(go());
  3. @Jonforum he wants to show ingame, not in console
    @Flicker789 you need to transfer the value into one of the existing game variables and then use the textcodes for variables in show text.
  4. Flicker789 said:
    In this JS file, I have a function "function go(){ var test = "message";}
    This won't work because test only exists within the function, so as soon as you exit the function, the variable is gone.

    Instead you need a global variable, like this:
    Code:
    function go() {
        test = "message";
    }
    By not writing "var" in front, the variable becomes global.

    Or better yet, since other plugins might also need a global test variable, do this:
    Code:
    MyPlugin = {};
    function go() {
        MyPlugin.test = "message";
    }
    "MyPlugin" here is intended as a namespace. It's basically a name that you're certain no other plugin will use, so no other plugin will overwrite it.
    In your case, you can use "Flicker789" instead of "MyPlugin". If you intend to share variables between your plugins, you can also do this:
    Code:
    Flicker789 = Flicker789 || {}; // makes sure you don't overwrite it if it already exists
    Flicker789.MyAwesomePluginName = {};
    
    function go() {
        Flicker789.MyAwesomePluginName.test = "message";
    }
    You get the idea.


    Next, in order to show the variable's value in a message window, you need to put the variable's value into an RMMV variable.
    Use "Control Variables" -> "Set" -> "Script" -> "test" or "MyPlugin.test" (without the quote marks).

    In the message window, you'd write:
    Code:
    Hello \v[1] how are you today?
    Assuming you stored test into variable 1.


    The easiest way to deal with this, however, is to reserve a variable for your plugin to use, then write directly into that variable:
    Code:
    function go() {
        $gameVariables[1] = "message";
    }
    This writes "message" into variable 1, which you can then access with \v[1].
  5. Andar said:
    @Jonforum he wants to show ingame, not in console
    @Flicker789 you need to transfer the value into one of the existing game variables and then use the textcodes for variables in show text.
    I understand but he got an answer depending on the time he put to properly format his issue.

    If he wants a more detailed answer with a broader context, he has to reformat his question and give more details about his objective.
    Can not read in people's minds.
  6. Nolonar said:
    This won't work because test only exists within the function, so as soon as you exit the function, the variable is gone.

    Instead you need a global variable, like this:
    Code:
    function go() {
        test = "message";
    }
    By not writing "var" in front, the variable becomes global.

    Or better yet, since other plugins might also need a global test variable, do this:
    Code:
    MyPlugin = {};
    function go() {
        MyPlugin.test = "message";
    }
    "MyPlugin" here is intended as a namespace. It's basically a name that you're certain no other plugin will use, so no other plugin will overwrite it.
    In your case, you can use "Flicker789" instead of "MyPlugin". If you intend to share variables between your plugins, you can also do this:
    Code:
    Flicker789 = Flicker789 || {}; // makes sure you don't overwrite it if it already exists
    Flicker789.MyAwesomePluginName = {};
    
    function go() {
        Flicker789.MyAwesomePluginName.test = "message";
    }
    You get the idea.


    Next, in order to show the variable's value in a message window, you need to put the variable's value into an RMMV variable.
    Use "Control Variables" -> "Set" -> "Script" -> "test" or "MyPlugin.test" (without the quote marks).

    In the message window, you'd write:
    Code:
    Hello \v[1] how are you today?
    Assuming you stored test into variable 1.


    The easiest way to deal with this, however, is to reserve a variable for your plugin to use, then write directly into that variable:
    Code:
    function go() {
        $gameVariables[1] = "message";
    }
    This writes "message" into variable 1, which you can then access with \v[1].


    This was a good explanation! Thank you so much <3