Add a prototype to $dataItems [SOLVED]

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
Hi
Is possible to change the prototype of $dataItems
because i need to call fast function to build some regexr when is nessesery.

Or I would be better to create a new $variable in window, that when i will calling, will indirectly modify the $dataItems[]?
ex:
Code:
function itemsManage(){};
$items = new itemsManage();

itemsManage.prototype.doSomething = function(AddTXT) {

}
I looked at how the $dataItems semm to work, and I can not find the constructor and how is added to eatch array at gameBoot.
Do you have any idea about the line i need to check?
thank
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
$dataItems is an array, not a class. There is no prototype, and no constructor.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
$dataItems is an array, not a class. There is no prototype, and no constructor.
I understand, I express myself badly, I speak in fact of what is found in the array.
aefaefae.jpg
the proto are OBJECT,
I wanted to know if it was possible to easily modify it to add additional functions.

But I think rather opt for the second solution.
I'm not like too mutch, to create a new variable just to add some (1 or 2) complementary function.

Code:
function itemsManage(){};
$items = new itemsManage();

itemsManage.prototype.doSomething = function(ItemID) {

}
_alias_DataItem_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _alias_DataItem_createGameObjects.apply(this, arguments);
    $items        = new itemsManage();
};
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
I wonder if you can add your function to the Object class
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
you can use .map to quickly replace $dataItems with a new list using your prototype.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
I wonder if you can add your function to the Object class
According to my reading on the bases in javascript, it is better to avoid modifying the default class and library of javascript.

In fact I'm just looking to find the line that build this object in the array.
But without success.
I may do a bad search. !

idealy is for example, call $dataItems like this.
$dataItems[15].Dosomething('');
Otherwise, other way, I think it's not bad, but I'll have to call it by create a new variable.
$items(id).dosomething('');
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
i dont know .map, i will look about it thanks.
Basically you would do something like this:

Code:
$dataItems = $dataItems.map(function(item){
  if (item) {
    return new ItemClass(item);
  } else {
    return item;
  }
});
Then make your ItemClass with such a constructor:

Code:
function ItemClass(itemData) {
  for (var key in itemData) {
    if (itemData.hasOwnProperty(key) {
      this[key] = itemData[key];
    }
  }
}
I didn't test it here, but basically it would replace all items on $dataItems with an instance of your ItemClass. And the constructor of that class is just copying the data from the old item to the instance.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
The line that builds the object into the array doesn't exist, because the array is created by the MV editor.
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,545
Reaction score
3,715
First Language
Java's Crypt
Primarily Uses
RMMZ
This should do:

Code:
var oldDataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
  oldDataManager_onLoad.apply(this, arguments);

  if (object == $dataItems) {
    $dataItems = $dataItems.map(function(item){
      if (item) {
        return new ItemClass(item);
      } else {
        return item;
      }
    });
  }
};
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
This should do:

Code:
var oldDataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
  oldDataManager_onLoad.apply(this, arguments);

  if (object == $dataItems) {
    $dataItems = $dataItems.map(function(item){
      if (item) {
        return new ItemClass(item);
      } else {
        return item;
      }
    });
  }
};
After doing a bit of reading on the .map, the idea is not bad but I think this will cause some performance problem.
This however gave me another idea.
I understood why the all $data not have constructor, simply because it is created by the the .json from the sofware rmmv database!

I made a small code, which changes the prototype of $dataItems
So when the game boot , or load save game , is change one time the prototype.
According to my test this work fine, maybe there are still many other ways to do it.
aecfava.jpg

Code:
function buildNewDataItemClass(){
    // make temp copy of $dataItems
    for(var i=0,itemTemp=[],max=$dataItems.length;i<max;i++){
        itemTemp[i] = Object.assign({}, $dataItems[i]);
    }
    for(var i=0,max=itemTemp.length; i<max; i++){
       $dataItems[i] = new itemsData(); // assign new class itemsData to $dataItems[i]
            for (var data in itemTemp[i]) {
                $dataItems[i][data] = itemTemp[i][data]; // replace data
            }
    }
}
function itemsData(){}; // new class for $dataItems

_alias_DataItem_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _alias_DataItem_createGameObjects.apply(this, arguments);
    buildNewDataItemClass();
  
};
I hope not to encounter a problem this way
It seems correct. !?
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
@Hudell I had not seen your last proposal, I will try it.
 

Jonforum

Veteran
Veteran
Joined
Mar 28, 2016
Messages
1,623
Reaction score
1,439
First Language
French
Primarily Uses
RMMV
This should do:

Code:
var oldDataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
  oldDataManager_onLoad.apply(this, arguments);

  if (object == $dataItems) {
    $dataItems = $dataItems.map(function(item){
      if (item) {
        return new ItemClass(item);
      } else {
        return item;
      }
    });
  }
};
Thank you infinitely for your solution hudell.
It is very ingenious, I just replace it, and it works wonderfully.
I think I'm going to learn more .map, because I did not get it in this that way.

[SOLVED]
 

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

Latest Threads

Latest Profile Posts

Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:

Forum statistics

Threads
105,854
Messages
1,017,004
Members
137,562
Latest member
tamedeathman
Top