Jump to content

Increase crafted armor durability (works in DS, not in DST)


Recommended Posts

I have this code right here that increases crafted armor's starting HP by 35%.

local function ab(inst,data)		if data.item.prefab == "armorwood" or data.item.prefab == "armormarble" or data.item.prefab == "armorruins" or data.item.prefab == "ruinshat" or data.item.prefab == "armordragonfly" or data.item.prefab == "footballhat" or data.item.prefab == "armorgrass" or data.item.prefab == "armor_sanity" or data.item.prefab == "beehat" then        data.item.components.armor:SetPercent(1.35)    endend

That executes thanks to this:

inst:ListenForEvent("builditem", ab)

Works perfectly in DS, but I'm having trouble porting it to DST. I checked the log, but it says nothing about the problem.

Thanks in advance.

 

EDIT: Another question: if talk_LP is linked to sounds when examining things, the sounds when making a gesture (angry, sad, joy, ecc.) are linked to what?

Edited by jimmosio
Link to comment
Share on other sites

@jimmosio, look at how the reviver.lua prefab works. I believe there is an OnBuilt function which is called when items are built in DST, which can be manipulated like you want them to be.

There is no real "onbuilt" function there, only a smart way of using "onpickedup", where the drain on life and sanity is done, then the function to look for when picked up changes.

But that code is item-side, and couldn't think of a way to make it player-side.

 

Back to my code, I tried to use "prod" and "item" instead of "data.item", but no results.

Edited by jimmosio
Link to comment
Share on other sites

When you build something:

1) The event builditem gets pushed to the builder, with the data containing item and recipe

2) An onBuild attached to the builder component is sought and ran, with builder and item parameters.

3) Also, an onBuilt attached to the product prefab is sought and ran, with item and builder parameters.

 

Your listener is fine in this case.

 

The problem here is in SetPercent:

SetPercent(1.35) runs SetCondition(max * 1.35)
newcondition equals min of ( max * 1.35, max )

 

So you don't change the max condition.

You change the armor condition to be full, but because you just crafted it, it's already full.

 

Do:

data.item.components.armor.maxcondition = data.item.components.armor.maxcondition * 1.35data.item.components.armor:SetPercent(1)

This should improve the armor's maxcondition, and then up the condition so it shows 100%.

Unless you make a more convoluted change, you can't display 135%.

 

If you try to have armor with condition = 135, and maxcondition = 100 then when you get hit for say, 5 damage, you get:

SetCondition(135 - 5) = SetCondition(130)

newcondition equals min of ( 130, 100 ), which gives 100 and renders your upgrade void.

 

There's another issue here.

The maxcondition doesn't get saved.

So if you save, quit, then load the game, the armor will get the old maxcondition.

Then, when applying the saved condition, the game will pick between the oldmax * 1.35 and the restoredmax, and will choose restoredmax, making your upgrade void.

Edited by DarkXero
Link to comment
Share on other sites

-snip-

Are you telling me that what I want to do is impossible in DST? Or that I need to store some variables somewhere when the world is saved (and closed)?

Edited by jimmosio
Link to comment
Share on other sites

There is no real "onbuilt" function there, only a smart way of using "onpickedup", where the drain on life and sanity is done, then the function to look for when picked up changes.

But that code is item-side, and couldn't think of a way to make it player-side.

 

Back to my code, I tried to use "prod" and "item" instead of "data.item", but no results.

 

Yes my apologies, the reviver was changed.

 

You can look in the builder.lua component file and see this line:

prod:OnBuilt(self.inst)

This line is called the OnBuilt function of the instance you're trying to build. Simply create a function and attach it to the OnBuilt of the instance.

Link to comment
Share on other sites

@jimmosio, no, there are many ways to do it and I was weighing the different options.

 

I think what you should do is copy paste the SetCondition function of the armor component, and put in your modmain:

AddComponentPostInit("armor", function(self)    function self:SetCondition(amount)        self.condition = amount        self.inst:PushEvent("percentusedchange", {percent = self:GetPercent()})           if self.condition <= 0 then            self.condition = 0            GLOBAL.ProfileStatsSet("armor_broke_" .. self.inst.prefab, true)            GLOBAL.ProfileStatsSet("armor", self.inst.prefab)        if GLOBAL.METRICS_ENABLED then            GLOBAL.FightStat_BrokenArmor(self.inst.prefab)        end        if self.onfinished then            self.onfinished()        end        self.inst:Remove()        end    endend)

Then your listener will work.

Edited by DarkXero
Link to comment
Share on other sites

@DarkXero, it takes less code to actually use the on built method.

AddComponentPostInit("armor", function( armor )	armor.inst._OnBuilt = armor.inst.OnBuilt    armor.inst.OnBuilt = function( inst )    	armor.inst._OnBuilt( inst )        inst.components.armor:InitCondition( ( inst.components.armor.condition * 1.35 ), inst.components.armor.absorp_percent )    endend)
Edited by Kzisor
Link to comment
Share on other sites

@Kzisor, that is useful if you want to do it for all characters.

The issue remaining there is that when you save/load, only the condition is saved, not the maxcondition.

So you have to also edit the save/load functions, to avoid having the prefab file's InitCondition override your new max one.

 

So in the end it would look like this:

AddComponentPostInit("armor", function( armor )     armor.inst._OnBuilt = armor.inst.OnBuilt    armor.inst.OnBuilt = function( inst, builder )        armor.inst._OnBuilt( inst, builder )		if builder.prefab == "wilson" then			inst.components.armor:InitCondition( ( inst.components.armor.condition * 1.35 ), inst.components.armor.absorp_percent )		end    end		armor._OnSave = armor.OnSave	armor.OnSave = function( armor )		local ret = armor._OnSave( armor )		ret.maxcondition = armor.maxcondition		return ret	end		armor._OnLoad = armor.OnLoad	armor.OnLoad = function( armor, data )		armor._OnLoad( armor, data )		if data.maxcondition then			armor.maxcondition = data.maxcondition		end	endend)

However, the real reason I copied the SetCondition function and changed the math.min to the amount, is to display percentages over 100%.

 

Imagine you have a Willow and a Wilson master crafter. Wilson crafts an armorwood, Willow does too.

Which armor is the more resilient one?

You can't know unless you get hit or you keep track of which armor is which.

 

Instead of having implicit knowledge, I prefer having two different percentages, that way it's clear which armor is which.

Unless the 135% gets the percentage down to 100%, in which case they are the same armor.

Link to comment
Share on other sites

@DarkXero, eh, the percentage displaying is a differing of opinion.

 

Also since you pointed out about the issue not saving the maxcondition or the absorb_percent I think that is a mod-ability issue more than anything. We should see if PeterA could push an update to that particular code to save/load those two values.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...