Jump to content

I'm trying to make a character's own spear, and I need help.


Recommended Posts

Hello everyone who is reading, I need help from more experienced people. I have very little understanding of coding, but I have already made some small progress (alas, only with night vision). I just can’t build the code for a custom spear, it’s quite ordinary, the battle spear code could even work for me, but alas, I can’t find it anywhere (in fact, I had an idea that when hit, the spear would cast poison, but I don’t know how to do even that). If anyone can help, I will be very glad.

P.S. Here is the code that I somehow put together, the mod certainly didn’t crash when entering the game, but there were no weapon in my inventory or craft:

Spoiler

local assets=
{    
    Asset("ANIM", "anim/corrupt_spear.zip"),
    Asset("ANIM", "anim/swap_corrupt_spear.zip"),
    
    Asset("ATLAS", "images/inventoryimages/corrupt_spear.xml"),
    Asset("IMAGE", "images/inventoryimages/corrupt_spear.tex"),
    
}

local prefabs = 
{
}

local function OnEquip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_corrupt_spear", "corrupt_spear")
    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 function fn(sim)
    
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    local sound = inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
    
    MakeInventoryPhysics(inst)
    inst.entity:AddMiniMapEntity()
    inst.MiniMapEntity:SetIcon( "corrupt_spear.tex" )
    
    anim:SetBank("corrupt_spear")
    anim:SetBuild("corrupt_spear")
    anim:PlayAnimation("idle")
    
    --Weapon Ascpect
    inst:AddTag("sharp")
    inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end
    
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(51)
    -----
    

    
    -------
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")


    inst.components.inventoryitem.imagename = "corrupt_spear"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/corrupt_spear.xml"
    
    inst.components.inventoryitem.cangoincontainer = false
    inst.components.inventoryitem.onputininventoryfn = function(inst, player) 
    if player.prefab ~= "corrupt" then 
        inst:DoTaskInTime(0.1, function()
        
        player.components.inventory:DropItem(inst)
        player.components.talker:Say("It seems to slip right out of my hands!")
    end)
    end
    end
    
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )
    
    return inst
end

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

 

Link to comment
Share on other sites

9 hours ago, Baguettes said:

Does the prefab itself exist in the game? just curious, I'm not expert at these kinda things either but I want to make sure whether the prefab is added to the game or not.

Yes, it seems like there is, but even when I try to spawn it, it-.. Well, it’s just not there, it’s not invisible, it’s not there.

sorry my english is not very good

Link to comment
Share on other sites

On 3/20/2024 at 4:15 PM, Wuf said:

anim:SetBank("corrupt_spear")
    anim:SetBuild("corrupt_spear")
    anim:PlayAnimation("idle")

Can you perhaps try uhh, turning these into:

inst.AnimState:SetBank("corrupt_spear")
inst.AnimState:SetBuild("corrupt_spear")
inst.AnimState:PlayAnimation("idle")

Because right now from what I see, you're using local anim = inst.entity:AddAnimState() which would mean that Lua would probably see it as
inst.entity:AddAnimState():SetBank("corrupt_spear") and to me that just feels odd.

Also, I think you can just use inst.entity:AddAnimState() instead of local anim = inst.entity:AddAnimState().

Edited by Baguettes
Link to comment
Share on other sites

23 hours ago, Baguettes said:

Can you perhaps try uhh, turning these into:

inst.AnimState:SetBank("corrupt_spear")
inst.AnimState:SetBuild("corrupt_spear")
inst.AnimState:PlayAnimation("idle")

Because right now from what I see, you're using local anim = inst.entity:AddAnimState() which would mean that Lua would probably see it as
inst.entity:AddAnimState():SetBank("corrupt_spear") and to me that just feels odd.

Also, I think you can just use inst.entity:AddAnimState() instead of local anim = inst.entity:AddAnimState().

(6o 6) I'll try, thanks

Link to comment
Share on other sites

On 3/31/2024 at 1:26 PM, Baguettes said:

Can you perhaps try uhh, turning these into:

inst.AnimState:SetBank("corrupt_spear")
inst.AnimState:SetBuild("corrupt_spear")
inst.AnimState:PlayAnimation("idle")

Because right now from what I see, you're using local anim = inst.entity:AddAnimState() which would mean that Lua would probably see it as
inst.entity:AddAnimState():SetBank("corrupt_spear") and to me that just feels odd.

Also, I think you can just use inst.entity:AddAnimState() instead of local anim = inst.entity:AddAnimState().

Alas, it didn't work

Link to comment
Share on other sites

On 4/3/2024 at 4:45 PM, Baguettes said:

Ah heck. Clearly I'm not cut out for most of these things. I guess we need to wait for real professional help, sorry I can't help more.

It's okay, it was at least an attempt. Thank you

Link to comment
Share on other sites

On 3/30/2024 at 3:27 PM, Wuf said:

Yes, it seems like there is, but even when I try to spawn it, it-.. Well, it’s just not there, it’s not invisible, it’s not there.

sorry my english is not very good

I'm not exactly an expert, but have you tried putting this in your modmain.lua? 

PrefabFiles = {
	"common/inventory/corrupt_spear"
}

I also am pretty sure that you don't need to put a file path as the name of your prefab, you can just put the item name in return prefab() (maybe that's the problem, maybe you tried to spawn a prefab called "corrupt_spear" when the only item that was added to the game was "common/inventory/corrupt_spear"). I also dont think that you need to have a prefab table because i've made custom items without these two things.
if that does work, and your item spawns in the game then i think i also know how to make the poison mechanic, in the health component there is a function called StartRegen(amount, period, interruptcurrentregen) so the code would look something like this (i havent tested it but this should maybe work)

local function onhit(inst, owner, target)
	target.components.health:StartRegen(-1, 5) -- you should probably also check if the target exists and has a health component
end

--other stuff until you add the weapon component

inst.components.weapon:SetOnAttack(onhit)

-- the rest of the code
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...