Create and keep a global variable for outside plugin

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
hi guys .


How i can create a global variable in a plugin for use outside with a script call. ?


ex: in plugin i have this


var Itemdescr = []; // the global saved variable array initialise
// and this
Itemdescr[Pid]=Listi;


after in game i have out script call in game , and i need to get the variable


Itemdescr = Itemdescr[$gameVariables.value(2)];
Textitems = String($dataItems[Itemdescr].name); // on va chercher le nom fichier
TheiconIndex = String($dataItems[Itemdescr].iconIndex)
Textitems = Textitems.split(' - '); // on le découpe en 2 iconIndex
$gameScreen.setDTextPicture('\\OW[5] ' + Textitems[0] + ' ' Textitems[1] + '\\i['+TheiconIndex+']', 20); //plugincall dtext
$gameScreen.setFont('GameFont2');


But the var array Itemdescr[xxx] don't exist outside ?


Do you have a trick to do this ?
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,787
Reaction score
12,217
First Language
English
Primarily Uses
RMMV
Where do you have it in the plugin?  What's the context?  That determines whether you can, and how you can, access it from outside.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,612
Reaction score
1,407
First Language
French
Primarily Uses
RMMV
Where do you have it in the plugin?  What's the context?  That determines whether you can, and how you can, access it from outside.






here my plugin


the variable array Itemdescr[Pid]=Listi; are creat in the loop at line 83.


After when i clic on picture, in common event, i use a script call , and  i need to get the variable Itemdescr[1], Itemdescr[2] , Itemdescr[3] .....


For found the item name , id and description base on the ID of the picture.


Ex: if click on picture ID 3


get valur of the Itemdescr[3] 

Code:
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
    // to be overridden by plugins
    _Game_Interpreter_pluginCommand.call(this, command, args);
    if (command === 'menuobj') {
        var Pid = 100; // picture ID for clic, 100 + max 36 Max 200
        var ItemtotalDB = $dataItems.length; // Maximum item dans le DB rpgmaker
        var Itemtotalposs = 0; // Nombre objet posseder, on int, car ont veut faire un calcul eatch/pass plugin called
        var Objetposseder = []; // creer une variable poru chaque item avec valeur
        var i = 1; // représente page pour listi 
		$gameVariables.setValue(38, i);// Int la VAr38 car i doi avoir page 1 initial. (bug corr)
        var Pagevar = $gameVariables.value(34);
		var Listi = 0; 
		var ListiP = [];// ou doi commencer le listi a chaque page
        for (Listi = 1; Listi < ItemtotalDB; Listi++) {
			$gameScreen.erasePicture(Pid);$gameScreen.erasePicture(Pid + (36 * 2));$gameScreen.erasePicture(Pid + (36 * 3));
			Pid++;
            if ($gameParty.hasItem($dataItems[Listi]) == true) {
                 Objetposseder[Listi] = Listi; //create variable based on DB Objetposseder1=2,Objetposseder2=2,Objetposseder3=3...
                Itemtotalposs++; //calcul global total objet poseder		
                if (Itemtotalposs == 37 || Itemtotalposs == 73 || Itemtotalposs == 108 || Itemtotalposs == 144 || Itemtotalposs == 180) { //a chaque +1 que 36 Itemtotalposs = 1 nouvelle page                    
					i++;
					$gameVariables.setValue(38, i); // NB de page total
                     ListiP[i] = Listi; // pour page 2,3,4..., on sait que on doi comment la list a cette endroit . 
                }
            }
        }

		
		
		var Pid = 100; // picture ID for clic, 100 + max 36 Max 200
		$gameVariables.setValue(42, Itemtotalposs);
        if (Itemtotalposs == 0) {
            console.log('le joueur na pas objet');
        } // stop because no objet !!}
        else {
            //page1
            var Xitem = 0; // X coor for the item + picturesize+ marge
            var Yitem = 0; // Y coor for the item + picturesize+ marge
            var InpicX = 88; // Initial X of menue item list
            var InpicY = 226; // Initial Y of menue item list
            var MargeX = 7; // Marge bettwenn 2 picture X 
            var MargeY = 6; // Marge bettwenn 2 picture Y
            var ISIZEX = 82; // X size of picture
            var ISIZEY = 82; // X size of picture
            var NBitempagemenu = 0;
			var Itemdescr = [];

            if (Pagevar == 1) {
                Listi = 1; // si sur page 1 , listi recommence le scan a 1
            } else {
                Listi = ListiP[Pagevar]; // si sur page 2 ou autre , listi recommence le scan a Listi[Pagevar]...[pagevariable dans jeux]
            }

            for (; Listi < ItemtotalDB; Listi++) { console.log ( Listi);
                if (Objetposseder[Listi] == Listi) {
                    //afficher objet

					Listi = String(Listi);  console.log('Detien objet '+ Objetposseder[Listi] + ' Listi est a ' + Listi); 
                    $gameScreen.showPicture(Pid, Listi, 1, InpicX + Xitem, InpicY + Yitem, 150, 150, 255, 0);
					$gameScreen.movePicture(Pid, 1, InpicX + Xitem, InpicY + Yitem, 100, 100, 255, 0, 18);
					Listi = Number(Listi); 
                    $gameScreen.setPictureCallCommon(Pid, 21, 1); // si clique sur item renvoi vers event.commun
                    Textitems = String($dataItems[Listi].name); // on va chercher le nom fichier 
                    Textitems = Textitems.split(' - '); // on le découpe en 2 
                    $gameScreen.setDTextPicture('\\OW[5] ' + Textitems[0] + '', 20); //plugincall dtext
					$gameScreen.setFont('GameFont');
                    $gameScreen.showPicture(Pid + (36 * 1), '', 0, InpicX + Xitem - 45, InpicY + Yitem-80, 100, 160, 50, 0); // affiche image a PID+36 (max72)
                    $gameScreen.setDTextPicture('\\OW[4] ' + Textitems[1] + '', 19); //plugincall dtext
                    $gameScreen.setFont('GameFont');
					$gameScreen.showPicture(Pid + (36 * 2), '', 0, InpicX + Xitem-41, InpicY + Yitem -80, 100, 20, 50, 0); // affiche D_text titre objet
					Textitems = $gameParty._items[Listi]; // Obtien le nombre objet posseder
					 console.log ( (Pid + (36 * 3)) + 'zzzzzzzzz');
					$gameScreen.setDTextPicture('\\OW[5] ' + Textitems + ' ', 17); //plugincall dtext
					$gameScreen.setFont('GameFont');
					$gameScreen.dTextBackColor = 'rgba(0, 0, 0, 0.5)';
                    $gameScreen.showPicture(Pid + (36 * 3), '', 0, InpicX + Xitem+21, InpicY + Yitem+15, 100, 100, 255, 0); // affiche image a PID+36 (max72)
					$gameScreen.movePicture(Pid + (36 * 1), 0, InpicX + Xitem - 45, InpicY + Yitem-46, 100, 100, 255, 0, 27);
					$gameScreen.movePicture(Pid + (36 * 2), 0, InpicX + Xitem-41, InpicY + Yitem -30, 100, 100, 255, 0, 20);
                    NBitempagemenu++
                    Pid++
                    Xitem += ISIZEX + MargeX;
					Itemdescr[Pid]=Listi;

                }
                if (NBitempagemenu == 38) {
                    console.log('On break  ligne 74 car NBitempagemenu= ' + NBitempagemenu); //afficher objet
                    break;
                } else if (NBitempagemenu == 12 || NBitempagemenu == 25) {
                    //Saut de ligne
					console.log('Osaut ligne caR ON EST A ' + NBitempagemenu);
                    Xitem = 0;
                    Yitem += ISIZEY + MargeY;
					NBitempagemenu++;
                }
            }
			console.log ( 'fin scan');
        }
    }
}
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,787
Reaction score
12,217
First Language
English
Primarily Uses
RMMV
itemdescr is defined inside your function, which means it's local to the function.  I notice also that you only do it within the else block of a condition, which means that if the condition is true, it won't be created anyway.  Try and define it right at the start, where you alias the plugin command.


tbh I'm not sure that will work - someone said a while ago that in javascript variables are handled differently to the way they are in RGSS.
 

Clock Out

Veteran
Veteran
Joined
Jun 14, 2016
Messages
92
Reaction score
44
First Language
English
Primarily Uses
RMMV
Omit the var keyword to a make a variable global. I don't favor that approach though since you might need to make more global variables in the future and there is no guarantee that someone else won't overwrite your variable by doing the same thing. One solution might be using the module pattern.


Module Pattern


A module is simply a immediately invoked function expression (IIFE) that returns an object to the caller. The benefit is that the programmer can make anything inside their module available to the outside from a single object. Here's a live example.


Code

Code:
// A module is a immedeatly invoked function expression (IIFE) that returns an
// object to the caller. The object has access to any variables or functions
// inside the module.

var MyPlugin = (function () {
    "use strict";
    // Variables declared here are local to the module.

    var Itemdescr = ["Item 0", "Item 1", "Item 2"];

    // Functions declared here are local to the module. They have access to
    // everything within the module.

    /**
     *  Returns a copy of the Itemdescr variable.
     *  @returns {Array}
     */
    function getCopyOfItems() {
        return Itemdescr.slice(0);
    }

    // Return an object to the caller. All properties of this object can now
    // be acessed by using MyPlugin.property and methods via MyPlugin.method().
    return {
        items: getCopyOfItems
    };
}());

console.log(MyPlugin.items());
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Kraken Art By @whtdragon

- For the No Travel Game Jam -
Yay! One of my Youtube videos got to 100 views (in 2 days)! This Youtube thing is great, it's like the profile posts except I get to annoy entertain more people at once! Now I just have to practice my "Leave a like and hit that Subscribe button" shilling technique. :LZSexcite:
Just found out there is an about me page. I updated it and happy the way its looking!
Ladies and Gentlemen, I can't act like things will be ok anymore. It needs to be said: We have a Pandemic...
Release version of Pillow Hero is out on windows now! --> https://bit.ly/2USHKLv

Forum statistics

Threads
95,558
Messages
930,091
Members
125,846
Latest member
StormyRange
Top