Weapons in inventory set to 99?

Hello,

I'm testing my game's shopping mechanics with buying/selling weapons. I have a parallel common event running with a switch that is turned on with an autorun event as soon as the game starts, so this part works. This common event sets up some basic info, like setting variables and giving the player weapons.

When I set it up to give the player one (or 10) of each weapon, via the "change weapon" command, the inventory for those weapons is changed to 99 pieces, while my "change weapon" command is instructing it to add only 1. When I delete the command in this common event, the weapon is not in the inventory at all. When I add an extra command to this common event to first substract 99 pieces of this weapon and then add 1, it works correctly and there will only be 1 of this weapon in the player's inventory.

How is this possible? I can go ahead and set all to 0 with a script maybe (does anyone know which script call to make for this? Would ($gameParty.gainItem(weapon, -99)) work or is there a script call for setting instead of adding/substracting?), but this doesn't seem right to me. Nowhere in the events am I adding 99 of each weapon, which I think is also proven by the fact that if I remove the "change weapon" command, the weapon will not be in my inventory at all.
Best to send a screenshot of the actual event. We can't help you without seeing what you are doing.
Best to send a screenshot of the actual event. We can't help you without seeing what you are doing.
Didn't think it was necessary, since it's really just "change weapon", but here you go:

Common event:


Result:


Changing common event to substract 99 cucumber seeds and then adding 1:


Result:


After removing cabbage and strawberry seeds, they disappear from inventory:
It looks like you did not turn off the common event after it run one time.
Resulting in it running up to 60 Times per second.
With +1 Weapon you would add up to 60 Weapons in 1 second.

Insert, at the end of the Eventcode, Control Switch: "101 Always On" Turn Off.
It looks like you did not turn off the common event after it run one time.
Resulting in it running up to 60 Times per second.
With +1 Weapon you would add up to 60 Weapons in 1 second.

Insert, at the end of the Eventcode, Control Switch: "101 Always On" Turn Off.
Thank you! I totally forgot that it will loop then of course. Next time I'll post pictures right away!
you could also use a scriptcall to add "all weapons" + 1.
like:
JavaScript:
$dataWeapons.forEach(function(weapon) {$gameParty.gainItem(weapon,1,false);});
and you get 1 weapon of each. (same you can do for Items, Armor
you could also use a scriptcall to add "all weapons" + 1.
like:
JavaScript:
$dataWeapons.forEach(function(weapon) {$gameParty.gainItem(weapon,1,false);});
and you get 1 weapon of each. (same you can do for Items, Armor
This is nice, thank you. I tried looking this up yesterday to no avail, which is why I went with the separate commands. Will definitely use your script call, will make testing much easier!
It looks like you did not turn off the common event after it run one time.
Resulting in it running up to 60 Times per second.
With +1 Weapon you would add up to 60 Weapons in 1 second.

Insert, at the end of the Eventcode, Control Switch: "101 Always On" Turn Off.
And that is why I was asking for the screenshots 😀
you could also use a scriptcall to add "all weapons" + 1.
like:
JavaScript:
$dataWeapons.forEach(function(weapon) {$gameParty.gainItem(weapon,1,false);});
and you get 1 weapon of each. (same you can do for Items, Armor
Is this also possible for weapon type? For certain types I only need 1, but for the seeds it's useful to have more. I can just give more of everything, but it would still be useful knowledge for me to know.
Thanks in advance 🙂
Is this also possible for weapon type? For certain types I only need 1, but for the seeds it's useful to have more. I can just give more of everything, but it would still be useful knowledge for me to know.
Thanks in advance 🙂
Supposing that the Weapon Type ID (you can find out in the Types tab of your database) of "Seed" weapons is 6, you could do it like this:

JavaScript:
const seedTypeId = 6;
$dataWeapons.forEach(weapon => {
    if (!weapon || weapon.name.length < 1) {
        return;
    }

    if (weapon.wtypeId === seedTypeId) {
        $gameParty.gainItem(weapon, 10, false);
    } else {
        $gameParty.gainItem(weapon, 1, false);
    }
});

I also added in a little extra check to prevent it from trying to add null/nameless weapons.

Just change the first line to represent the actual ID of your Seed type.
you could also use a scriptcall to add "all weapons" + 1.
like:
JavaScript:
$dataWeapons.forEach(function(weapon) {$gameParty.gainItem(weapon,1,false);});
and you get 1 weapon of each. (same you can do for Items, Armor
Could you break this down, explain what each part of the code does? That way when someone (like myself) wants to use a variant on the code, we can do it ourselves. I am specially interested in the "for each" part of the code.

I do get that I can get the same result for armors or items, just by replacing "weapon" with "armor" or "item" in the code. 😛
I am specially interested in the "for each" part of the code.
It's one of JavaScript's built-in methods.

The syntax that ShadowDragon used will work, but is a little bit older. The more modern/efficient way would be to use an inline arrow function, which has examples in the documentation above.

If you are also asking about the gainItem method, that's in rpg_objects.js:
gainItem(item: BaseItem, amount: number, includeEquip: boolean)
It's one of JavaScript's built-in methods.

The syntax that ShadowDragon used will work, but is a little bit older. The more modern/efficient way would be to use an inline arrow function, which has examples in the documentation above.

If you are also asking about the gainItem method, that's in rpg_objects.js:
gainItem(item: BaseItem, amount: number, includeEquip: boolean)
I know the gainItem part (from the well known google sheet with script calls), just was not so sure how to use the forEach part with the gainItem part. 😛

It has been a loooong time since I went to w3schools. 😛 I kinda forgot about that. Maybe because most of it is above my level of javascript. 😛 (Everything I know about javascript, I learned because I needed it to make my game systems, like my chess engine and my card game.)

I think I understand this now though. Thanks for the link. I will experiment a little more before I fully understand which part does what. 😛
I know the gainItem part (from the well known google sheet with script calls), just was not so sure how to use the forEach part with the gainItem part. 😛

It has been a loooong time since I went to w3schools. 😛 I kinda forgot about that. Maybe because most of it is above my level of javascript. 😛 (Everything I know about javascript, I learned because I needed it to make my game systems, like my chess engine and my card game.)

I think I understand this now though. Thanks for the link. I will experiment a little more before I fully understand which part does what. 😛
forEach is basically just a fancy way to loop through each element of an array. You define a function, and it will call that function once for each element of the array, passing that element to the function. In ShadowDragon's snippet, he defined the function inside of the forEach function call, rather than defining it separately.

Here is ShadowDragon's original script call (with slightly adjusted formatting for easier reading):
JavaScript:
$dataWeapons.forEach(function(weapon) {
    $gameParty.gainItem(weapon, 1, false);
});

And here is basically what JavaScript is doing under the hood:
JavaScript:
function iterationFunction(weapon) {
    $gameParty.gainItem(weapon, 1, false);
}

for (let i = 0; i < $dataWeapons.length; i++) {
    iterationFunction($dataWeapons[i]);
}
And here is basically what JavaScript is doing under the hood:
JavaScript:
function iterationFunction(weapon) {
    $gameParty.gainItem(weapon, 1, false);
}

for (let i = 0; i < $dataWeapons.length; i++) {
    iterationFunction($dataWeapons[i]);
}
Okay, that part is more complicated than the original line. 😛 I think I understand ShadowDragons script now. I might have to learn a bit more before I can use it in other situations, though.

Thanks for the explanation. I didn't mean to highjack Space_Lions topic, though. I hope the extra info is helpful for them as well.
@JohnDoeNews it's a bit older snippet which I used before I know arrows
part in the "GainAllItems" plugin to use a plugin command, best used
in a common event and autorun event to turn the switch OFF.

the way Athran elplained what happend under the hood as well for
the "forEach" function (and by ATT_Turan).

but as long the snippet works, why not, as I still try to understand
the (element => {}) part.

(element => {}) = the same as (function(element) { // code here}

advanced coders use it more often as its easier and effience.

do I might redo my MainMenu plugin and my ULS, maybe my TRI plugin
to use that methode as well.

as long it works, I wont change anything anytime soon.

Original thread: https://forums.rpgmakerweb.com/threads/weapons-in-inventory-set-to-99.151478/