Jump to content

Recommended Posts

 

As mentioned in my previous post,

I solved the issue with sanity loss from murder, then added another condition, as the game didn't know what to do when character kills pigs. For some reason there is an issue, game requires function to be closed right before else statement in OnKill function, closing also obviously doesn't work. I am not that familiar with lua but this mistake seems out of place. Does anyone have clues as to why it might happen and how to fix?

local function SanitysFall(inst, ini_time)          
    if inst.components.sanity then          
        inst.components.sanity:DoDelta(-1)      
    end            
    if GLOBAL.GetTime() > ini_time + 20 then            
        inst.lose_sanity_task:Cancel()          
        inst.lose_sanity_task = nil    
    end
end
 
local function OnKill(inst, data)
    local victim = data.victim
    if victim and not victim:HasTag("prey") and not victim:HasTag("pigman") and inst.components.sanity then
        if inst.lose_sanity_task ~= nil then
            inst.lose_sanity_task:Cancel() end      
            inst.lose_sanity_task = nil            
            inst.lose_sanity_task = inst:DoPeriodicTask(1, SanitysFall, nil, GLOBAL.GetTime())  
        end  
    else
    if victim:HasTag("prey") or victim:HasTag("pigman") and inst.components.sanity then
        inst.lose_sanity_task = nil
    end
 
    end
end

You seem to have an additional end at the end of your OnKill function
Also, I would suggest making a function for stopping and starting sanity drain, probably. Though as I observed, the way the game is coded it doesnt do that for some reason.
Let me know your results with this, it probably wont work though if I messed something up.
It is useful to look at your client_log or master_server_log for error information.
I refactored the code to look like this to be more readable.

Spoiler
local function StopSanityDrain(inst)
	if inst.lose_sanity_task then -- Unless the task is something that returns false then this works, otherwise use inst.lose_sanity_task ~= nil
		inst.lose_sanity_task:Cancel()
		inst.lose_sanity_task = nil
	end
end

local function SanityDrain(inst, ini_time)          
    if not inst.components.sanity then 
		StopSanityDrain(inst) --might be best to stop the task if sanity doesnt exist
		return
	end       
	inst.components.sanity:DoDelta(-1)               
    if GLOBAL.GetTime() > ini_time + 20 then            
        StopSanityDrain(inst)
    end
end

local function StartSanityDrain(inst)
	StopSanityDrain(inst)
	inst.lose_sanity_task = inst:DoPeriodicTask(1, SanityDrain, nil, GLOBAL.GetTime())
end
 
local function OnKill(inst, data)
    local victim = data.victim
	if not victim then return end
    if not victim:HasTag("prey") and not victim:HasTag("pigman") then --  perhaps you meant victim.prefab == "pigman" or HasTag"pig"?
        StartSanityDrain(inst)
    elseif victim:HasTag("prey") or victim:HasTag("pigman") then
        StopSanityDrain(inst)
    end
end 

 

 

Edited by oregu

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