That's the same question, so the answer is still the same: yes.
MZ Non-manual treasure system?
JS Plugin RequestsThank you. Would you share an example how?
Something like this:
const id = Math.randomInt(9) + 7;
Or is there a better solution?
const is to define a constant - a fixed number that never cchanges. So that will never work because you want a variable that can randomly change.
I suggest you don't try to guess code when you have no experience in programming.
we can help you get correct code when you describe what you want - but the problem why that was not done so far is because you didn't give enough info to even start coding.
as I already said there are half a dozen different ways to handle a treasure system, and they all require different code. And before anyone can start giving you that code, you need to decide on a specific mechanic on what to do.
So if you describe a complete game mechanic (instead of the small pieces you wrote so far), then we can tell you how to do this. Either with event commands (tedious due to long number of conditional branches, but possible) or with javascript (requiring at least minimum understanding of how code works) or through a plugin request (where someone else handles the javascript and gives you plugin commands or notetags to transfer the data).
Why thank you for your explanation.
So, my idea is to have a treasure system which is, well, as essential as possible.
There is a good example in the VZ sample game (the "infinity chest") as that, with a minimal rework, can be adapted - of course, it is prone to fail whenever the database is updated, but it's a good start.
even with my 0 knowledge of scripting, I can understand (now, thanks to your explanation) what this does in detail, and I have managed to make it somewhat work.
It is, however, a very basic and non-weighted system. So I'm wondering if it possible to build on it to have multiple items being given, weighted.
It would also be nice to have a dynamic message saying what people have, built on this:
So, my idea is to have a treasure system which is, well, as essential as possible.
There is a good example in the VZ sample game (the "infinity chest") as that, with a minimal rework, can be adapted - of course, it is prone to fail whenever the database is updated, but it's a good start.
JavaScript:
const id = Math.randomInt(9) + 7;
const amount = Math.randomInt(10) + 1;
$gameParty.gainItem($dataItems[id], amount);
even with my 0 knowledge of scripting, I can understand (now, thanks to your explanation) what this does in detail, and I have managed to make it somewhat work.
It is, however, a very basic and non-weighted system. So I'm wondering if it possible to build on it to have multiple items being given, weighted.
It would also be nice to have a dynamic message saying what people have, built on this:
JavaScript:
<center>
Found x\LastGainObjQuantity \LastGainObj!
</center>
Given your knowledge, I think the easiest weighted item gain is to use multiple ifs. But you need to do a bit of math for that. I haven't opened MZ or code editor to confirm whether it works, but the sample code is gonna be like this
JavaScript:
function randomGain(){
const weightRoll = Math.randomInt(100) + 1
if (weightRoll < 50) {
$gameParty.gainItem($dataItems[1], 1);
}else if (weightRoll < 75){
$gameParty.gainItem($dataItems[2], 1);
}else if (weightRoll < 90){
$gameParty.gainItem($dataItems[3], 1);
}else {
$gameParty.gainItem($dataItems[4], 1);
}
}
- item 1 has a 50% chance to drop
- Item 2 has a 25% chance to drop
- Item 3 has a 15% chance to drop
- Item 4 has a 10% chance to drop
To use it is very simple. Drop this as a plugin. Then write a script call
randomGain().It is very hardcoded and you can even translate that to a common event if you want to.
Of course, there's a better way of implementing that, by using roulette wheel selection (or this from StackOverflow) if it becomes a plugin. I think the reason why no one hasn't made the plugin yet is simply because of the lack of requirements. Because setting up a loot table can be anything.
all that is possible.
The main problem with the VS example is that it is too simple. It makes a number of assumptions that require a lot of more complex coding to overcome.
one example of that is that the line with the random(9)+7 means that it requires items with IDs 7 to 16 and it will not allow you to find other items (ID less or higher) and no skipping of slots either.
and not even talking about different weights.
all that can only be overcome by making your own data list, either fixed (like theoallen's example above) or dynamic (requires a more complex code reading notetags from items).
having a fixed code like the example above (could be done both by event commands or by javascript) will require you to understand the code enough to make intelligent changes whenever your database is changed.
a dynamic code with notetags is much more comfortable to use, but will require some game mechanic design before it can be programmed.
I don't have time for any of that at the moment, but if no one else steps in I might have time to design such a mechanic in two weeks or so.
The main problem with the VS example is that it is too simple. It makes a number of assumptions that require a lot of more complex coding to overcome.
one example of that is that the line with the random(9)+7 means that it requires items with IDs 7 to 16 and it will not allow you to find other items (ID less or higher) and no skipping of slots either.
and not even talking about different weights.
all that can only be overcome by making your own data list, either fixed (like theoallen's example above) or dynamic (requires a more complex code reading notetags from items).
having a fixed code like the example above (could be done both by event commands or by javascript) will require you to understand the code enough to make intelligent changes whenever your database is changed.
a dynamic code with notetags is much more comfortable to use, but will require some game mechanic design before it can be programmed.
I don't have time for any of that at the moment, but if no one else steps in I might have time to design such a mechanic in two weeks or so.
I feel like this got over-complicated. If you have a range of item ids that you want to use, and you're not into using scripts, then all you need to do is use the engine's variable system, it has a random function with a range. If your items aren't all neatly organized then you can have an additional layer, e.g. get a random number 1-4. If you get 1 then you change the variable to item id 123, if you get 2, you change the variable to item id 155, 3, 165 etc.
If you want to weight items by rarity, then you simply need to have multiple random result point to more common items (roll 1-10, 1-5 go to a common item loot table, 6-7 to uncommon, 8 to rare, etc.) .
You can simply have a common event that has all the different item tables, and have a variable in each chest that determines which table to look at.
Anything you do with scripts would be some variation of that anyway (admittedly there are shortcuts you can take there).
If you want to weight items by rarity, then you simply need to have multiple random result point to more common items (roll 1-10, 1-5 go to a common item loot table, 6-7 to uncommon, 8 to rare, etc.) .
You can simply have a common event that has all the different item tables, and have a variable in each chest that determines which table to look at.
Anything you do with scripts would be some variation of that anyway (admittedly there are shortcuts you can take there).
So, I'd like to thank you all for the suggestions.
Would this actually need to go in a plugin, or would I technically be able to use it in events like in the VZ example?
If you don't want this as a plugin, then you can simply write this in the script call
Code:
const weightRoll = Math.randomInt(100) + 1
if (weightRoll < 50) {
$gameParty.gainItem($dataItems[1], 1);
}else if (weightRoll < 75){
$gameParty.gainItem($dataItems[2], 1);
}else if (weightRoll < 90){
$gameParty.gainItem($dataItems[3], 1);
}else {
$gameParty.gainItem($dataItems[4], 1);
}
function randomGain() { } part basically enables you to call that function anywhere.I definitely need to read a bit about Javascript.
This is wrong. Assigning a random value to a
const variable is perfectly fine, because const only means you can't assign a new value once the variable is declared. When the variable is in a function, it can still take a different value each time the function is called.Original thread: https://forums.rpgmakerweb.com/threads/non-manual-treasure-system.143613/