Modify weapon database In game

Hello, i'm trying to add weapon for my "handmade job system" on database in game like this:

weapon = $dataWeapons[2] // it's my "blank weapon"
createweapon = weapon
createweapon.id = $dataWeapons.length;
$dataWeapons[$dataWeapons.length] = createweapon //add newweapon on database
$gameVariables.setValue(241, createweapon)

ID Result is = 3001 here, everything is normal.

When i create a new weapon again, the console show me 3002, good again. But when i see back the previous id the "3001 id weapon" show the same 3002 ID too.

Same problem with $dataWeapons[2] is changed too... When i check the id, the console show "3002", everything who use my blank weapon have the same Id..

How i can add new weapon ingame without modifying my blank weapon, and avoiding the "all new weapon use the same ID"?
The problem here is that you cannot modify a blank weapon to create new ones. If you do, you are modifying a blank object, but it is still the same object every time. What you want to do is creating a new object and modify its properties, not modifying the property of an existing object.

In particular, JavaScript always operates by value, but when it comes to objects, the value itself is the reference to the object. In your code, the value you are assigning to weapon is the reference to the object stored in $dataWeapons[2]. The same applies when you set createweapon = weapon. So, when you use create_weapon.id you are referring to the id property of the object referenced by create_weapon, which is the same object stored in $dataWeapons[2]. The object is not going to change, you are just creating new variables that store the reference to the same object, it does not matter how many times you do it.

To create a new weapon object, you need the new keyword, which creates a new instance (object) of the weapon class. First of all, I recommend you understanding better how JavaScript and JavaScript objects work, after that, I warn you that what you are trying to do is more tricky than you think, and I absolutely recommend doing it with a plugin and planning it carefully. There are hidden issues lurking in what you are trying to do. This does not mean that you will incur in them, but it means that you have to be wary of what you are doing.

That said, you can learn how to create new instances of a certain class here.
You can't do that by storing stuff in $dataWeapons. Even if you managed to create unique weapons and store them in $dataWeapons, you'd lose everything once you exit the game. $dataWeapons is initialized every time you open the game and it simply loads the json file data inside it. It's not an object that is saved by the game. For your system to work, you'll have to rework the entire item system in the game, or at least the entire weapon system of the game.
Thanks for fast replying!
I looked for the link you posted, thanks i learned something!

I've deepcloned data of $dataWeapon[2] into a clone for modifying it without ID problems!

Now, another problem come to me as expected, i can save the game, but when i load it..... Nothing.
Just the error buzzer ringing. ( Must be $dataWeapons like said above)

So i need to find a way to keep my weapons alive after one save...
I've desperatly tried to store it into $gameSystem, adding a gameSystem.weapon keyword. It work, but cant spawn weapon from it( if a way exist tell me!)
Tried to find a way to include $dataWeapon to save file on google but nope.

I have googled "$data save", unfortunately, all plugins discovered is for Vx or vxace.
Any clues?
Typically the engine only stores game objects and refers solely to the JSON files for data objects, like @MushroomCake28 said.

It may be possible to tweak this by modifying some of the code within the DataManager (found in rpg_managers). If there was a line within the makeSaveContents function that said something along the lines of

Code:
contents.weapons       = $dataWeapons;

And then a line within extractSaveContents that read something like

Code:
 $dataWeapons       = contents.weapons;

This might work. I don't usually do a great deal with the DataManager other than run code when the database is loading so I have my reservations.
I don't know what exactly you're trying to achieve, but perhaps you should look into treating $dataWeapons as an object that is saved and loaded from the save file (the same way $gameSystem and $gameParty are treated). The disadvantage of that approach is that any change to the database you make after saving will not be applied in the game since it will be loading the data from the save file instead of the json file. There are ways to kinda fix this issue, but it's kinda complicated and a mess.
I have tried Silva's trick on rpg_managers.js, unfortunatly... Don't work... When trying to load a saved game, $dataWeapons back to his "vanilla" state using weapons.json. i've searched for hours in rpg_managers but can't find the code part (or i'm just blind) who "force the vanilla mode" of $dataWeapons when loading a saved game. I've tried some other tricks ans tweaks to avoid that but don't work.

By the way, i have created a new json file named "customWeapons.json" for loading saved games with it, to avoid some troubles using the real Weapon.json and destroy the game at startup.

For now, i need to know how i can stop the "auto vanilla mode " of $dataWeapons when i load a saved game

I know, i maybe go into the depht of error, corruption, troubles, manipulating $dataWeapons. But i want to see how far i can go before i give up and ask for a harmless plugin!

Original thread: https://forums.rpgmakerweb.com/threads/modify-weapon-database-in-game.112566/