Jump to content

[Solved] Restrict survivor's ability to attack


Recommended Posts

Hey-hey!

I'm looking for a bit of guidance about a perk-idea I have in mind. The main goal would be that my survivor could only be able to attack entities with the "hostile" tag.

The prohibition code will probably be one of these, but I'm not sure how the frame of the perk should look like:

Quote

inst.components.combat.canattack = false
inst.components.combat:SetTarget(nil)

Is this a simple local function I should create or something more complicated?

I can't really think of an example for this, as far as I know only non-playable creatures can sort targets.

Thanks in advance!

Cheers!

Edited by C_Thun
Link to comment
Share on other sites

I've tinkered it a bit, but I feel like I'm still very far from the solution. This is the best I came up with so far, but better programmers will just probably just whisper a "Huh, pathetic..." line in anime style:

Quote

local function moral(inst, data)
        if inst.prefab == "samjack" and data.target.components.combat then
            if (not data.target:HasTag("hostile")) then
                inst.components.combat.canattack = false
                inst.components.combat:SetTarget(nil)
            end
        end
    end

This doesn't do anything at the moment : (

Edited by C_Thun
Link to comment
Share on other sites

Okay, I tried adapting some code from my own mod, you'll need to change hatkid to your character's prefab, and add some extra stuff yourself. It will also only work if you have movement prediction disabled on your client. I'll help you later with how to get it working with it, just figured you can test this from now.

Put this in your modmain

AddStategraphPostInit("wilson", function(sg)
    local _attack = sg.states["attack"]
    local _onenter = _attack.onenter
    _attack.onenter = function(inst,...)
        _onenter(inst,...)
        if inst.prefab == "hatkid" and inst.components.combat.target:HasTag("hostile") then
        -- code if hostile
        else
        --code if not hostile
        end
        return
        end
end)

do not do this

Edited by TheSkylarr
Link to comment
Share on other sites

Okay, ignore my previous post, that doesn't work. I've got a a second one for you that doesn't require you to have special code in your characters prefab file, it just works with whatever if statement you want.

AddStategraphPostInit("wilson", function(sg)
	local _attack = sg.states["attack"] -- This specifies which state we're adding to
	local _onenter = _attack.onenter
	_attack.onenter = function(inst,...)
		_onenter(inst,...)
		if inst.prefab == "hatkid" and not inst.components.combat.target:HasTag("monster") then -- this runs if the thing your character is attacking doesn't have the tag "monster"
			inst:ClearBufferedAction() -- Cancel the attack before it hits.
			inst.sg:GoToState("idle", true) -- Go to idle state, technically redundant but it keeps things neat.
			inst.components.talker:Say(GetString(player, "ACTIONFAIL_GENERIC")) -- This will use the characters speech file for this line.
		end
		return
	end
end)

In this example, once you change the prefab name (I used Hat Kid to test it), the specified character will not be able to attack anything that doesn't have the monster tag.

  • Thanks 1
Link to comment
Share on other sites

@TheSkylarr It works like charm, many thanks <3

I had to replace this line

Quote

inst.components.talker:Say(GetString(player, "ACTIONFAIL_GENERIC"))

with

Quote

inst.components.talker:Say("I cannot do that.")

but otherwise it works as intended.

Edited by C_Thun
  • Like 1
Link to comment
Share on other sites

11 minutes ago, C_Thun said:

@TheSkylarr It works like charm, many thanks <3

I had to replace this line

with

but otherwise is works as intended.

Understandable change there, glad I could help. Personally I like having all my speech lines in the speech files, but if works, it works. Good luck with your project.

  • Like 1
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...