Jump to content

[CUSTOM CHARACTER] making mob passive to mod character


Recommended Posts

hello! first time making a mod, and I am trying to make a custom character that bees will not attack. 

this link is the only thread I could find on the subject, but either the files for bees are written differently, or the way the code is written has changed. 

I have added the tag "beefriend" to the character as shown below; I don't know if it is in the right place

local function onbecamehuman(inst)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "alyx_speed_mod", 2)
	inst:AddTag("beefriend")

here is the code that I think dictates what the bees attack

local function KillerRetarget(inst)
    return FindEntity(inst, SpringCombatMod(8),
        function(guy)
            return inst.components.combat:CanTarget(guy)
        end,
        { "_combat", "_health" },
        { "insect", "INLIMBO" },
        { "character", "animal", "monster" })
end

local function SpringBeeRetarget(inst)
    return TheWorld.state.isspring and
        FindEntity(inst, 4,
            function(guy)
                return inst.components.combat:CanTarget(guy)
            end,
            { "_combat", "_health" },
            { "insect", "INLIMBO" },
            { "character", "animal", "monster" })
        or nil
end

if anyone knows where and how to add the tag to the code, and where to put that code in the modmain.lua , that would help me a lot.

 

thank you for reading!

Link to comment
Share on other sites

Adding the tag in onbecamehuman will add the tag every time the character is revived or joins the game. Just add it to the character at the top of your character's fn() function.

Looking at the code in the combat-component, it seems to me that you should take a look at the IsValidTarget function in the combat-component of the bees. Extend that function, and return false if the character is your character. Here's an example for the normal bee. To also change the killer bees, copy this, and change "bee" to "killerbee".

AddPrefabPostInit("bee", function(inst)
	if inst.components.combat then
		-- Store the original IsValidTarget function (this will keep it compatible with other mods which might also have extended this function)
		local old_IsValidTarget = inst.components.combat.IsValidTarget
		-- Replace the IsValidTarget function with a new one. Notice that we add "self" to it, since the function is declared with a :.
		inst.components.combat.IsValidTarget = function (self, target)
			if owner.prefab ~= "YOUR_CHARACTER_PREFAB_NAME"
				return false
			end
			-- Run the original IsValidTarget function, if your check above has not already returned from our function.
			return old_IsValidTarget(self, target)
		end
	end
end)

The two functions in the code snippets you provided are both local functions, which makes them hard to extend. To do it right, meaning not just copying them and replacing them, there would have to be non-local functions available to change those local functions in the bees, and there aren't.

Edited by Ultroman
Link to comment
Share on other sites

EDIT: I looked into it more, and the two functions in your snippets are actually "attached" to the combat-components of the bees using the SetRetargetFunction function, and their original function is stored in a variable which you have access to (I think). The problem with just attaching a new retarget-function to each type of bee (i.e. you just copy the code, and change it a bit, and attach your new function using the SetRetargetFunction function), is that if Klei ever changes something in the way the bees work, your mod might stop working or introduce weird bugs, since you would be overriding the original code instead of extending it. What are the chances of this? They're probably not high, but we never know. Overriding means you have to keep tabs on whether Klei updates their code. Extending means you don't have to care (unless they completely change things, so the function is removed or something like that).

If you don't care about that, you can override the retarget functions using the SetRetargetFunction function, and simply add your tag to the "ignore"-tags:

local function KillerRetarget(inst)
    return GLOBAL.FindEntity(inst, GLOBAL.SpringCombatMod(8),
        function(guy)
            return inst.components.combat:CanTarget(guy)
        end,
        { "_combat", "_health" },
        { "insect", "INLIMBO", "beefriend" }, -- <=== there
        { "character", "animal", "monster" })
end

local function SpringBeeRetarget(inst)
    return TheWorld.state.isspring and
        GLOBAL.FindEntity(inst, 4,
            function(guy)
                return inst.components.combat:CanTarget(guy)
            end,
            { "_combat", "_health" },
            { "insect", "INLIMBO", "beefriend" }, -- <=== there
            { "character", "animal", "monster" })
        or nil
end

 

And then do:

AddPrefabPostInit("bee", function(inst)
	if inst.components.combat then
		inst.components.combat:SetRetargetFunction(2, SpringBeeRetarget)
	end
end)

AddPrefabPostInit("killerbee", function(inst)
	if inst.components.combat then
		inst.components.combat:SetRetargetFunction(2, KillerRetarget)
	end
end)

 

Edited by Ultroman
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...