Jump to content

Recommended Posts

Hi, I had an idea about shotgun (or other firearm) as main weapon of my character in DST. I have created 3 character mods so far, but I used Extended Sample Character Template by Dleowolf, so basically I do not have any knowledge and I have a little experience about simple modding. Anyway I want my character to be badass with shotgun, but I totally don't know how to achieve it. Programing in Lua is black magic to me and I do not want to bother one very kind person from this forum everytime I need help with something trivial for veteran modders.

I would be grateful for every clue. Thank you in advance and have a nice day.

Hmm interesting I thought someone else would have the code for something like this, guess not. I only have code that's close to a gun/crossbow so I haven't coded something like the spread of the pellets to hit multiple enemies. First you need something the modmain:

local STRINGS = GLOBAL.STRINGS
local ACTIONS = GLOBAL.ACTIONS
local Action = GLOBAL.Action

AddComponentPostInit("combat", function(Combat, inst)
    local DoAttack_prev = Combat.DoAttack
    function Combat:DoAttack(target_override, weapon, ...)
        if weapon and weapon.HasAmmo then
            if weapon:HasAmmo(inst) then
                return DoAttack_prev(self, target_override, weapon, ...)
            end
            return
        end        
        return DoAttack_prev(self, target_override, weapon, ...)
    end
end)

Next is the weapon lua:

inst.components.weapon:SetDamage(0)
    inst.components.weapon:SetRange(30)
    inst.components.weapon:SetProjectile("batteries")

inst.HasAmmo = function(inst, owner)
        if (owner and owner.components.inventory and owner.components.inventory:Has("ammo", 1)) then
            owner.components.inventory:ConsumeByName("ammo", 1)
            inst.components.weapon:SetDamage(570)
            return true
        end
        return false
    end

And there's your black magic.

This makes me think you could do an AOE attack as a cone instead of a circle. That way you would just hit whatever enemies are within the AOE instead of worrying about separate pellets. It would almost be a melee weapon, but you just have extended range and hit multiple enemies within an area. Just a few ideas.

I've used code by K1NGT1GER609 and my character swings with weapon (I used it on sword-a-like thing) while staying in place. It doesn't come close to target and swing, just swing from place where it was. Is it caused by absence of ammo ? What shall I set as ammo then and how of course.

Oh yeah the ammo you'll need to make the bullets with this:

use the sampleprefab.zip and in the lua file add these lines of code for the shotgun:

local function OnHit(inst, owner, target)
    inst:Remove()    
end

inst:AddTag("projectile")

if not TheWorld.ismastersim then
        return inst
    end

inst:AddComponent("stackable")    
    inst.components.stackable.maxsize = 40
    
    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(40)
    inst.components.projectile:SetOnHitFn(OnHit)
    inst.components.projectile:SetOnMissFn(OnHit)

Edited by K1NGT1GER609

Actually this line of code should be above the main(fn(sim)):

local function OnHit(inst, owner, target)
    inst:Remove()    
end

in the ammo lua file. Declaring the function after the main the code the game will never see the function.

Edited by K1NGT1GER609

When I keep these code lines:

if not TheWorld.ismastersim then
        return inst
 end

It gives me error.


In other hand when I delete it there is no error, but when I attack something further away then character swings and nothing happens. I have an ammo in inventory and still nothing.

error 2.png

The code:

if not TheWorld.ismastersim then
        return inst
 end

needs to be between the animstate and inventory item like so:

inst.AnimState:SetBank("ammo")
    inst.AnimState:SetBuild("ammo_build")
    inst.AnimState:PlayAnimation("idle", true)

if not TheWorld.ismastersim then
        return inst
 end

inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/ammo.xml"

without theworld.ismastersim you get some weird errors.

So your saw is a gun? I'm not sure where your going with this. Your not using ammo probably cause your missing the code above:

inst.components.weapon:SetDamage(0)
    inst.components.weapon:SetRange(12)
    inst.components.weapon:SetProjectile("ammo")

inst.HasAmmo = function(inst, owner)
        if (owner and owner.components.inventory and owner.components.inventory:Has("ammo", 1)) then
            owner.components.inventory:ConsumeByName("ammo", 1)
            inst.components.weapon:SetDamage(570)
            return true
        end
        return false
    end

 

Edited by K1NGT1GER609

Yay. It works ! After swing bird dies. I don't see projectile but it's kinda fun to kill with it. It can be used as Soul Sword or something like that to kill with swing.  BTW if I want to make my animation similar to recoil from firing shotgun, shall I change animation.xml and build.xml in exported>swap_wand>swap_wand_saw.zip ? If yes, can I use any program to preview weapon position in character's hands ?

What can I do to make projectiles visible ? I made some artworks for this new weapon. Now it's sawn-off shotgun not regular saw for cutting wood. I need projectiles to finish it off (except animations).

 

 

Edited by Yakuzashi

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