Jump to content

I have no idea on how to code this weapon...


Recommended Posts

I want to add a weapon to my character mod, the issue is that i have NO IDEA on how to code it, (i'm really new to making mods for DST)

It's just like the Shield of terror, so it can be repaired with food, but it cannot be destroyed when reaching 0%, and also has hambat-like damage, dealing more damage the more full it is.

Does anyone have any idea on how can i make it? i've been trying to "merge" the part of the code i need from the items i mentioned, but so far i haven't made much progress.

Here are the items script and the modmain.lua file, just in case

hungern.lua modmain.lua

Link to comment
Share on other sites

Hey-hey!

I have a similar weapon I made earlier by chance.

First of all, you have to add the right components:

Weapon Component:

inst:AddComponent("weapon")
inst.components.weapon:SetDamage(ScaleDamage) -- You can name the function inside the brackets as you wish.

Finiteuses Component:

inst:AddComponent("finiteuses") -- This means that it will have a percentage counter.
inst.components.finiteuses:SetMaxUses(10) -- In this example I will give it 10 uses, but you can change it to your likings.
inst.components.finiteuses:SetUses(10) -- Same value.

Eater Component:

inst:AddComponent("eater") -- This will add the feed option to the weapon.
inst.components.eater:SetOnEatFn(oneatfn) -- This line will call the function that will happen when you feed the weapon, name it as you want.
inst.components.eater:SetAbsorptionModifiers(4.0, 1.75, 0) -- I'm not sure what this is, I just copied it.
inst.components.eater:SetCanEatGears(true) -- My weapon eats gears, search around to see what you can replace this with. Run a search with Notepad ++ inside the game's scripts files with the search term of "SetCanEat" to see what other options you have.

You also have to add two tags to make it all work:

inst:AddTag("handfed")
inst:AddTag("fedbyall")

The only things reaminging are the functions themselves:

local function ScaleDamage(inst)
    local dmg = 50 * inst.components.finiteuses:GetPercent() -- This calculates the damage the weapon will deal. This version multiplies 50 by the weapon's current durability percentage. Change it to your likings.
    return Remap(dmg, 0, 50, 10, 50) -- You need these to set a min and a max damage to your weapon. 50 is the max and 10 is the min I've set, but you can change them to something else as well. Leave the zero alone, that's a necessity, but I don't know why.
end
local function oneatfn(inst, food)
    inst.components.finiteuses:SetUses(50) -- This will set the uses to a desired value.
end

This should be it. I'll include my weapon here as a reference for your own.

Hope this helps!

Cheers!

graft_knight.lua

Link to comment
Share on other sites

8 hours ago, Goregonzola said:

Hey-hey!

I have a similar weapon I made earlier by chance.

First of all, you have to add the right components:

Weapon Component:

inst:AddComponent("weapon")
inst.components.weapon:SetDamage(ScaleDamage) -- You can name the function inside the brackets as you wish.

Finiteuses Component:

inst:AddComponent("finiteuses") -- This means that it will have a percentage counter.
inst.components.finiteuses:SetMaxUses(10) -- In this example I will give it 10 uses, but you can change it to your likings.
inst.components.finiteuses:SetUses(10) -- Same value.

Eater Component:

inst:AddComponent("eater") -- This will add the feed option to the weapon.
inst.components.eater:SetOnEatFn(oneatfn) -- This line will call the function that will happen when you feed the weapon, name it as you want.
inst.components.eater:SetAbsorptionModifiers(4.0, 1.75, 0) -- I'm not sure what this is, I just copied it.
inst.components.eater:SetCanEatGears(true) -- My weapon eats gears, search around to see what you can replace this with. Run a search with Notepad ++ inside the game's scripts files with the search term of "SetCanEat" to see what other options you have.

You also have to add two tags to make it all work:

inst:AddTag("handfed")
inst:AddTag("fedbyall")

The only things reaminging are the functions themselves:

local function ScaleDamage(inst)
    local dmg = 50 * inst.components.finiteuses:GetPercent() -- This calculates the damage the weapon will deal. This version multiplies 50 by the weapon's current durability percentage. Change it to your likings.
    return Remap(dmg, 0, 50, 10, 50) -- You need these to set a min and a max damage to your weapon. 50 is the max and 10 is the min I've set, but you can change them to something else as well. Leave the zero alone, that's a necessity, but I don't know why.
end
local function oneatfn(inst, food)
    inst.components.finiteuses:SetUses(50) -- This will set the uses to a desired value.
end

This should be it. I'll include my weapon here as a reference for your own.

Hope this helps!

Cheers!

graft_knight.lua 2.72 kB · 0 downloads

Thanks, this was really useful!!

But i seem to have encountered a problem with something. I wanted the item to also give protection, so i added the "armor" component, but with the "finiteuses" component the item had two durabilities, one while recieving damage and other while attacking.

I tried using only the armor component and it only lost durability while recienving damage, so i used this to try and make it lose it while attacking:
(I took it from the Shield of terror's script)

inst._weaponused_callback = function(_, data)
        if data.weapon ~= nil and data.weapon == inst then
            inst.components.armor:TakeDamage(2)
        end

But... it didn't work, any clue on why it doesn't?

Also, i don't know how to make it so it doesn't break when at 0% (Like lanterns), i think making a local function like this one might work, but i don't know how to make it work in this case

local function oncurrentfuel(self, currentfuel)
    if currentfuel <= 0 then
        self.inst:AddTag("fueldepleted")
    else
        self.inst:RemoveTag("fueldepleted")
    end
    onmaxfuel(self, self.maxfuel)

Any ideas on how to make it work?

Thanks in advance and sorry for asking too much questions

hungern.lua

Edited by CrisChemical
Added the items script
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...