How to add pistol ammo by script to the pool no to the clip

davids

New member
hi ,
i have a gun with 2 bullets in its clip the clip it self have max place for 4 bullets , now my player find a new bullet just 1 , so how i can add it to the pool only not to the clip directly , i have this code that i'm using:

string PistolWeaponBasePlayer = "PistolWeaponBasePlayer";
CharacterItemAction[] CharacterItemAction_PistolWeaponBasePlayer;
ListSlice<CharacterItem> characterItems = inventory.GetAllCharacterItems();
for (int y = 0; y < characterItems.Count; ++y)
{
if (characterItems[y].name == PistolWeaponBasePlayer)
{
CharacterItemAction_PistolWeaponBasePlayer = characterItems[y].ItemActions;

CharacterItemAction[] itemActions = characterItems[y].ItemActions;
print("itemActions= " + itemActions.Length);

var shootableAction = itemActions[0] as ShootableAction;
var ammoModuleGroup = shootableAction.AmmoModuleGroup;
print("ammoModuleGroup= " + ammoModuleGroup.ModuleCount);

var shootableAmmoModule1 = ammoModuleGroup.Modules[0] as Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;
var shootableAmmoModule2 = ammoModuleGroup.Modules[1] as Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;

shootableAmmoModule2.AdjustAmmoAmount(1);
}

this one is according to
https://opsive.com/support/documentation/ultimate-character-controller/items-inventory/inventory/

another try is

shootableAction.MainAmmoModule.AdjustAmmoAmount(1);
according to WillEquip() function in ReloaderModule.cs

but the result is same it push the new bullet directly to the clip even without reloading animation
 

Attachments

  • pool.png
    pool.png
    95.1 KB · Views: 2
You should add the amount to the inventory, not the specific item. The item will listen for the adjustments event. Take a look at AddItemIdentifierAmount from the documentation.
 
How?

[SerializeField] protected ItemType m_ItemType;

for (int i = 0; i < inventory.SlotCount; ++i)
{
var item = inventory.GetCharacterItem(m_ItemType, i);
if (item != null)
{
inventory.AddItemIdentifierAmount(item.ItemIdentifier,1);
}
}
doesn't work no mater if m_ItemType is the pistol itself or ammo

string PistolWeaponBasePlayer = "PistolWeaponBasePlayer";
for (int i = 0; i < inventory.SlotCount; ++i)
{
var item = inventory.GetActiveCharacterItem(i);
if (item != null)
{
if (item.name == PistolWeaponBasePlayer)
{
CharacterItemAction_PistolWeaponBasePlayer = item;
inventory.AddItemIdentifierAmount(CharacterItemAction_PistolWeaponBasePlayer.ItemIdentifier, 1);
}
}
}
gives me same results , am i doing something wrong?
 
Last edited:
It looks like your pistol bullet was added successfully. You should be using the ammo ItemType rather than the item ItemType.
 
What?
It looks like your pistol bullet was added successfully
my gun have 2 bullets out of 4 when i start play , now i need to add more bullets to the pool as i said before , by pressing some key , without using pickup object
You should be using the ammo ItemType rather than the item ItemType.
how or where ? PistolBullets(Item Type)in inspector its default pistol bullets

var shootableAmmoModule2 = ammoModuleGroup.Modules[1] Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;
shootableAmmoModule2.AdjustAmmoAmount(1);
will push a bullet to the pistol directly
 
Ah, I was thinking that the 2 were new. You do want to use the PistolBullets with AddItemIdentifier. Maybe you have a cap which is what is preventing them from being added? AddItemIdentifier is the correct method. I recommend setting a breakpoint and comparing against Inventory.LoadDefaultLoadoutInternal where AddItemIdentifierAmount is also used.
 
so i have no changes in progress...

i started a new project and open Shooter part in demo scene and try to add bullets to Atlas Pistol by simple script, but it's make auto reload to the gun , and only after gun clip is full its goes to the poll the same problem

using System.Collections;
using System.Collections.Generic;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items.Actions;
using Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable;
using UnityEngine;
public class AddAmmoTest : MonoBehaviour
{
[SerializeField] protected ItemType m_ItemType;
[SerializeField] protected ItemAmmo m_ItemTypeAmmo;
void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
string PistolBullet = "PistolWeaponRight";
var inventory = GetComponent<InventoryBase>();
for (int i = 0; i < inventory.SlotCount; ++i)
{
var item = inventory.GetActiveCharacterItem(i);
if (item != null)
{
if (item.name == PistolBullet)
{
CharacterItemAction[] itemActions = item.ItemActions;
print("itemActions= " + itemActions.Length);
var shootableAction = itemActions[0] as ShootableAction;
var ammoModuleGroup = shootableAction.AmmoModuleGroup;
print("ammoModuleGroup= " + ammoModuleGroup.ModuleCount);
var shootableAmmoModule1 = ammoModuleGroup.Modules[0] as Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;
var shootableAmmoModule2 = ammoModuleGroup.Modules[1] as Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;
Debug.Log("AmmoRemainingCount: " + shootableAction.AmmoRemainingCount + " ClipRemainingCount=" + shootableAction.ClipRemainingCount);
Debug.Log("Ammo Remaining: " + shootableAmmoModule1.GetAmmoRemainingCount() + " =" + shootableAmmoModule2.GetAmmoRemainingCount());
//shootableAction.MainAmmoModule.AdjustAmmoAmount(1);
shootableAmmoModule2.AdjustAmmoAmount(1);
//shootableAmmoModule1.AdjustAmmoAmount(1);
}
}
}
}
if (Input.GetKeyDown(KeyCode.O))
{
var inventory = GetComponent<InventoryBase>();
inventory.PickupItem(m_ItemType.CreateItemIdentifier(), 0, 1, true, false);
}
if (Input.GetKeyDown(KeyCode.P))
{
var inventory = GetComponent<InventoryBase>();
inventory.AddItemIdentifierAmount(m_ItemTypeAmmo.AmmoItemIdentifier, 1);
inventory.AdjustItemIdentifierAmount(m_ItemTypeAmmo.AmmoItemIdentifier, 1);
}
}
}

when i try to use PickUp system to do the trick with
inventory.PickupItem(m_ItemType.CreateItemIdentifier(), 0, 1, false, false);
its play reload animation

when i try to use
inventory.PickupItem(m_ItemType.CreateItemIdentifier(), 0, 1, true, false);
it it doesn't play but results are the same its make auto reload

is there any way to just add a bullet to the atlas poll without reloading the gun?

its just natural for horror type of game when the player find some ammo he just put him to the bag first like in resident evil game
 

Attachments

  • Screen1.png
    Screen1.png
    540.2 KB · Views: 1
Last edited:
ok i figured it out

you need to change Generic reloader - auto reload mode to Nothing or just cancel pickup option and use this code

if (Input.GetKeyDown(KeyCode.I))
{
string PistolBullet = "PistolWeaponRight";
var inventory = GetComponent<InventoryBase>();
for (int i = 0; i < inventory.SlotCount; ++i)
{
var item = inventory.GetActiveCharacterItem(i);
if (item != null)
{
if (item.name == PistolBullet)
{
CharacterItemAction[] itemActions = item.ItemActions;
var shootableAction = itemActions[0] as ShootableAction;
var ammoModuleGroup = shootableAction.AmmoModuleGroup;
var shootableAmmoModule1 = ammoModuleGroup.Modules[0] as Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;
var shootableAmmoModule2 = ammoModuleGroup.Modules[1] as Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable.ShootableAmmoModule;
int old = shootableAction.MainClipModule.GetItemDefinitionRemainingCount();
shootableAction.MainAmmoModule.AdjustAmmoAmount(1);
shootableAction.MainClipModule.SetClipRemaining(old);
}
}
}
}
or more simple just use Pickup system

if (Input.GetKeyDown(KeyCode.O))
{
var inventory = GetComponent<InventoryBase>();
inventory.PickupItem(m_ItemType.CreateItemIdentifier(), 0, 1, false, false);
}
 

Attachments

  • screen2.png
    screen2.png
    174.4 KB · Views: 6
Last edited:
Top