Jump to content

Recommended Posts

I have been working on this function for a while, and I only got it to work once, when I first put in the print and return functions. I have no idea what is wrong, or what kind of syntax error I am making. If anyone knows what's wrong please help.

rating = .13

local function updateArmor(owner)
	local sanity = owner.components.sanity and owner.components.sanity.current or 0
  	local armor = owner.components.inventory and owner.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)
	local modifiedMax = owner.components.sanity.max * .75
	print(modifiedMax)
	if armor ~= nil then
    		if sanity > modifiedMax then
			rating = .9
			print(rating)
			return rating
		elseif sanity <= modifiedMax then
		    rating = sanity/modifiedMax
			print(rating)
			return rating
		end
	end
end
local function onequip(inst, owner) 

    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("equipskinneditem", inst:GetSkinName())
        owner.AnimState:OverrideItemSkinSymbol("swap_body", skin_build, "swap_body", inst.GUID, "sanityarmor")
    else
		owner.AnimState:OverrideSymbol("swap_body", "sanityarmor", "swap_body")
    end    
		owner:ListenForEvent("sanitydelta", updateArmor)
end

local function onunequip(inst, owner) 
    owner.AnimState:ClearOverrideSymbol("swap_body")   
	
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("unequipskinneditem", inst:GetSkinName())
    end	
	owner:RemoveEventCallback("sanitydelta", updateArmor)
end
                           
    inst:AddComponent("armor")
    inst.components.armor:InitCondition(TUNING.ARMORMARBLE * 4, rating)
Edited by Earthyburt
Wrong wording

@-t- It doesn't crash the game. Basically, the function works until the updateArmor function ends, when my new variable for rating gets updated to .9 or when it uses the sanity/modifiedMax equation. The rating returns to being .13 again. (the .13 is for testing purposes, so I can tell better if the function is working or not). Sorry if I did not make that clear enough

 

Right now I have two therioes of why this isn't working: A. The Initcondotions component is only being run once and I may have forgotten to save when I was testing or B. The rating variable outside of the updateArmor function and the rating inside the updateArmor function are actually different variables. I can check for B by creating a new function that purely prints the rating. (I cannot do that right now because I am at school)

Edited by Earthyburt

Based on this function I'm assuming you're creating an armor that changes it's absorption percent when the wearer has low/high sanity.

For that you can use the SetAbsorption function present in the 'armor' component. With this function the 'updateArmor' would have to look different:

local function updateArmor(owner)
	local sanity_percent = owner.components.sanity and owner.components.sanity:GetPercent() or 0 -- This gets the current sanity percentage
	local absorptionRange = sanity_percent/(0.75/0.77) + 0.13 -- This is the absorption range (from 90% to 13%)
	local absorption = absorptionRange -- I added this just to change the name of the variable but you can delete it and use 'absorptionRange' instead
	
	if absorption > 0.9 then -- Don't allow a value higher than 90%
		absorption = 0.9
	end
	
	inst.components.armor:SetAbsorption(absorption) -- Set the new absorption
end

 

This function smaller than yours but the math required for the range of the absorption is pretty specific if you want to change the range here is how you can do it:

current_sanity_percent / (start_decreasing_absorption_percent / (maximum_absorption - minimum_absorption)) + minimum_absorption

start_decreasing_absorption_percent is at what point the armor has to start losing absorption, so 0.75 would mean that at 75% sanity the armor will start decreasing it's absorption.

 

Example: current_sanity / (0.75 / (0.8 - 0.2 ) ) + 0.2

The armor will start losing absorption at 75% sanity, starting from 80% absorption down to 20% absorption.

  • Like 1
  • Thanks 1
3 hours ago, -t- said:

Based on this function I'm assuming you're creating an armor that changes it's absorption percent when the wearer has low/high sanity.

For that you can use the SetAbsorption function present in the 'armor' component. With this function the 'updateArmor' would have to look different:


local function updateArmor(owner)
	local sanity_percent = owner.components.sanity and owner.components.sanity:GetPercent() or 0 -- This gets the current sanity percentage
	local absorptionRange = sanity_percent/(0.75/0.77) + 0.13 -- This is the absorption range (from 90% to 13%)
	local absorption = absorptionRange -- I added this just to change the name of the variable but you can delete it and use 'absorptionRange' instead
	
	if absorption > 0.9 then -- Don't allow a value higher than 90%
		absorption = 0.9
	end
	
	inst.components.armor:SetAbsorption(absorption) -- Set the new absorption
end

 

This function smaller than yours but the math required for the range of the absorption is pretty specific if you want to change the range here is how you can do it:

current_sanity_percent / (start_decreasing_absorption_percent / (maximum_absorption - minimum_absorption)) + minimum_absorption

start_decreasing_absorption_percent is at what point the armor has to start losing absorption, so 0.75 would mean that at 75% sanity the armor will start decreasing it's absorption.

 

Example: current_sanity / (0.75 / (0.8 - 0.2 ) ) + 0.2

The armor will start losing absorption at 75% sanity, starting from 80% absorption down to 20% absorption.

Wow! I didn't know that there was an absorption component. Didn't think to check. However, The code provided did crash the game due to the game not recognizing the inst in the absorption component. Oh and also the .13  wasn't a minumum absorption level, it was put there to keep the game from crashing. I am going to keep the original function though, but I will consider a minimum absorption value and if I do decide on that, I'll use the code you provided.

 

Update: Basically, all I needed to do was replace 'inst' with armor. Thank you so much for your help! I greatly appreciate it!

Edited by Earthyburt
  • GL Happy 1

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
×
  • Create New...