Add a prototype methods

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,440
First Language
French
Primarily Uses
RMMV
hi am trying add a global prototype to all my object.

ex:
if I create some new object in $Gamevariable. for fun

Code:
var NewPid = 20;
$gameVariables.Hud_PinBoard = new Hud_PinBoard(NewPid);
var NewPid = Preload_Hud_PinBoard(); // return the new PID

$gameVariables.Hud_Displacement= new Hud_Displacement(NewPid);
var NewPid = Preload_Hud_Displacement(); // return the new PID

$gameVariables.Hud_Info= new Hud_Info(NewPid);
var NewPid = Preload_Hud_Info(); // return the new PID
now i have all my object with specifique proprety and method in
Code:
$gameVariables.Hud_PinBoard
$gameVariables.Hud_Displacement
$gameVariables.Hud_Info

Now I would like add a global prototype, for all those objects
example this methods
Code:
function A(a, b,c) {
this.XY = [a,b,c];
}
how i can add this method for make example this call operational
Code:
$gameVariables.Hud_PinBoard.A(a,b,c); // change the proprety this.XY in the object Hud_PinBoard
$gameVariables.Hud_Info.A(a,b,c); // change the proprety this.XY in the object Hud_Info
i try add this prototype
Code:
Game_Variables.prototype.A = function A(a, b,c) {
this.XY = [a,b,c];
}
ist work only if i call
Code:
$gameVariables.A()
but no if i call
Code:
$gameVariables.Hud_Displacement.A(); // return undefined
Why ?

Tell me if I'm not clear
thanks a lot to help me more understand js
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,961
First Language
English
Primarily Uses
RMMV
Adding a function to the Game_Variables prototype will make that function callable from any instance of it, as you've seen. The reason the second bit doesn't work is that Hud_Displacement doesn't have an A function. You would have to define A in that class for it to be callable this way.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,440
First Language
French
Primarily Uses
RMMV
Adding a function to the Game_Variables prototype will make that function callable from any instance of it, as you've seen. The reason the second bit doesn't work is that Hud_Displacement doesn't have an A function. You would have to define A in that class for it to be callable this way.
Yes but should not it inherit methods of gameVariables?
because i create it in $gameVariables at start
$gameVariables.Hud_Displacement= new Hud_Displacement();
Of what I read, even if a method does not exist, javascript will search all the existing sub-methods of the object.
What would be the right way, because for now, I am forced to add it manually to each object, and I find it not super clean.

The real context is this.
Boot Game
Code:
// les item action son mintenant lier a des etiquette dans le menue
//-----------------------------------------------------------------------------
/////////////CONSTRUCTOR OBJET DEPENDANCE (WHEN GAME BOOT)/////////////////////
/////Tous les objets, stokage variable permanante nessesaire a ce plugin //////
//-----------------------------------------------------------------------------
// ici on ajoute tous les besoin constructeur au boot.
alias_HUDSTATS_initialize = DataManager.setupNewGame;
DataManager.setupNewGame = function() {
    alias_HUDSTATS_initialize.call(this);
    // LES PLUGIN
    $gameVariables.Avatar1 = new Avatar1(); //built avatar 1
    $gameVariables.Avatar2 = new Avatar2(); //built avatar 2
    /*
    L'ordre des builts ici a une important carlinitialisation renvoi le pid final dans objets
     une foi lorbjet pid final existe il reste permanent
    */
    var NewPid = 20; // pid de depart pour built tous les menue visuel // en theorit less 2 pid disponible entre chaque menue et hud
    // GIVE FINALPID IF FIRT TIME CONSTRUCTOR
    //Hud_PinBoard
    Hud_PinBoard.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_PinBoard.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_PinBoard = new Hud_PinBoard(NewPid);
    var NewPid = Preload_Hud_PinBoard(); // initialise et return End.Pid
    console.log('Preload_Hud_PinBoard EndNewPid: ', NewPid);


    //Hud_Displacement
    Hud_Displacement.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_Displacement.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_Displacement = new Hud_Displacement(NewPid);
    var NewPid = Preload_Hud_Displacement(); // initialise et return End.Pid
    console.log('Preload_Hud_Displacement EndNewPid: ', NewPid);

    //Hud_Info
    Hud_Info.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_Info.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_Info = new Hud_Info(NewPid);
    var NewPid = Preload_Hud_Info(); // initialise et return End.Pid
    console.log('Preload_Hud_Info EndNewPid: ', NewPid);

    //Hud_States
    Hud_States.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_States.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_States = new Hud_States(NewPid);
    var NewPid = Preload_Hud_States(); // initialise et return End.Pid
    console.log('Preload_Hud_States EndNewPid: ', NewPid);

    console.log('window: ', window); // HELPER CONSOL

};
The method i tries to add to all my object Without doing it manually, with prototype is
Code:
    //STOP ANIMATION
    //OBJECT ARE VAR IN $gameVariables:: PBoard, DHud, HStat, HInfo, Avatar ....
    //RequestStopAnimation.call(Object)
    function RequestStopAnimation() {
        for (var i = 0; i < this.LavraAnime.length; i++) {
            this.LavraAnime[i].stop();
        }
    }
    //CREATE ANIMATION IN [ARRAY]
    //OBJECT ARE VAR IN $gameVariables:: PBoard, DHud, HStat, HInfo, Avatar ....
    //MoveAnime.call(Object,param1, param2,)
    function MoveAnime(PID, Proprety, EaseType, EaseMode, TimeMove, Delay) {
        if (!(Proprety instanceof Array)) {
            this.LavraAnime.push(new IAVRA.ANIMATE.Tween($gameScreen.picture(PID), Proprety).easing(IAVRA.EASING[EaseType][EaseMode]).duration(TimeMove).delay(Delay).start())
        } else {
            this.LavraAnime.push(new IAVRA.ANIMATE.Tween($gameScreen.picture(PID), Proprety[0]).easing(IAVRA.EASING[EaseType][EaseMode]).duration(TimeMove).delay(Delay).chain
            (new IAVRA.ANIMATE.Tween($gameScreen.picture(PID), Proprety[1]).easing(IAVRA.EASING[EaseType][EaseMode]).duration(TimeMove)).start())
        }
    }
    //TIME ANIMATION CALCULATOR MS
    function SpeedFrameForMove(TimeMove) {
        return ((TimeMove * 1000) / 60);
    }

And this is a example of one of object created.
with the proprety this.LavraAnime, And I want to call the method to modify the objects consequently
Code:
//-----------------------------------------------------------------------------
/////////////CONSTRUCTOR OBJET DEPENDANCE (WHEN GAME BOOT)/////////////////////
/////Tous les objets, stokage variable permanante nessesaire a ce plugin //////
//-----------------------------------------------------------------------------
// Built 1 time at BOOTGAME or NEWGAME
// $gameVariables.Hud_Displacement
// Objet parametre Hud_Displacement, tous les objets important du Hud_Displacement, + objet predefenie ou variable
function Hud_Displacement(NewPid) {
    //STATUS
    this.Status = function () { if($gameScreen._pictures[this.Pid()] !== undefined) { return true; } return false; } //Check si initialiser(PreLoad) true sinon false
    this.PositionMode = null; // THE CURRENT HUD POSITION (Null: DELETED IN MEMORY, 'HideXY': HIDE, 'MapBaseXY': SHOW...)
    //--PICTURE ID
    this.Pid = function () { return this.NewPid }; // RETURN INITIAL PID
    this.NewPid = NewPid; // GET THE PID PROPRETY FROM THE BOOT CONSTRUCTOR: ANFT_BOOTGAME.js
    this.EndPid = null; // GET THIS VALUR AT END OF PRELOAD_FUNCTION
    //--GLOBAL POSITION [x,y]
    this.HideXY = [-50, 980]; // POSITION FOR TEMPORY HIDDING HUD
    this.MapBaseXY = [100, 800]; // DEFAULT POSITION FOR GAME PLAY
    this.SelectOptionXY = [700, 300] // POSITION WHEN SELECT OPTION HUD ARE SHOW
    //--TIMER
    this.Timer = null; // ALL TIMER RELATED TO THIS HUD IF NEED
    //--FUNCTION MOVE LAVRA
    this.LavraAnime = []; // Stoke les animation easing
    //SLOT ITEM
    this.SlotPos = [[-65,39],[0,65],[65,39]]
    //--FUNCTION DE CALCUL ET RETOUR
    //--// PIXITEXT MODELE
    // pour LV,
    this.TextStyle1 = { align: 'center',fontSize: 38,fontWeight: 'bold',strokeThickness: 8 }
    // SOUS LE STAMINA INFO RESET ... ?
    this.TextStyle2 = { align: 'center',fontSize: 12,fontWeight: 'bold',strokeThickness: 6 }
}
//---
 
Last edited:

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,961
First Language
English
Primarily Uses
RMMV
Hud_Displacement would only inherit functions of gameVariables if the class itself used the prototype of Game_Variables as its own prototype, which it doesn't. I think you're misunderstanding how prototypes work.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,440
First Language
French
Primarily Uses
RMMV

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
I use prototypes all the time, but I still don't get how they work. To me it's just another argument on the wrong side of the function.

PHP:
String.prototype.fiveTimes = function(){
   return this + this + this + this + this;
}
PHP:
"IDK ".fiveTimes();
// returns "IDK IDK IDK IDK IDK "
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,440
First Language
French
Primarily Uses
RMMV
I use prototypes all the time, but I still don't get how they work. To me it's just another argument on the wrong side of the function.

PHP:
String.prototype.fiveTimes = function(){
   return this + this + this + this + this;
}
PHP:
"IDK ".fiveTimes();
// returns "IDK IDK IDK IDK IDK "
,
I understand, but in the idea, I would have liked to create a new prototype in $gameVariables
So that any new object created after in $gameVariables, will also inherit from this new prototype

For the moment I am obliged to do it manually, on each object.
But I would like to include it globally only once.

Code:
//Hud_PinBoard
    Hud_PinBoard.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_PinBoard.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_PinBoard = new Hud_PinBoard(NewPid);
    var NewPid = Preload_Hud_PinBoard(); // initialise et return End.Pid

    //Hud_Displacement
    Hud_Displacement.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_Displacement.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_Displacement = new Hud_Displacement(NewPid);
    var NewPid = Preload_Hud_Displacement(); // initialise et return End.Pid

    //Hud_Info
    Hud_Info.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_Info.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_Info = new Hud_Info(NewPid);
    var NewPid = Preload_Hud_Info(); // initialise et return End.Pid

    //Hud_States
    Hud_States.prototype.RequestStopAnimation =  RequestStopAnimation; // PROTOTYPE
    Hud_States.prototype.MoveAnime =  MoveAnime; // PROTOTYPEA
    $gameVariables.Hud_States = new Hud_States(NewPid);
    var NewPid = Preload_Hud_States(); // initialise et return End.Pid
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
Is this what you mean by manually? This is the extent of my knowledge...
PHP:
for(prop in $gameVariables){
// $gameVariables[prop].Hud_States = ... 
}
 

Clock Out

Veteran
Veteran
Joined
Jun 14, 2016
Messages
92
Reaction score
45
First Language
English
Primarily Uses
RMMV
Gotta setup the prototype chain.

Code:
function HudBase() {
  this.initialize.apply(this, arguments);
}

HudBase.prototype.initialize = function (pid) {
  this.pid = pid;
};

HudBase.prototype.requestStopAnimation = function () {
  // code
};

HudBase.prototype.moveAnime = function () {
  // code
};

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

HudPinBoard.prototype = Object.create(HudBase.prototype);

// HudPinBoard.prototype...

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

HudDisplacement.prototype = Object.create(HudBase.prototype);

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

HudInfo.prototype = Object.create(HudBase.prototype);

// HudInfo.prototype...

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

HudStates.prototype = Object.create(HudBase.prototype);

// HudStates.prototype...

var someGlobalObject = {};
someGlobalObject.pinBoard = new HudPinBoard(1);
someGlobalObject.displacement = new HudDisplacement(1);
someGlobalObject.info = new HudInfo(1);
someGlobalObject.states = new HudStates(1);

The Hud functions all link to HudBase.prototype which gives objects they create access to the HudBase methods.
 
Last edited:

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,440
First Language
French
Primarily Uses
RMMV
hey thank you i understand now
thanx friend
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
You've obliterated my understanding of javascript.

I'm like: this is cool because you can't clone deep objects, but only alias them, so when do this you create new objects it's like yay!

But then I'm like I can save a whole lot of spaghetti memory with just 2 or 3 simple master functions and some dynamic argument handling.
 

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

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,476
Members
137,824
Latest member
dobratemporal
Top