Jump to content

[need help] TallBirds do not attack the character mod


Recommended Posts

Hi everyone)

I'm trying to make a character who is not targeted by tallbirds and beefalo in heat, but I cannot figure out how to make this work.

I'm trying to add tags to character's lua like this:

local function common_postinit(inst)
    inst:AddTag("magizoologist") -- animals do not target him
end

And this part of code for tallbirds into modmain.lua

local require = GLOBAL.require
local STRINGS = GLOBAL.STRINGS


local function Retarget(inst)
    local function IsValidTarget(guy)
        return not guy.components.health:IsDead()
            and inst.components.combat:CanTarget(guy)
            --smallbirds that aren't companions are parented
            --to other tallbirds, so don't target them!
            and (not inst:HasTag("smallbird") or inst:HasTag("companion") or inst:HasTag("magizoologist"))
    end
    return --Threat to nest
        inst.components.homeseeker ~= nil and
        inst.components.homeseeker:HasHome() and
        FindEntity(
            inst.components.homeseeker.home,
            SpringCombatMod(TUNING.TALLBIRD_DEFEND_DIST),
            IsValidTarget,
            { "_combat", "_health" },
            { "tallbird" },
            { "character", "animal", "monster" })
        or --Nearby pigman (Why the hatred for pigs? It's expensive!)
        FindEntity(
            inst,
            SpringCombatMod(TUNING.TALLBIRD_TARGET_DIST),
            IsValidTarget,
            { "pig", "_combat", "_health" },
            { "werepig" })
        or --Nearby character or monster
        FindEntity(
            inst,
            SpringCombatMod(TUNING.TALLBIRD_TARGET_DIST),
            IsValidTarget,
            { "_combat", "_health" },
            { "tallbird" },
            { "character", "monster" })
end

This code is copied from tallbird prefab lua, but has a tag added in the first section.

Seems logical, but uhh, doesn't work at all. Can anybody tell me what am I doing wrong?

Thanks for help!

Link to comment
Share on other sites

Hi @keinekiv, welcome to the forums!

Could this be what you meant?

	local function IsValidTarget(guy)
		return not guy.components.health:IsDead()
			and inst.components.combat:CanTarget(guy)
			--smallbirds that aren't companions are parented
			--to other tallbirds, so don't target them!
			and (not inst:HasTag("smallbird") or inst:HasTag("companion"))
			and not inst:HasTag("magizoologist")
	end

 

Link to comment
Share on other sites

UPD: I figured that I can use this code instead, and just include all monsters I want to attac my character in the second line. Can somabody tell me whether it affects only my character or also others on the map, and if it affects others, how to avoid it?

local function PassiveUntilAttack(self)
	if self.inst:HasTag("shadow") or self.inst:HasTag("hound")
		then
		return
	end
	local _SetRetargetFunction = self.SetRetargetFunction
	self.SetRetargetFunction = function(self, period, fn)
		if fn then
			local _fn = fn
			fn = function(...)
				local _target = _fn(...)
				if _target and _target.prefab == "newtscamander" then
					return
				end
				return _target
			end
		end
		return _SetRetargetFunction(self, period, fn)
	end
end

AddComponentPostInit("combat", PassiveUntilAttack)

 

Thank you all for your replies! I will try to find this "Wegg" character and take a look.

Tried this, and still no result. I guess I have to tell the game to apply this part of code to the tallbirds, but I have no idea how, though...

the string must be GLOBAL.FindEntity, but I wonder what next.

2 hours ago, alainmcd said:

Hi @keinekiv, welcome to the forums!

Could this be what you meant?


	local function IsValidTarget(guy)
		return not guy.components.health:IsDead()
			and inst.components.combat:CanTarget(guy)
			--smallbirds that aren't companions are parented
			--to other tallbirds, so don't target them!
			and (not inst:HasTag("smallbird") or inst:HasTag("companion"))
			and not inst:HasTag("magizoologist")
	end

 

 

Edited by keinekiv
Link to comment
Share on other sites

Sorry, I assumed there was more to your modmain.lua, including the AddPrefabPostInit. And yes, you'd need to sprinkle a few GLOBAL's around. Here's a "full" version (untested):

Spoiler

local function Retarget(inst)
	local function IsValidTarget(guy)
		return not guy.components.health:IsDead()
			and inst.components.combat:CanTarget(guy)
			--smallbirds that aren't companions are parented
			--to other tallbirds, so don't target them!
			and (not inst:HasTag("smallbird") or inst:HasTag("companion"))
			and not inst:HasTag("magizoologist")
	end
    return --Threat to nest
        inst.components.homeseeker ~= nil and
        inst.components.homeseeker:HasHome() and
        GLOBAL.FindEntity(
            inst.components.homeseeker.home,
            GLOBAL.SpringCombatMod(GLOBAL.TUNING.TALLBIRD_DEFEND_DIST),
            IsValidTarget,
            { "_combat", "_health" },
            { "tallbird" },
            { "character", "animal", "monster" })
        or --Nearby pigman (Why the hatred for pigs? It's expensive!)
        GLOBAL.FindEntity(
            inst,
            GLOBAL.SpringCombatMod(TUNING.TALLBIRD_TARGET_DIST),
            IsValidTarget,
            { "pig", "_combat", "_health" },
            { "werepig" })
        or --Nearby character or monster
        GLOBAL.FindEntity(
            inst,
            GLOBAL.SpringCombatMod(TUNING.TALLBIRD_TARGET_DIST),
            IsValidTarget,
            { "_combat", "_health" },
            { "tallbird" },
            { "character", "monster" })
end

local function tallbirdpostinit(inst)
	if not GLOBAL.TheWorld.ismastersim then
		return
	end
	
	if inst.components.combat then
		inst.components.combat:SetRetargetFunction(3, Retarget)
	end
end

AddPrefabPostInit("tallbird", tallbirdpostinit)

 

Going by the new code you provided, this could suit your needs (again, untested):

Spoiler

local function PassiveUntilAttack(self)
	if self.inst.prefab ~= "tallbird" and self.inst.prefab ~= "beefalo" then
		return
	end
	
	local _SetRetargetFunction = self.SetRetargetFunction
	self.SetRetargetFunction = function(self, period, fn)
		if fn then
			local _fn = fn
			fn = function(...)
				local _target = _fn(...)
				if _target and _target.HasTag("magizoologist") then
					return
				end
				return _target
			end
		end
		return _SetRetargetFunction(self, period, fn)
	end
end

AddComponentPostInit("combat", PassiveUntilAttack)

 

 

Link to comment
Share on other sites

Hi again) Tested both codes, first one still doesn't do anything, but the second one works like a charm, so thank you very much for your help <3

Can I ask another question? Is it possible to make an item like a backpack, but which will be equipped into hands slot instead? I've browsed through many topics here, but all they do is create 4th slot for an item, which is not what I'm looking for...

Edited by keinekiv
Link to comment
Share on other sites

Copy as much as you need from the backpack prefab, but remove the line

inst.components.equippable.equipslot = EQUIPSLOTS.BODY

The equippable component defaults to the hand slot, so that would take care of itself. However, I don't know where the widget will appear. Try

inst.components.container:WidgetSetup("backpack")

after declaring the container component, just like the basic backpack. If the widgets overlap or don't work for any other reason, try moving your widget around. Place this in your prefab file:

local mypack =
{
    widget =
    {
        slotpos = {},
        animbank = "ui_backpack_2x4",
        animbuild = "ui_backpack_2x4",
        pos = Vector3(-175, -70, 0),
    },
    issidewidget = true,
    type = "pack",
}

for y = 0, 3 do
    table.insert(mypack.widget.slotpos, Vector3(-332, -75 * y + 114, 0))
    table.insert(mypack.widget.slotpos, Vector3(-332 + 75, -75 * y + 114, 0))
end

and again calling WidgetSetup from your main prefab function:

inst.components.container:WidgetSetup("mypack", mypack)

Note: I haven't done anything like this, and this is all untested. Move things around as you need. Check data/scripts/containers.lua to see where I started from.

Link to comment
Share on other sites

The second option worked perfectly, thank you soooooo much! Just one last thing... I have all animations done and they work smoothly, but whenever I drop my item on the ground, it becomes invisible, and if I hover mouse over it it's got no name, nothing. But if I push the spacebar - voila, it's in my inventory again, picked from the ground with name and so on.

Also I tried to introduce it so it showed up on the map like this, but it also doesn't show up on map if dropped.

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.MiniMapEntity:SetIcon("newtscase.png")
    inst.AnimState:SetBank("newtscase")
    inst.AnimState:SetBuild("newtscase")
    inst.AnimState:PlayAnimation("anim")

    inst.foleysound = "dontstarve/movement/foley/krampuspack"

    inst:AddTag("krampus_sack")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

Must be a naming mistake, I guess, but I'm sure I've renamed all builds, so this might be coding problem again...(

Link to comment
Share on other sites

If it's invisible on the ground, then the problem is with your anim, nothing related to the code given.

For minimap icon, i use :


        inst.MiniMapEntity:SetIcon("myprefab.tex")

in the prefab, like your like (but i'm using a .tex file, don't know if it works with .png)

And in the modmain, in the asset list :


			Asset( "IMAGE", "minimap/myprefab.tex" ),
			Asset( "ATLAS", "minimap/myprefab.xml" ),

And in the modmain :


		AddMinimapAtlas("minimap/myprefab.xml")

 

Link to comment
Share on other sites

I don't know where is the problem exactly.

What i know is that sometimes, you need an anim for when the item is on the ground, and one when the item is in the hand of the player. Usually, nameofanim and swap_nameofanim. Here, since it's a custom anim i don't know if it's the same thing, and if you miss something in the assets, in the anim, in the name or anywhere else...

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