Jump to content

How to get component value?


Recommended Posts

How do you get the value of component... let's say sanity?

inst.components.sanity:

I am blindfully trying GetValue, Value, Get but none of them seems working. I know you can get percent, but I'd rather go with exact value.

I am trying to build a if statement that gets player character's sanity value and if it's higher than X then rest of the code occurs.

Link to comment
Share on other sites

for current amount,

inst.components.sanity.current

This will give you current sanity.

inst.components.sanity:GetPercent()

Also this will return percentage of your sanity (if you have sanity of 50% => returns 0.5).

inst.components.sanity:GetPercentWithPenalty()

And this one will return percent but calculates with max sanity penalty, like Maxwell summoned shadows or somehow you made changes with max sanity.

Example) if you have 150 max sanity and have penalty of 50(so effectively max is 150-50=100 now) and current sanity is 50, then using GetPercentWithPenalty will return 0.5(50/100), but GetPercent will not regard penalty and will return 0.33(50/150).

 

So you could use one of these codes.

1.
if inst.components.sanity and inst.components.sanity.current >= 100 then
  --codes
end

2.
if inst.components.sanity and inst.components.sanity:GetPercent() >= 0.66 then
  --codes
end

3.
if inst.components.sanity and inst.components.sanity:GetPercentWithPenalty() >= 0.66 then
  --codes
end

 

  • Like 1
Link to comment
Share on other sites

@Combustiblemon Thank you for your reply, I knew i was getting close.

Saddly it doesn't work... I don't know why - I can't even log onto server with mod enabled with this code.

local assets=
{ 
    Asset("ANIM", "anim/greatericestaff.zip"),
    Asset("ANIM", "anim/swap_greatericestaff.zip"), 

    Asset("ATLAS", "images/inventoryimages/greatericestaff.xml"),
    Asset("IMAGE", "images/inventoryimages/greatericestaff.tex"),
}

local function onattack_atiesh(inst, attacker, target)
		if not inst.components.fueled:IsEmpty() and attacker.components.sanity and attacker.components.sanity.current > 5 then
			inst.components.weapon:SetDamage(40)
			attacker.components.sanity:DoDelta(-10)
			
				if target.components.freezable ~= nil then
				target.components.freezable:AddColdness(1)
				target.components.freezable:SpawnShatterFX()
				end
			
			inst.components.fueled:DoDelta(-1)
		else
			inst.components.weapon:SetDamage(12)
			if target.components.freezable ~= nil then
			target.components.freezable:AddColdness(0.25)
			target.components.freezable:SpawnShatterFX()
			end
		end
end

local function fn(colour)

    local function OnEquip(inst, owner) 
        --owner.AnimState:OverrideSymbol("swap_object", "swap_greatericestaff", "purplestaff")
		if not owner:HasTag("rubick") then --Replace TAG with a tag only your character has
		inst:DoTaskInTime(0, function()
			if owner:HasTag("player") then
				owner.components.inventory:DropItem(inst) --Drops the item if they don't have the above TAG
				owner.components.talker:Say("I can't pick that up!")-- Generic line
				return
			end	
		end)
		return
		end
        owner.AnimState:OverrideSymbol("swap_object", "swap_greatericestaff", "greatericestaff")
        owner.AnimState:Show("ARM_carry") 
        owner.AnimState:Hide("ARM_normal") 
    end

    local function OnUnequip(inst, owner) 
        owner.AnimState:Hide("ARM_carry") 
        owner.AnimState:Show("ARM_normal") 
    end

    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
	inst.entity:AddNetwork()
    MakeInventoryPhysics(inst)
    
    anim:SetBank("greatericestaff")
    anim:SetBuild("greatericestaff")
    anim:PlayAnimation("idle")
	
    if not TheWorld.ismastersim then
        return inst
    end
	
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "greatericestaff"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/greatericestaff.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )
	
	inst:AddComponent("fueled")
	inst.components.fueled.maxfuel = 20
	inst.components.fueled.fueltype = FUELTYPE.BLUEGEMS
	inst.components.fueled.accepting = true				-- so it takes fuel
    inst.components.fueled:InitializeFuelLevel(20)
	
	inst:AddComponent("weapon")
	inst.components.weapon:SetOnAttack(onattack_atiesh)
    inst.components.weapon:SetRange(8, 10)
	inst.components.weapon:SetProjectile("ice_projectile")
	
	inst:AddComponent("tool")
	-- inst.components.tool:SetAction(ACTIONS.CHOP, 1)
	-- inst.components.tool:SetAction(ACTIONS.DIG, 1)
	-- inst.components.tool:SetAction(ACTIONS.MINE, 1)
--	inst.components.tool:SetAction(ACTIONS.HAMMER, 1)

    return inst
end

return  Prefab("common/inventory/greatericestaff", fn, assets, prefabs)

It's a simple custom ice staff.
Do you have any idea what's wrong?

Link to comment
Share on other sites

It works if I cut the code for sanity check, so I think that's not an issue. At least for now.
 

I've found a solution.

attacker.components.sanity

This part above was an issue and caused crashes. Without it it works like a charm!

  • Like 1
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...