Jump to content

How to get sanity and health % of an item holder, and apply it to an item prefab


Recommended Posts

Hello! I do not know how to do the action in the title, I tried asking in a list of questions post, but I wasn't given any Lua code to work off of, and my request for how to code that task was never answered, so I decided to make this post about it. (And maybe help somebody in the future who has this issue). Also if I cannot get the percentage directly, then just give me the way to get Current sanity/health and max sanity/health of the owner.

Edited by Earthyburt
Link to comment
Share on other sites

If it has the inventoryitem component

Then

inst.components.inventoryitem.owner

Where inst is the item's reference in your code

From there we can then do

inst.components.inventoryitem.owner.components.health.current

inst.components.inventoryitem.owner.components.health:GetPercent()

inst.components.inventoryitem.owner.components.sanity.current

inst.components.inventoryitem.owner.components.sanity:GetPercent()

The same can be applied for obtaining max health, and other data for reference look at the components for

  • InventoryItem
  • Health
  • Sanity
Edited by IronHunter
Typo
  • Like 2
Link to comment
Share on other sites

12 hours ago, IronHunter said:

If it has the inventoryitem component

Then


inst.components.inventoryitem.owner

Where inst is the item's reference in your code

From there we can then do


inst.components.inventoryitem.owner.components.health.current

inst.components.inventoryitem.owner.components.health:GetPercent()

inst.components.inventoryitem.owner.components.sanity.current

inst.components.inventoryitem.owner.components.sanity:GetPercent()

The same can be applied for obtaining max health, and other data for reference look at the components for

  • InventoryItem
  • Health
  • Sanity

 

@IronHunterThank you for your information. There is one more question (hopefully) I have to ask of you: I tried applying this value into my code and the game crashed. Could you give me insight on how to fix this? I have included a screenshot of this and code associated with it.

20210330184306_1.thumb.jpg.55815dd3a1ed89573accc09bba56d8e5.jpg

	playerSanity = inst.components.inventoryitem.owner.components.sanity:GetPercent()
	damageAddition = (30*playerSanity)


    inst:AddComponent("weapon")   	
	inst.components.weapon:SetDamage(damageAddition + 15)  ---damage

 

Link to comment
Share on other sites

This is because their is no owner unless the item is in a inventory. I would add a failsafe.

So if there is no owner set it to a default value.

Also you want to listenfor when this item is picked up and dropped so when the owner value changes.

In this case because this is a weapon, I would simply have it update on equip and listenforevent the owners sanitydelta

  • Like 2
Link to comment
Share on other sites

23 hours ago, IronHunter said:

This is because their is no owner unless the item is in a inventory. I would add a failsafe.

So if there is no owner set it to a default value.

Also you want to listenfor when this item is picked up and dropped so when the owner value changes.

In this case because this is a weapon, I would simply have it update on equip and listenforevent the owners sanitydelta

@IronHunter , sorry for pinging you again, but my anxiety got the better of me; How would I set the default value again? I am still new at modding.

Link to comment
Share on other sites

Hey, sorry I haven't had time to reply.

I would suggest considering its a weapon, you add to your on equip and unequip fns a listenforevent that will update whenever sanity changes, then have it update the weapon that way.

local function updateDamage(owner)
	local sanity = owner.components.sanity and owner.components.sanity:GetPercent() or 0
  	local weapon = owner.components.inventory and owner.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
	if weapon ~= nil then
		weapon.components.weapon:SetDamage(30*sanity + 15) 
	end
end
local function OnEquip(inst, owner)
  --in OnEquip Function
	owner:ListenForEvent("sanitydelta", updateDamage)
end
local function OnUnEquip(inst, owner)
  --in OnUnEquipFunction
	owner:RemoveEventCallback("sanitydelta", updateDamage)
end
--in local function fn
inst.components.weapon:SetDamage(15)

inst.components.equippable:SetOnEquip(OnEquip)
inst.components.equippable:SetOnUnequip(OnUnEquip)

 

Edited by IronHunter
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

10 hours ago, IronHunter said:

Try testing it out with the updateDamage fn outside the fn(sim)
As that will put it in the scope for the listenforevents

 

sanityspear.lua 2.26 kB · 0 downloads

Oh! So I had to put the function ABOVE the function. Alright, thank you so much for helping me!! I greatly appreciate it.

@IronHunter

Edited by Earthyburt
Link to comment
Share on other sites

On 3/30/2021 at 6:02 AM, IronHunter said:

If it has the inventoryitem component

Then


inst.components.inventoryitem.owner

Where inst is the item's reference in your code

From there we can then do


inst.components.inventoryitem.owner.components.health.current

inst.components.inventoryitem.owner.components.health:GetPercent()

inst.components.inventoryitem.owner.components.sanity.current

inst.components.inventoryitem.owner.components.sanity:GetPercent()

The same can be applied for obtaining max health, and other data for reference look at the components for

  • InventoryItem
  • Health
  • Sanity

Hey, sorry for not asking this sooner, but what is the Syntax of getting Max Sanity / Max Health? I do not know how to get these values for it

Link to comment
Share on other sites

Like I said look at the components for health.lua and sanity.lua

for max health.lua you can
 

--the first will take into account black health which cannot be healed
..health:GetMaxWithPenalty()
..health.maxhealth

for sanity.lua

--its very similar
..sanity:GetMaxWithPenalty()
..sanity.max

I seriously suggest you check out the files, as it could save you a lot of time.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

12 hours ago, IronHunter said:

Like I said look at the components for health.lua and sanity.lua

for max health.lua you can
 


--the first will take into account black health which cannot be healed
..health:GetMaxWithPenalty()
..health.maxhealth

for sanity.lua


--its very similar
..sanity:GetMaxWithPenalty()
..sanity.max

I seriously suggest you check out the files, as it could save you a lot of time.

Alright Thanks! It looks like I did get getmaxwithpenalty right (though I prefered just Max, so thank you for helping me with that). It seems like my problem is something else, but it is an unrelated issue.

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...