Jump to content
AlanHK

Excuse me, how can I change the quantity of an ammunition instance?

Recommended Posts

Excuse me, how can I change the quantity of an ammunition instance?

I want to write a script for converting bullets from 9x39 to 762x39 using a wrench

But I hope the quantity corresponds, using a few 9x39 will generate a few 762x39

But after generating a set of 762x39 bullets

Ammo762Object.SetQuantity(Ammo9Object.getQuantity());

Failed to change the quantity, is there any way to modify the number of bullet instances?

Please guide me, teachers.

Share this post


Link to post
Share on other sites
1 hour ago, AlanHK said:

Excuse me, how can I change the quantity of an ammunition instance?

I want to write a script for converting bullets from 9x39 to 762x39 using a wrench

But I hope the quantity corresponds, using a few 9x39 will generate a few 762x39

But after generating a set of 762x39 bullets


Ammo762Object.SetQuantity(Ammo9Object.getQuantity());

Failed to change the quantity, is there any way to modify the number of bullet instances?

Please guide me, teachers.

Hello. Here you have the logical issue.

You're trying to set Quantity of one item to another item and I have a feeling that you didn't check the is NULL Ammo9Object?

So first of all - any kind of 7.62x39/54R has Quantity from 0% - none of it up to 100% - full stack.

But you can't set 101 or 150 or 100500% for that stack because it's fixed limit.

 

Instead of it you shall brake the system of stacking and override the limits definition like so:
Inside of your config.cpp create CfgMagazines class, it shall contains the Ammo_762x39 and Ammo_762x39Tracer extends from Ammunition_Base.
Inside of each class there's a value: "count" aka stacking count. Default is 20, but let set 100 for example and code bellow will show you up to do it.

Spoiler

class CfgMagazines {
	class Ammunition_Base;

	class Ammo_762x39 : Ammunition_Base {
		count = 100;
	};
	class Ammo_762x39Tracer : Ammunition_Base {
		count = 100;
	};
};

 

And that's all about, now the 7.62x39 cartridges has quantity about 100 items in one stack.

References:
1. (Ammo definition) DayZ\Addons\weapons_ammunition\config.cpp
2. (Bullet definition) DayZ\Addons\weapons_projectiles\config.cpp
P.S. Case 2 is for the tweaking (changing) bullet characteristics like speed, caliber and etc.

So have fun!

Also instead of:
 

Ammo762Object.SetQuantity(Ammo9Object.getQuantity());

I suggest you to make it correctly:
 

Ammo762Object.SetQuantity(Ammo762Object.GetQuantityMax());

 

Edited by Sid Debian
Included code exmaples and defenitions.

Share this post


Link to post
Share on other sites

Thank you for your help, but in the actual script, I have already checked the quantity.
This is a process script that has been checked in the Init settings for a minimum quantity of 1 for 9x39. I also confirmed in Debug that I could obtain a quantity of 9x39, but resetting the quantity through SetQuantity failed.

modded class PluginRecipesManagerBase
{
	override void RegisterRecipies()
	{
		super.RegisterRecipies();
		RegisterRecipe(new AmmoNewCraft_762x39);
	}
};

class AmmoNewCraft_762x39 extends RecipeBase
{	
	override void Init()
	{
		m_Name = "Craft 762x39";
		m_IsInstaRecipe = false;
		m_AnimationLength = 2;
		m_Specialty = -0.2;

		m_MinDamageIngredient[0] = -1;
		m_MaxDamageIngredient[0] = -1;
		m_MinQuantityIngredient[0] = 1;
		m_MaxQuantityIngredient[0] = -1;
		
		m_MinDamageIngredient[1] = -1;
		m_MaxDamageIngredient[1] = -1;
		m_MinQuantityIngredient[1] = 1;
		m_MaxQuantityIngredient[1] = -1;

		InsertIngredient(0,"Wrench");m_IngredientAddHealth[0] = -30;
		m_IngredientSetHealth[0] = -1;
		m_IngredientAddQuantity[0] = -1;
		m_IngredientDestroy[0] = false;
		m_IngredientUseSoftSkills[0] = false;
		
		InsertIngredient(1,"Ammo_9x39");
		m_IngredientAddHealth[1] = -1;
		m_IngredientSetHealth[1] = -1;
		m_IngredientAddQuantity[1] = -1;
		m_IngredientDestroy[1] = true;
		m_IngredientUseSoftSkills[1] = false;
		
		AddResult("Ammo_762x39");
		m_ResultSetFullQuantity[0] = false;
		m_ResultSetQuantity[0] = -1;
		m_ResultSetHealth[0] = -1;
		m_ResultInheritsHealth[0] = -2;
		m_ResultInheritsColor[0] = -1;
		m_ResultToInventory[0] = -2;
		m_ResultUseSoftSkills[0] = true;
		m_ResultReplacesIngredient[0] = -1;
    }
    override bool CanDo(ItemBase ingredients[], PlayerBase player) {
        return true;
    }
    override void Do(ItemBase ingredients[], PlayerBase player, array < ItemBase > results, float specialty_weight) {
		float health_ing_1 = ingredients[0].GetHealth("", "");
		float health_ing_2 = ingredients[1].GetHealth("", "");
		float resultHealth = Math.Min(health_ing_1, health_ing_2);
		
		float resultQuantity = ingredients[1].GetQuantity();
		
		ItemBase resultItem = results.Get(0);
		resultItem.SetHealth(resultHealth);
		resultItem.SetQuantity(resultQuantity);
    }
};

 

Edited by AlanHK

Share this post


Link to post
Share on other sites
3 minutes ago, AlanHK said:

Thank you for your help, but in the actual script, I have already checked the quantity.
This is a process script that has been checked in the Init settings for a minimum quantity of 1 for 9x39. I also confirmed in Debug that I could obtain a quantity of 9x39, but resetting the quantity through SetQuantity failed.


modded class PluginRecipesManagerBase
{
	override void RegisterRecipies()
	{
		super.RegisterRecipies();
		RegisterRecipe(new AmmoNewCraft_762x39);
	}
};

class AmmoNewCraft_762x39 extends RecipeBase
{	
	override void Init()
	{
		m_Name = "Craft 762x39";
		m_IsInstaRecipe = false;
		m_AnimationLength = 2;
		m_Specialty = -0.2;

		m_MinDamageIngredient[0] = -1;
		m_MaxDamageIngredient[0] = -1;
		m_MinQuantityIngredient[0] = 1;
		m_MaxQuantityIngredient[0] = -1;
		
		m_MinDamageIngredient[1] = -1;
		m_MaxDamageIngredient[1] = -1;
		m_MinQuantityIngredient[1] = 1;
		m_MaxQuantityIngredient[1] = -1;

		InsertIngredient(0,"Wrench");m_IngredientAddHealth[0] = -30;
		m_IngredientSetHealth[0] = -1;
		m_IngredientAddQuantity[0] = -1;
		m_IngredientDestroy[0] = false;
		m_IngredientUseSoftSkills[0] = false;
		
		InsertIngredient(1,"Ammo_9x39");
		m_IngredientAddHealth[1] = -1;
		m_IngredientSetHealth[1] = -1;
		m_IngredientAddQuantity[1] = -1;
		m_IngredientDestroy[1] = true;
		m_IngredientUseSoftSkills[1] = false;
		
		AddResult("Ammo_762x39");
		m_ResultSetFullQuantity[0] = false;
		m_ResultSetQuantity[0] = -1;
		m_ResultSetHealth[0] = -1;
		m_ResultInheritsHealth[0] = -2;
		m_ResultInheritsColor[0] = -1;
		m_ResultToInventory[0] = -2;
		m_ResultUseSoftSkills[0] = true;
		m_ResultReplacesIngredient[0] = -1;
    }
    override bool CanDo(ItemBase ingredients[], PlayerBase player) {
        return true;
    }
    override void Do(ItemBase ingredients[], PlayerBase player, array < ItemBase > results, float specialty_weight) {
		float health_ing_1 = ingredients[0].GetHealth("", "");
		float health_ing_2 = ingredients[1].GetHealth("", "");
		float resultHealth = Math.Min(health_ing_1, health_ing_2);
		
		float resultQuantity = ingredients[1].GetQuantity();
		
		ItemBase resultItem = results.Get(0);
		resultItem.SetHealth(resultHealth);
		resultItem.SetQuantity(resultQuantity);
    }
};

 

That;s the way how you're crafting it. It's another case you shall consider to sync the data between client (where executed the Crafting) and Server (the receiver of Craftig result). Also you need to create the result stack on server side tho. Or it will not workin.
Also I'd updated my reply end of it suggesting correct usage of setquant.

Share this post


Link to post
Share on other sites

I roughly understand, which means that this synthesis process can only be executed on the client side and cannot modify the ammunition quantity, right?
We still need to develop the server, thank you.

Edited by AlanHK

Share this post


Link to post
Share on other sites
50 minutes ago, AlanHK said:

I roughly understand, which means that this synthesis process can only be executed on the client side and cannot modify the ammunition quantity, right?
We still need to develop the server, thank you.

Not exactly.

You need to change just a bit the code.

Spoiler

class JoinStacks_76239 extends RecipeBase {
	static float BaseItemQuantitiy_L = 0;	// Item stack 1

	override void Init() {
		m_Name = "Craft 9x39";
		m_IsInstaRecipe = false;
		m_AnimationLength = 1;
		m_Specialty = 0;
		m_MinDamageIngredient[0] = -1;
		m_MaxDamageIngredient[0] = -1;
		m_MinQuantityIngredient[0] = 1;
		m_MaxQuantityIngredient[0] = -1;
		m_MinDamageIngredient[1] = -1;
		m_MaxDamageIngredient[1] = -1;
		m_MinQuantityIngredient[1] = 1;
		m_MaxQuantityIngredient[1] = -1;
 
		InsertIngredient(0,"Ammo_9x39");
		m_IngredientAddHealth[0] = -1;
		m_IngredientSetHealth[0] = -1;
		m_IngredientAddQuantity[0] = -1;
		m_IngredientDestroy[0] = 1;
		m_IngredientUseSoftSkills[0] = false;
		
		InsertIngredient(1,"Wrench");
		m_IngredientAddHealth[1] = -30;
		m_IngredientSetHealth[1] = -1;
		m_IngredientAddQuantity[1] = -1;
		m_IngredientDestroy[1] = 1;
		m_IngredientUseSoftSkills[1] = true;
		//result
		AddResult("Ammo_762x39");	
		m_ResultSetFullQuantity[0] = -1;
		m_ResultSetQuantity[0] = 0;
		m_ResultSetHealth[0] = -1;
		m_ResultInheritsHealth[0] = -1;
		m_ResultInheritsColor[0] = -1;
		m_ResultToInventory[0] = -2;
		m_ResultUseSoftSkills[0] = true;
		m_ResultReplacesIngredient[0] = 1;
	}
	override bool CanDo(ItemBase ingredients[], PlayerBase player) { missmatched!
		ItemBase ingredient1 = ingredients[0];
		ItemBase ingredient2 = ingredients[1];
		if ((ingredient1.GetType() == "Ammo_9x39") && (ingredient2.GetType() == "Wrench")) {
			if (ingredient1.GetQuantity() > 0) {
				ItemBase left = ItemBase.Cast(ingredient1);
				if (left) {
					this.BaseItemQuantitiy_L = left.GetAmmoCount();	// Get Quantity of 1st stack
				}
			}
			return true;
		} else {
			return false;
		}
	}
	override void Do(ItemBase ingredients[], PlayerBase player,array<ItemBase> results, float specialty_weight) {
		//float BaseItemQuantitiy_Result = BaseItemQuantitiy_L + BaseItemQuantitiy_R; // will work on joining of 2 stacks but we need to fill up current stack
		for (int i = 0; i < results.Count(); i++) {
			ItemBase target = results[i];
			if (target.GetType() == "Ammo_762x39") {	// When we've done will be careated new stack!
				MiscGameplayFunctions.TransferItemProperties(this, target);	// transpher left stack count to result
				AddQuantity(-split_quantity_new);	// Remove count from left stack
				target.SetQuantity(target.GetQuantityMax());	// Forse to set MAXIMUM Quant
				UpdateNetSyncVariableFloat("m_VarQuantity", GetQuantityMin(), m_VarQuantityMax); // Request for updating Quantity stack count
				target.OnAttachmentQuantityChanged(item);	// notifi ours result stack that's cound had been changed to MAXIMUM
				this.BaseItemQuantitiy_L = 0;	// Just for fun clearing saved value coz it's not required anymore.
				//this.BaseItemQuantitiy_R = 0;
				//BaseItemQuantitiy_Result = 0;
			}
		}
	}
}

 

Didn't checked the result but might help you just a bit.

Edited by Sid Debian
Added the code

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×