MZ Crafting new database entries- thoughts?

I'm wrapping up the finalizations of my crafting system (aptly named: JAFTING), and looked at the code and realized maybe others handled it better, so here I'm asking:

How do my fellow MV/MZ plugin devs handle dynamic creation of brand new weapons/armor?

Spoiler: The meat and potatoes of the details...
In my JAFTING system, the player can create stuff from one of two ways: via "Recipe" or via "Refinement".

- "Recipe" is probably the more common of the two, where you sacrifice X number of Y ingredients, and !!!! you get new things in your inventory that the recipe's output specified (a bit more complicated than that, but most RPG gamers are familiar).

- "Refinement" is also probably familiar, where the player picks a "base" weapon/armor, a "material" weapon/armor, and sacrifices the "material" to pass on the rpg maker traits that the "material" had onto the "base". There is a bit more magic behind the scenes (about 800 lines of it), but the end result is basically your "base"+1 with some new/upgraded/replaced traits based on the "material". In order to achieve this though, all newly "refined" weapons are given a brand new entry in the database of their corresponding type ($dataWeapons for weapons, etc). When it comes to saving, all refined equips ever created are captured into the save file, and upon loading we re-inject the refined equipment into the $dataWeapons/$dataArmor where they can continue being used.

I am a bit nervous about releasing this functionality publicly, as I fear if the player decided "today is the day we refine 70k weapons", well, then all those database objects will be captured into the save file and I am fairly certain there is a limit. I did it this way because I just couldn't come up with a better way...

At a glance, I didn't even see any other plugins that covered this type of functionality (quick google search, though I didn't try hard), but, to those of you who have done something like this, how did you accomplish it? Is there a better way?
Hi there!

If I understand you correctly, your system is kinda like this:

Database Sword.
Player crafts a weapon.
Player receives Database Sword+1

Database Sword+1
is nothing more than the same Database Sword, but plus some differences, right?
Maybe new stats points(atk, def, etc...), traits, etc.?

And, there is a limit of what the player can have when crafting a weapon this way, like +99 for example?
Oh hey!
The face of Hakuen Studio!
Before I even reply, your plugins are pretty awesome if I do say so myself; very diverse collection.

Hi there!

If I understand you correctly, your system is kinda like this:

Database Sword.
Player crafts a weapon.
Player receives Database Sword+1

Database Sword+1
is nothing more than the same Database Sword, but plus some differences, right?
Maybe new stats points(atk, def, etc...), traits, etc.?

And, there is a limit of what the player can have when crafting a weapon this way, like +99 for example?

Now then, you're largely correct on that.
Under the covers, I just clone the base weapon object,
"merge" the traits based on some logic into the clone,
increment the refinement counter (new property),
adjust the .index (new property, but functionally identical to how .id works),
adjust the name to reflect the +1 or whatever,
directly enter that into the $dataWeapons/$dataArmors at the clone's .index,
remove the original and material from the player's inventory,
give them the new clone

The same equip object can be refined repeatedly, except only:
- as many times as the RM dev stated the equip can be refined (configurable)
- as many times as it takes for the equip to reach it's maximum merged trait count (configurable)
- any combination of the above two points

Leveraging that functionality, the RM dev could allow for the player to have weapons that reach +99, or much lower like +10, or even only once. Or as many times as they want before they reach a weapon's maximum trait count.

Unlike you describe though, refining one equip with another does not actually affect it's base parameters, only the designated "transferable traits" can/will impact the power of the weapon as you refine it with various equips that have "transferable traits". ("transferable trait" is defined as all traits listed below the "divider", which is just a specific trait: the Collapse Effect, on an equip)
Oh hey!
The face of Hakuen Studio!
Before I even reply, your plugins are pretty awesome if I do say so myself; very diverse collection.



Now then, you're largely correct on that.
Under the covers, I just clone the base weapon object,
"merge" the traits based on some logic into the clone,
increment the refinement counter (new property),
adjust the .index (new property, but functionally identical to how .id works),
adjust the name to reflect the +1 or whatever,
directly enter that into the $dataWeapons/$dataArmors at the clone's .index,
remove the original and material from the player's inventory,
give them the new clone

The same equip object can be refined repeatedly, except only:
- as many times as the RM dev stated the equip can be refined (configurable)
- as many times as it takes for the equip to reach it's maximum merged trait count (configurable)
- any combination of the above two points

Leveraging that functionality, the RM dev could allow for the player to have weapons that reach +99, or much lower like +10, or even only once. Or as many times as they want before they reach a weapon's maximum trait count.

Unlike you describe though, refining one equip with another does not actually affect it's base parameters, only the designated "transferable traits" can/will impact the power of the weapon as you refine it with various equips that have "transferable traits". ("transferable trait" is defined as all traits listed below the "divider", which is just a specific trait: the Collapse Effect, on an equip)
First of all... I think you should separate the ORIGINAL database like $dataWeapon with the CUSTOM one to avoid accidental 'mess' with the original database data... maybe $dataCustomWeapon or something like that... when generating custom item... you can give it a flag that it's a custom item... so any item with the flag will reference to that $dataCustomWeapons instead $dataWeapons... and then you can save that variable on save file...

but there will be a catch though... if the savefile too big... it will throw warning... thus to avoid that... maybe you can create another 'separate saved' file that a savefile will reference too (customData001 will be used by Save file 001, and so on)... it will be harder though... but if you can create custom item with no limit (or high limit)... this step might be necessary.

hope this help
I second the suggestion of creating a separate $data object to hold crafted items to avoid data contamination, but that's hardly a concrete necessity. $dataWeapons and $dataArmors are just javascript objects like any other, so there's no actual limit to how many elements can be in them. But as estriole said, there comes a point where the save file is too big for the engine to handle it. That said, the actual data object for a given weapon or armour is so small it would take an astronomical number of extra entries to scratch the surface of this limit on anything but the most rudimentary potato PC.

If this is a legitimate concern for you, though, you have two main options to avoid it: limit the number of crafting materials, or limit the number of items the player can carry.

Another thing to remember (if you haven't done this already) is that unless you have a buyback system, you should delete the custom entry when the player sells a crafted item.
@Jragyn Hi!
Thanks for the compliments! 😍
I did make a few things here and there, but nothing like an abs system, right? xD

Ok, so I got it with your explanation.
If the save file is something that worries you, then see if you can try an approach that only saves the new data of the crafted weapons.

This can be hard to set up at first, but once it is done, you will gain some benefits.

So let's say you have a database sword. And the user crafted a database sword+1.
As an example, both of them will have the note property and meta property.
But instead of creating a new note/meta for the crafted weapon, you can just get it from its base weapon.

Like:

JavaScript:
const dataBaseWeapon1 = {
    name: Sword,
    note: "This contains all sort of note tags from your plugins and other plugins"
}
const craftedWeapon = {
    baseWeaponId : 1,
    name: "+1", // This is what will be saved on the save file.
    note: "baseWeapon" // This is what will be saved on the save file.
}
// When the game loads, you build what need to be builded.
const buildCraftedWeapons = () =>{
    if(craftedWeapon.note.includes("baseWeapon")){
        const id = craftedWeapon.baseWeaponId
        craftedWeapon.note = $dataWeapons[id].note,
        name: $dataWeapons[id].name +craftedWeapon.name 
    }
}
console.log(craftedWeapon.note) // "This contains all sort of note tags from your plugins and other plugins"
console.log(craftedWeapon.name) // "Sword + 1"

I don't know if I manage to explain it right...
This can give you some extra work though. But once is done, I believe you can save some space on the save file.
Put only into the save file, the things that changed from the base weapon.

I had the though of making a craft plugin one day, but things like that made me give up xD
So after I wrote this post, I went to test a couple of other things and realized that $data* (such as $dataWeapons or $dataArmor) are generated everytime the game is started and isn't even saved, so modifications that I make there are irrelevant. Instead, I capture all refinements into arrays and store them with the save file separately, and then re-add them into $data* upon reload. That actually worked a lot smoother than I thought it would.

it would take an astronomical number of extra entries to scratch the surface of this limit
I was pretty concerned, but after doing a bit more testing, it seems you're right, even 100 brand new copies of various weapons with various traits resulted in a save file increase of like 1kb. My main concern was file size limitation because I thought I saw something about an error when you exceed 50000 bytes (~50kb) but it seems thats just a warning (thank goodness).

you can try an approach that only saves the new data of the crafted weapons.
This was ultimately what I ended up doing, but was still a concern because I guess I assumed everyone would refine millions of times! But as @Trihan said, it seems I overestimated the size in data increase for that- embarrassing.


I think you should separate the ORIGINAL database like $dataWeapon with the CUSTOM one to avoid accidental 'mess' with the original database data
Yeah, I was a bit nervous about this, but after discovering that datastores from the database, like $dataWeapons, aren't even saved to the save file, and are autogenerated with each load of the game, I had to rethink the approach and ended up splitting off the refined data and saving that instead. Good call out!

you can try an approach that only saves the new data of the crafted weapons.
This ultimately was what I ended up doing! Very similar to how you described it, actually XD


I appreciate all you folks chiming in with your thoughts and guidance.
If you're curious, you're free to peek at the code before I release it.
I still wanted to add in some plugin parameters for some stuff, but otherwise it is operational and appears to work as I intended it (finally- took like a week).
Hahaha I'm sorry I just can't stop laughing at JAFTING.

Interesting code. I'm intrigued by your practice of creating an object to hold your aliased methods so that you don't need an IFFE to avoid namespace contamination.
Interesting code. I'm intrigued by your practice of creating an object to hold your aliased methods so that you don't need an IFFE to avoid namespace contamination.
You can't control how other people write stuff, but you can control how you write it!
This has been an effective means of self-management: if it belongs to me, then I own it within my own module's namespace.

Hahaha I'm sorry I just can't stop laughing at JAFTING.
:2k32:

One of the folks who did some QA for it described it:
1621524000612.png
You can't control how other people write stuff, but you can control how you write it!
This has been an effective means of self-management: if it belongs to me, then I own it within my own module's namespace.


:2k32:

One of the folks who did some QA for it described it:
View attachment 189792
How familiar are you with IFFEs? Because they basically do the same thing but you just put everything in a self-contained and self-calling anonymous function where you don't need to store anything in a proprietary object. Essentially has the same end result: no namespace pollution because you can use any identifiers you want and they won't affect anything outside it.

The one downside I'm aware of is that you can't call code you create there from the console when testing the game.
I feel this thread is better suited to Learning Javascript (which includes learning the RPG Maker implementation of Javascript), so

Moderator note: Moving
How familiar are you with IFFEs?
I seldom use them these days. They aren't used in my actual day-to-day SDE job, and I don't use them to write plugins (anymore). Having stuff like this attached to an object I created allows me to also use them in other plugins should the need arise (see: J-Base), and hopefully offers an easy-to-understand pattern that others can make sense of should they want to peruse my code to understand how my code does its thing.

This isn't to say that I couldn't adjust my plugins to that pattern, but I just don't see the need and have already heavily committed to this pattern 😅 .

Surprisingly, it wasn't until like, the last week that I actually started using the console to check objects and stuff (when doing things with $dataWeapons or the $gameJAFTING object I spun up for this). Nifty.

Original thread: https://forums.rpgmakerweb.com/threads/crafting-new-database-entries-thoughts.136683/