Jump to content

Recommended Posts

Hello!

I read tutorials about LUA about how to modify weapon stats, f.e. set max uses, damage of item and even player stats, but in real time this is possible only on events OnEquip and OnUnequip etc. But i want to make it like while it IsEquipped, but it didn't work.

So straigt to the code which i want to add:

if ((GetPlayer().components.sanity:GetPercent() * 100) > 90) then
GetPlayer().components.combat.damagemultiplier = 1.25
elseif ((GetPlayer().components.sanity:GetPercent() * 100) > 90) then
GetPlayer().components.combat.damagemultiplier = 1.2
elseif ((GetPlayer().components.sanity:GetPercent() * 100) > 90) then
GetPlayer().components.combat.damagemultiplier = 1.15
else GetPlayer().components.combat.damagemultiplier = 1.1

and this code must work in real time, so if my sanity dropped down to 179 from 180 while the thing is equipped, my walkspeed will drop down too, like in code. Didn't find any examples in game files how to implement it. It's pretty hard for me in LUA. Help!

Firstly, a few things..
You should try to minimize how much you call functions (GetPlayer) or use deep indexed variables (inst.components...)
Your code is checking for the percentage above 90 multiple times. It never gets to any other elseif statement.
There's no need to multiply the value of the percentage, just use decimals.
 
You can use the sanitydelta event which is passed from "..\scripts\components\sanity.lua".
 
-- where inst is the player's instance-- if you aren't sure, uncomment the line below-- inst = GetPlayer()inst:ListenForEvent("sanitydelta", function(inst, data)    local percent = data.newpercent    if (percent > 0.9) then -- More than 90%        inst.components.combat.damagemultiplier = 1.25    elseif (percent > 0.8) then -- More than 80%        inst.components.combat.damagemultiplier = 1.2    else -- Less than 80%        inst.components.combat.damagemultiplier = 1.15    endend)

 

Edited by Blueberrys

 

@Amalleus

Firstly, a few things..

You should try to minimize how much you call functions (GetPlayer) or use deep indexed variables (inst.components...)

Your code is checking for the percentage above 90 multiple times. It never gets to any other elseif statement.

There's no need to multiply the value of the percentage, just use decimals.

You can use the sanitydelta event which is passed from "..\scripts\components\sanity.lua".

-- where inst is the player's instance-- if you aren't sure, uncomment the line below-- inst = GetPlayer()inst:ListenForEvent("sanitydelta", function(inst, data)	local percent = data.newpercent		if (percent > 0.9) then -- More than 90%		inst.components.combat.damagemultiplier = 1.25	elseif (percent > 0.8) then -- More than 80%		inst.components.combat.damagemultiplier = 1.2	else -- Less than 80%		inst.components.combat.damagemultiplier = 1.15	endend)

i know about 90% multiple times. i just weren't able to edit it to change mistake. i wrote it in fast

@[member=Blueberrys]

 

i'll test it tomorrow. And as i said, im pretty new to lua. Where should i post this code? It seems like inst variable was already taken by equippable item. Can't attach file, so here's text...

local assets={     Asset("ANIM", "anim/steel.zip"),    Asset("ANIM", "anim/swap_steel.zip"),     Asset("ATLAS", "images/inventoryimages/steel.xml"),    Asset("IMAGE", "images/inventoryimages/steel.tex"),}local prefabs = {}local function fn(colour)		--local function UpdateBoost(inst)     --if inst.components.equippable:IsEquipped() then --works only once	--	if ((GetPlayer().components.sanity:GetPercent() * 100) > 90) then	--		GetPlayer().components.combat.damagemultiplier = 1.25	--	elseif ((GetPlayer().components.sanity:GetPercent() * 100) > 80) then	--		GetPlayer().components.combat.damagemultiplier = 1.2	--	elseif ((GetPlayer().components.sanity:GetPercent() * 100) > 70) then	--		GetPlayer().components.combat.damagemultiplier = 1.15	--	else GetPlayer().components.combat.damagemultiplier = 1.1	--end  	    local function OnEquip(inst, owner)         --owner.AnimState:OverrideSymbol("swap_object", "swap_steel", "purplestaff")        owner.AnimState:OverrideSymbol("swap_object", "swap_steel", "steel")        owner.AnimState:Show("ARM_carry")         owner.AnimState:Hide("ARM_normal") 		--UpdateBoost(inst)    end    local function OnUnequip(inst, owner)         owner.AnimState:Hide("ARM_carry")         owner.AnimState:Show("ARM_normal") 		--testing.... works once		--owner.components.sanity:SetMax(400)    end		local function onfinished(inst)		inst:Remove()	end    local inst = CreateEntity()    local trans = inst.entity:AddTransform()    local anim = inst.entity:AddAnimState()    MakeInventoryPhysics(inst)        anim:SetBank("steel")    anim:SetBuild("steel")    anim:PlayAnimation("idle")    inst:AddComponent("inventoryitem")	inst.components.inventoryitem.keepondeath = true    inst.components.inventoryitem.imagename = "steel"    inst.components.inventoryitem.atlasname = "images/inventoryimages/steel.xml"        inst:AddComponent("equippable")    inst.components.equippable:SetOnEquip( OnEquip )    inst.components.equippable:SetOnUnequip( OnUnequip )	--inst.components.equippable.walkspeedmult = 1.15	inst.components.inventoryitem.keepondeath = true		inst:AddComponent("inspectable")		inst:AddTag("sword")		inst:AddComponent("weapon")    inst.components.weapon:SetDamage(57)	inst.components.weapon:SetRange(0.4, 0.4)		inst:AddComponent("finiteuses")    inst.components.finiteuses:SetMaxUses(150)    inst.components.finiteuses:SetUses(150)        inst.components.finiteuses:SetOnFinished( onfinished )    return instendreturn  Prefab("common/inventory/steel", fn, assets, prefabs)
Edited by Amalleus

seems like i made it to work through Event 

ListenForEvent("sanitydelta", StatBoost)

where i have 

local function StatBoost(inst)	local percent = inst.components.sanity:GetPercent()		if (percent > 0.9) then -- More than 90%		inst.components.combat.damagemultiplier = 3	elseif (percent > 0.8) then -- More than 80%		inst.components.combat.damagemultiplier = 2	else -- Less than 80%		inst.components.combat.damagemultiplier = 1	endend;

BUT! This event works everytime while i have an item in inventory, equipped - so if i have it. But i need to make this code work only if it's equipped. If i will post next code in function StatBoost(inst):

if (IsEquipped() == true) then

Then i need to paste also instance of this equippable before IsEquipped. But how to get instance of equippable?

Edited by Amalleus

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