Jump to content

Recommended Posts

Hi! I'm fairly new to modding and I'm trying to make Walter be able to melee attack while on Woby. I've tried to look into beefalo.lua, SGbeefalo.lua and other files to add something similar but failed to understand how exactly player can command to attack while riding.

I've added combat component to Woby prefab, "doattack" event handler and "attack" state to the state graph. I can start the world with this mod enabled, but it is doing nothing.

Can you please point me in some direction because now I'm absolutely lost.

Here should be my code if I'm pasting it correctly:

local env = env
env._G = GLOBAL._G
GLOBAL.setfenv(1, env)

local RETARGET_MUST_TAGS = { "_combat" }
local RETARGET_CANT_TAGS = {"wobybig", "wall", "INLIMBO" }

local function Retarget(inst)
    return GLOBAL.FindEntity(
                inst,
                GLOBAL.TUNING.BEEFALO_TARGET_DIST,
                function(guy)
                    return inst.components.combat:CanTarget(guy)
                        and (guy.components.rider == nil
                            or guy.components.rider:GetMount() == nil
                            or not guy.components.rider:GetMount():HasTag("wobybig"))
                end,
                RETARGET_MUST_TAGS, --See entityreplica.lua (re: "_combat" tag)
                RETARGET_CANT_TAGS
            )
        or nil
end

local function OnNewTarget(inst, data)
    if data ~= nil and data.target ~= nil and inst.components.follower ~= nil and data.target == inst.components.follower.leader then
        inst.components.follower:SetLeader(nil)
    end
end

local function CanShareTarget(dude)
    return dude:HasTag("beefalo")
        and not dude:IsInLimbo()
        and not (dude.components.health:IsDead() or dude:HasTag("player"))
end

local function go_to_idle(inst)
    inst.sg:GoToState("idle")
end



AddPrefabPostInit("wobybig", function(inst)
if not GLOBAL.TheWorld.ismastersim then
        return inst
    end
	
	inst:AddComponent("combat")
    inst.components.combat.hiteffectsymbol = "beefalo_body"
    inst.components.combat:SetDefaultDamage(TUNING.BEEFALO_DAMAGE.DEFAULT)
    inst.components.combat:SetRetargetFunction(1, Retarget)

	inst:ListenForEvent("newcombattarget", OnNewTarget)
	--inst:ListenForEvent("riderdoattackother", OnRiderDoAttack)

	--AddStategraphState(attack_state)
	--AddStategraphEvent(attack_event_handler)

end)

env.AddStategraphEvent("SGwobybig", 
GLOBAL.EventHandler("doattack", 
	function(inst, data) 
		if not inst.components.health:IsDead() then 
			inst.sg:GoToState("attack", data.target) 
		end 
	end
	)
)

env.AddStategraphState("SGwobybig", 
	GLOBAL.State{
        name = "attack",
        tags = {"attack", "busy"},

        onenter = function(inst, target)
            inst.sg.statemem.target = target
            --inst.SoundEmitter:PlaySound(inst.sounds.angry)
            inst.components.combat:StartAttack()
            inst.components.locomotor:StopMoving()
            inst.AnimState:PlayAnimation("atk_pre")
            inst.AnimState:PushAnimation("atk", false)
        end,

        timeline=
        {
            GLOBAL.TimeEvent(15*GLOBAL.FRAMES, function(inst) inst.components.combat:DoAttack(inst.sg.statemem.target) end),
        },

        events=
        {
            GLOBAL.EventHandler("animqueueover", go_to_idle),
        },
    }
)

Thanks in advance!

Edited by TheTurf
I found solution to the problem so I changed the title to display that
Link to comment
https://forums.kleientertainment.com/forums/topic/158919-solved-modding-woby/
Share on other sites

Hi again! Since I won't probably upload this mod to the workshop I will share the code in case someone is interested. I found that workaround in the Better Beefalo mod and it works good enough for me The idea to add "rangedweapon" tag to an equipped weapon while player riding Woby and to remove it after.

So modmain.lua would look like this:

local EQUIPSLOTS = GLOBAL.EQUIPSLOTS

-- Function to add 'rangedweapon' tag if mounted on Woby and weapon isn't already ranged
local function UpdateWeaponTag(inst)
    local rider = inst.components.rider
    local inventory = inst.components.inventory
    if rider and rider:IsRiding() then
        local mount = rider:GetMount()
        if mount and mount.prefab == "wobybig" then
            -- Add 'rangedweapon' tag to currently equipped item if not already a ranged weapon
            local weapon = inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
            if weapon and not weapon:HasTag("rangedweapon") and not weapon:HasTag("mod_added_rangedweapon") then
                weapon:AddTag("rangedweapon")
                weapon:AddTag("mod_added_rangedweapon") -- Custom tag to track mod's change
            end
            return
        end
    end
end

-- Remove 'rangedweapon' tag if it was added by the mod and dismounted or not riding Woby
local function RestoreWeaponTag(inst)
	local inventory = inst.components.inventory
    local weapon = inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and weapon:HasTag("mod_added_rangedweapon") then
        weapon:RemoveTag("rangedweapon")
        weapon:RemoveTag("mod_added_rangedweapon") -- Clean up our custom tag
    end
end

local function PostInit(inst)
	if not GLOBAL.TheWorld.ismastersim then
        return inst
    end

    inst:ListenForEvent("mounted", UpdateWeaponTag)
    inst:ListenForEvent("dismount", RestoreWeaponTag)
    inst:ListenForEvent("equip", UpdateWeaponTag)
    inst:ListenForEvent("unequip", RestoreWeaponTag)
end

AddPlayerPostInit(PostInit)

I could've missed something but that should do the trick.

I hope someone will find this helpful 

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