Ammo System Help

Background:
My system works like this:


Weapon: Revolver
Replaces Attack with "Shoot" skill
Replaces Guard with "Reload" skill

PLUGIN: This is accomplished via Visustella plugin: Weapon Unleash

There are 3 resource items related to both skills

1.Bullet
2.Loaded Bullet_actor1
3.Loaded Bullet_actor2

The Reload skill uses a common event

1. Check the active actor via conditional branch:

BattleManager._subject.actorId()===1

It then converts Bullets into either Loaded Bullet_actor1 or Loaded Bullet_actor2 based on whichever actor is active.

The Shoot skill uses a common event which checks the active actor via the same conditional branch as the Reload skill.

1. It checks for one of the two loaded bullet types according to the active actor.
2. If there's sufficient loaded bullets it subtracts a loaded bullet and deals damage etc.
3. If there's insufficient loaded bullets it plays a *click* sound.

Goal:

During battle I want the player to know how many bullets are loaded in the inputting actor's equipped firearm. This information needs to be available at a glance - so without needing to open an inventory menu.

What I've Tried:

I had renamed the Shoot skill to: Shoot \V[8]
This displays a variable in the skill name which is a step in the right direction.

The first problem is I have 2 types of loaded bullets for the Shoot skill, so this variable needs to be set to the number of Loaded Bullet_actor1 or Loaded Bullet_actor2 in the inventory.

I've tried creating a common event which checks the actor via conditional branch:

if BattleManager.actor()===1

Control variable: variable 8 = Bullet Loaded_actor1

if BattleManager.actor()===2

Control variable: variable 8 = Bullet Loaded_actor2

The Problem:

I can't seem to find a way to get this common event to run when the actor is selecting commands.
I've tried troop events, but using the Turn condition has it running before the actor is determined to the BattleManager returns an error due to a null value.

I feel like I'm super close to figuring this out, but would appreciate some guidance.
There are a bunch of threads about systems like this, you might want to Google it and read through what other people have done.

I would suggest that it would be much more user-friendly to simply disable skills the actor doesn't have enough ammo to use.

1. Check the active actor via conditional branch:
BattleManager._subject.actorId()===1
This isn't wrong, but you don't need to use a script call for this; you can use the built-in Control Variables -> Game Data -> Last Actor ID to Act

It then converts Bullets into either Loaded Bullet_actor1 or Loaded Bullet_actor2 based on whichever actor is active.
I would suggest the opposite here - instead of making fake items, put a property on the actor. It also saves you from needing any conditional branches in your common event because the actor ID is irrelevant (it also saves you from any common events in the first place).

I'm going to assume since you're using VisuStella Weapon Unleash you also have (or don't mind having) the Battle Core and Skills and States Core.

Using Battle Core notetags, you'd do with reload:
Code:
<JS Post-Apply as User>
user.ammo ??= 0;

// How many cartridges does the actor need? Presuming 6 is fully loaded
let ammo = 6 - user.ammo;

// Do we have enough cartridges in inventory? Replace 42 with item ID
ammo = Math.min(ammo, $gameParty.numItems($dataItems[42]));

// Lose that many cartridges from inventory
$gameParty.loseItem($dataItems[42], ammo);

// And add them to the actor
user.ammo += ammo;
</JS Post-Apply as User>

You only need to make the reload skill have two Effects - add and then remove any dummy state, so that the apply effects are activated.

During battle I want the player to know how many bullets are loaded in the inputting actor's equipped firearm. This information needs to be available at a glance - so without needing to open an inventory menu.
You'd use the VisuStella Skills and States Core for this.

You add the user.ammo property to the Skill Cost Types of the plugin parameters, then just give the skills a notetag with <Ammo Cost: 1> or whatever. The plugin should then automatically make the skill be disabled if the user can't afford it, just like with every other kind of cost. It should also handle paying the cost automatically.
I would suggest the opposite here - instead of making fake items, put a property on the actor.

I'm inclined to agree. How do I put a property on the actor? Is that separate from the other steps you'd included? Sorry I'm not so familiar with this side of RPGMZ.

You only need to make the reload skill have two Effects - add and then remove any dummy state, so that the apply effects are activated.

So like this?
1720465395434.png


You add the user.ammo property to the Skill Cost Types of the plugin parameters, then just give the skills a notetag with <Ammo Cost: 1> or whatever. The plugin should then automatically make the skill be disabled if the user can't afford it, just like with every other kind of cost. It should also handle paying the cost automatically.

I'd like to make sure I've handled this correctly, I added an "Ammo" skill cost in the parameters.
I've included the JS for cost calculation. Whereabouts would I place the user.ammo property?

I've included screenshots below:

1720466517536.png


1720465498661.png
1720465537825.png

Code:
// Declare Variables
const user = this;
const skill = arguments[0];
let cost = 0;

// Calculations
const note = skill.note;
if (note.match(/<AMMO COST:[ ](\d+)>/i)) {
    cost += Number(RegExp.$1);
}
if (note.match(/<JS AMMO COST>\s*([\s\S]*)\s*<\/JS AMMO COST>/i)) {
    const code = String(RegExp.$1);
    eval(code);
}

// Apply Trait Cost Alterations
if (cost > 0) {
    const rateNote = /<AMMO COST:[ ](\d+\.?\d*)([%%])>/i;
    const rates = user.traitObjects().map((obj) => (obj && obj.note.match(rateNote) ? Number(RegExp.$1) / 100 : 1));
    const flatNote = /<AMMO COST:[ ]([\+\-]\d+)>/i;
    const flats = user.traitObjects().map((obj) => (obj && obj.note.match(flatNote) ? Number(RegExp.$1) : 0));
    cost = rates.reduce((r, rate) => r * rate, cost);
    cost = flats.reduce((r, flat) => r + flat, cost);
    cost = Math.max(1, cost);
}

// Set Cost Limits
if (note.match(/<AMMO COST MAX:[ ](\d+)>/i)) {
    cost = Math.min(cost, Number(RegExp.$1));
}
if (note.match(/<AMMO COST MIN:[ ](\d+)>/i)) {
    cost = Math.max(cost, Number(RegExp.$1));
}

// Return cost value
return Math.round(Math.max(0, cost));
I'm inclined to agree. How do I put a property on the actor? Is that separate from the other steps you'd included?
No, it's in the notetag I gave you - user.ammo ??= 0;

If you want guns to start loaded, that's a separate issue - as presented, the player will have to make each actor Reload once before using guns for the first time.

If that's not what you want, you could put an Autorun event at the start of the game with the Script command
Code:
$gameActors.forEach(actor => actor.ammo = x)
where x is however much they should have.

As far as the plugin parameters go...copy Potions as a template, then change these things. (for the JS parameters. I assume you can change the other parameters appropriately for your project)

Cost Calculation:
Code:
// Declare Variables
const user = this;
const skill = arguments[0];
let cost = 0;

// Calculations
const note = skill.note;

if (note.match(/<POTION COST:[ ](\d+)>/i)) {
    cost += Number(RegExp.$1);
}

if (note.match(/<JS POTION COST>\s*([\s\S]*)\s*<\/JS POTION COST>/i)) {
    const code = String(RegExp.$1);
    eval(code);
}

return cost;

Can Pay Cost:
Code:
return user?.ammo >= arguments[1];

Paying Cost:
Code:
user.ammo -= arguments[1];
Can Pay Cost:
Code:
return user?.ammo >= arguments[1];

Is the ? a typo in this case? Just want to be sure.
No, it's in the notetag I gave you - user.ammo ??= 0;

If you want guns to start loaded, that's a separate issue - as presented, the player will have to make each actor Reload once before using guns for the first time.

If that's not what you want, you could put an Autorun event at the start of the game with the Script command
Code:
$gameActors.forEach(actor => actor.ammo = x)
where x is however much they should have.

As far as the plugin parameters go...copy Potions as a template, then change these things. (for the JS parameters. I assume you can change the other parameters appropriately for your project)

Cost Calculation:
Code:
// Declare Variables
const user = this;
const skill = arguments[0];
let cost = 0;

// Calculations
const note = skill.note;

if (note.match(/<POTION COST:[ ](\d+)>/i)) {
    cost += Number(RegExp.$1);
}

if (note.match(/<JS POTION COST>\s*([\s\S]*)\s*<\/JS POTION COST>/i)) {
    const code = String(RegExp.$1);
    eval(code);
}

return cost;

Can Pay Cost:
Code:
return user?.ammo >= arguments[1];

Paying Cost:
Code:
user.ammo -= arguments[1];

I managed to get this working and have been making some adjustments here and there.

I would like to thank you for the great help you've been.

You're truly an asset to this community.

All the best
"During battle I want the player to know how many bullets are loaded in the inputting actor's equipped firearm. This information needs to be available at a glance - so without needing to open an inventory menu."

Displaying an image, wouldn't it work? I mean, a battle hud, with ammo count somewhere on the screen?
One more question,

I'd like for the player to be able to reload their weapon(s) while on the map.

What I've Tried

Attempt #1
I tried using the reload skill from the menu - but that didn't work, which is to say nothing happened - presumably because the JS notetags of the skill are intended to trigger on the specified steps within the BattleManager.

Attempt#2
I then grabbed a VS plug-in called Field Skills
+ It allows me to call a common event when using the skill while on the map.
+ It also provides a user friendly quick access menu
+ It requires I add a note tag to refer to a common event


+ I created a state "field reload"
+ The state has a JS tag with identical contents of the reload skill.
+ I created and assigned a common event to the reload skill

Code:
<JS On Expire State>

user.ammo ??= 0;

let ammo = user.mmp - user.ammo;

ammo = Math.min(ammo, $gameParty.numItems($dataItems[1]));

$gameParty.loseItem($dataItems[1], ammo);

user.ammo += ammo;

</JS On Expire State>

Upon using the skill via the menu or the field skill menu this is the result:

iirc Nan means Not a Number

1720592193767.png



I've also tried copying the code directly from the JS note tag and adding it to a Script Command in the common event.
I suspected it wouldn't work - it didn't -, but figure its was good place to start in trying to solve this problem:

Common Event

Script Command:

Code:
user.ammo ??= 0;

let ammo = user.mmp - user.ammo;

ammo = Math.min(ammo, $gameParty.numItems($dataItems[1]));

$gameParty.loseItem($dataItems[1], ammo);

user.ammo += ammo;

I get an error: user.ammo is undefined. Figures.

Any idea how I might approach this?
I get an error: user.ammo is undefined. Figures.
When you get errors, you need to post screenshots so we can see what the engine is talking about. Press F8, go to the Console tab, and screenshot the full error trace.

It's not needed in this specific case, but for general practice.

I tried using the reload skill from the menu - but that didn't work, which is to say nothing happened - presumably because the JS notetags of the skill are intended to trigger on the specified steps within the BattleManager.
Correct.

Attempt#2
I then grabbed a VS plug-in called Field Skills
+ It allows me to call a common event when using the skill while on the map.
+ It also provides a user friendly quick access menu
+ It requires I add a note tag to refer to a common event
I don't see how this is supposed to work. From the screenshots in the documentation, you select the skill and it immediately closes the window and runs the common event.

How are you selecting which actor is reloading?

+ I created a state "field reload"
+ The state has a JS tag with identical contents of the reload skill.
I also don't see how this is intended to work - you'd have to apply the state to the correct actor (how?) and then walk around until it expired.

Code:
let ammo = user.mmp - user.ammo;
Why are ammo and max MP getting intermixed?

I've also tried copying the code directly from the JS note tag and adding it to a Script Command in the common event.
I suspected it wouldn't work - it didn't -...I get an error: user.ammo is undefined.
Correct. There's no such thing as user just in the game's code. You can only use that in places where a plugin has provided it.

Try this. Make a version of the Reload skill that's only usable from Menu Screen (the original can be only usable from Battle Screen).

Make it apply a state in the Effects so that you can target actors and it will go off - I'll reference state 42 for this.

Then give it a Common Event which does:
Control Variables -> Game Data -> Last Target Actor ID
Script:
Code:
// Replace 69 with the ID of the variable from step 1
let user = $gameActors.actor($gameVariables.value(69));
user.ammo ??= 0;
let ammo = 6 - user.ammo;
ammo = Math.min(ammo, $gameParty.numItems($dataItems[42]));
$gameParty.loseItem($dataItems[42], ammo);
user.ammo += ammo;
user.removeState(42);

Replace the variable and state IDs as appropriate. The rest of the code should be identical to the in-battle reload.
Why are ammo and max MP getting intermixed?
I'm using Max MP to represent sidearm capacity as I don't plan on using MP or MaxMP otherwise.
In the Skill States Core Cost JS, it needs a maximum value when drawing the gauges, and I figure I might not always want it to be 6 as there are other sidearms than revolvers with different max ammo capacities. I'd also like party members to use whatever weapon they want interchangeably.

Each weapon has a MaxMP stat - revolvers 6, german pistols 5-10.
So when you equip a different weapon this value changes;

So if you have a Revolver equipped it gives you 6 Max MP
If you have a C96 Mauser equipped you have 10 max MP
If you equip a Bergman you have 5 max MP

Its not perfect right now mind you - if you change from a fully loaded weapon with higher capacity than the weapon you're switching to the value still carries over until your next reload. So I'm still testing and experimenting in this regard.

I'm interested in what you think of this. Your insight is always appreciated.


I also implemented what you recommended and it works, thanks again. 🙂
If you're doing that, I don't really understand why you have ammo as a separate thing at all.

You don't need a separate ammo property for actors because they already have mp. And the game already has a gauge to show you how much current mp you have compared to your max.

And reloading is just restoring mp. So you don't need almost any of the code provided in this thread at all; the only useful bit is looking at the items in inventory and removing them to restore mp.
If you're doing that, I don't really understand why you have ammo as a separate thing at all.

You don't need a separate ammo property for actors because they already have mp. And the game already has a gauge to show you how much current mp you have compared to your max.

And reloading is just restoring mp. So you don't need almost any of the code provided in this thread at all; the only useful bit is looking at the items in inventory and removing them to restore mp.

I can understand why it appears that way.
I likely would have kept MP, but I neglected to mention I'm using VS Equipcore for weapon switching.

Actors have: Melee, Sidearm and Longarm weapon slots they can switch between in combat.
I have a gauge for sidearms and a gauge for longarms.

Siderarm capacity is using Max MP
Longarm capacity is using Magic Defense (also not being used for anything else)
And your sidearms and longarms have the same magazine capacity?
And your sidearms and longarms have the same magazine capacity?
No, they're all different.

Sidearms:

Revolver: 6
Mauser: 10
Bergmann: 5

Longarms:

Double Barrel Shotgun: 2
Lever-Action Rifle: 12
Bolt-Action Rifle: 1

Original thread: https://forums.rpgmakerweb.com/threads/ammo-system-help.170133/