Jump to content

[SOLVED] How to make modded weapon damage user when used?


Recommended Posts

I am not sure what to do about making my custom weapon damage the player on use, I don't know what code to add or where to add it. Sorry I am a newbie scripter.

Here is the script for my item (Its a infinity gaunlet)

local assets=
{ 
    Asset("ANIM", "anim/gauntlet.zip"),
    Asset("ANIM", "anim/swap_gauntlet.zip"), 

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

local prefabs = 
{
}

local function onthrown(inst, data)
    inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
end


local function fn(colour)

    local function OnEquip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_object", "swap_gauntlet", "swap_gauntlet")
        owner.AnimState:SetMultiSymbolExchange("swap_object", "hand")
        owner.AnimState:Show("ARM_carry") 
        owner.AnimState:Hide("ARM_normal") 
    end

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

    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)
    
    anim:SetBank("gauntlet")
    anim:SetBuild("gauntlet")
    anim:PlayAnimation("idle", true)
    
    inst.entity:AddNetwork()
    if not TheWorld.ismastersim then
        return inst
    end
    inst.entity:SetPristine()

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(10)
    inst.components.weapon:SetRange(4)
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "gauntlet"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/gauntlet.xml"
    
    inst:AddComponent("inspectable")
    
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(25555555)
    inst.components.finiteuses:SetUses(25555555)
    inst.components.finiteuses:SetOnFinished(inst.Remove)
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )

    return inst
end

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

If anyone could help that would be great.

Link to comment
Share on other sites

Look at the weapon component file.
There you can find a "SetOnAttack(fn)" functin, which lets you define a custom function that is called everytime there is an attack.
So within your fn you add after addding the weapon component:

inst.components.weapon:SetOnAttack(OnAttack)

and above your fn you define this OnAttack function:

local function OnAttack(weapon, attacker, target)
    if attacker~=nil and attacker.components~=nil and attacker.components.health~=nil and not attacker.components.health:IsDead() then
        attacker.components.health:DoDelta(amount, overtime, cause, ignore_invincible, afflicter, ignore_absorb)
    end
end

within DoDelta normally it is enough to only enter a value for amount and remove the rest of it. But maybe you want some of these too.

Edited by Serpens
Link to comment
Share on other sites

I did what you said, it isnt crashing but it just isnt damaging my character. Could you take a look at my script to see if I did what you said wrong. Thanks.

 

local assets=
{ 
    Asset("ANIM", "anim/gauntlet.zip"),
    Asset("ANIM", "anim/swap_gauntlet.zip"), 

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

local prefabs = 
{
}

local function onthrown(inst, data)
    inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
end

local function OnAttack(weapon, attacker, target)
    if attacker~=nil and attacker.components~=nil and attacker.components.health~=nil and not attacker.components.health:IsDead() then
        attacker.components.health:DoDelta(100, 1)
    end
end

local function fn(colour)

    local function OnEquip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_object", "swap_gauntlet", "swap_gauntlet")
        owner.AnimState:SetMultiSymbolExchange("swap_object", "hand")
        owner.AnimState:Show("ARM_carry") 
        owner.AnimState:Hide("ARM_normal") 
    end

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

    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)
    
    anim:SetBank("gauntlet")
    anim:SetBuild("gauntlet")
    anim:PlayAnimation("idle", true)
    
    inst.entity:AddNetwork()
    if not TheWorld.ismastersim then
        return inst
    end
    inst.entity:SetPristine()

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(10)
    inst.components.weapon:SetRange(4)
    inst.components.weapon:SetOnAttack(OnAttack)
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "gauntlet"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/gauntlet.xml"
    
    inst:AddComponent("inspectable")
    
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(25555555)
    inst.components.finiteuses:SetUses(25555555)
    inst.components.finiteuses:SetOnFinished(inst.Remove)
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )

    return inst
end

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

 

Link to comment
Share on other sites

I figured it out, I was using a positive value instead of a negative value hahaha. Cheers mate.

For anyone wondering, this is the final script. The weapon will do 2000 of damage with good range, but for a price of 125 health, (Infinity Gaunlet for my Thanos character) 

 

local assets=
{ 
    Asset("ANIM", "anim/gauntlet.zip"),
    Asset("ANIM", "anim/swap_gauntlet.zip"), 

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

local prefabs = 
{
}

local function OnAttack(weapon, attacker, target)
    if attacker~=nil and attacker.components~=nil and attacker.components.health~=nil and not attacker.components.health:IsDead() then
        attacker.components.health:DoDelta(-125)
    end
end

local function onthrown(inst, data)
    inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
end


local function fn(colour)

    local function OnEquip(inst, owner)
        owner.AnimState:OverrideSymbol("swap_object", "swap_gauntlet", "swap_gauntlet")
        owner.AnimState:SetMultiSymbolExchange("swap_object", "hand")
        owner.AnimState:Show("ARM_carry") 
        owner.AnimState:Hide("ARM_normal") 
    end

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

    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)
    
    anim:SetBank("gauntlet")
    anim:SetBuild("gauntlet")
    anim:PlayAnimation("idle", true)
    
    inst.entity:AddNetwork()
    if not TheWorld.ismastersim then
        return inst
    end
    inst.entity:SetPristine()

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(2000)
    inst.components.weapon:SetRange(4)
    inst.components.weapon:SetOnAttack(OnAttack)
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "gauntlet"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/gauntlet.xml"
    
    inst:AddComponent("inspectable")
    
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(25555555)
    inst.components.finiteuses:SetUses(25555555)
    inst.components.finiteuses:SetOnFinished(inst.Remove)
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )

    return inst
end

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

 

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