Jump to content

(SOLVED) Taking more damage while sanity is low


Recommended Posts

Hey, I'm pretty new to modding am trying to create a script for my mod that makes him take double damage and dish out double damage while his sanity is below 10 or so. Not really sure how to do this but I got a function to make my character take double damage. just need to figure out how to trigger it when he's low on sanity. Anyways, heres my code for the double damage just not sure where to go from here

I have this in modmain.lua and it works fine for the damage part of it
 

local Character = "wraith"
local Damage_Mult = -1 --double damage
AddComponentPostInit("combat",function(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return
    end
    local oldbonusdamagefn = inst.bonusdamagefn
    inst.bonusdamagefn = function(attacker, target, damage, weapon)
        local bonus = 0
        if oldbonusdamagefn then
            bonus = oldbonusdamagefn(attacker, target, damage, weapon) or 0
        end
        if target.prefab == Character and  then
            bonus = bonus - (damage + bonus) * Damage_Mult
        end
        return bonus
    end
end)

Thanks, Luah.

Edited by Luahh
Link to comment
Share on other sites

5 hours ago, Luahh said:

Hey, I'm pretty new to modding am trying to create a script for my mod that makes him take double damage and dish out double damage while his sanity is below 10 or so. Not really sure how to do this but I got a function to make my character take double damage. just need to figure out how to trigger it when he's low on sanity. Anyways, heres my code for the double damage just not sure where to go from here

I have this in modmain.lua and it works fine for the damage part of it
 


local Character = "wraith"
local Damage_Mult = -1 --double damage
AddComponentPostInit("combat",function(inst)
    if not GLOBAL.TheWorld.ismastersim then
        return
    end
    local oldbonusdamagefn = inst.bonusdamagefn
    inst.bonusdamagefn = function(attacker, target, damage, weapon)
        local bonus = 0
        if oldbonusdamagefn then
            bonus = oldbonusdamagefn(attacker, target, damage, weapon) or 0
        end
        if target.prefab == Character and  then
            bonus = bonus - (damage + bonus) * Damage_Mult
        end
        return bonus
    end
end)

Thanks, Luah.

You may want to do this over in your characters prefab instead of your modmain. You can do this to accomplish what you're wanting to do.

local function bonus(inst)
	if inst.components.sanity.currentsanity <= 10 then
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
    	inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
    	inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end
end

--inside master_postinit

inst:ListenForEvent("sanitydelta", bonus)

 

Link to comment
Share on other sites

6 hours ago, RedHairedHero said:

You may want to do this over in your characters prefab instead of your modmain. You can do this to accomplish what you're wanting to do.


local function bonus(inst)
	if inst.components.sanity.currentsanity <= 10 then
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
    	inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
    	inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end
end

--inside master_postinit

inst:ListenForEvent("sanitydelta", bonus)

 

Hey thanks for the reply, but I can't quite figure out how to get this to work. I've tried putting the top part in and out of the master_postinit function above and below it, and the same for the bottom bit, but every time I load up the world it crashes unless the 

inst:ListenForEvent("sanitydelta", bonus)

isn't there, then it works fine. If I put that line of code outside it says the variable 'inst' is not defined. However if I have it inside it crashes without giving me an error on-screen or if I put them both outside the master_postinit. Maybe you can help me and I hope i'm being descriptive enough about my issue.

Thanks, Luah.
 

Link to comment
Share on other sites

It should be structured like this.

local function bonus(inst)
	if inst.components.sanity.currentsanity <= 10 then
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
    	inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
    	inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end
end
                                                
local common_postinit = function(inst)
  --whatever other codes you have
end

local master_postinit = function(inst)
   	--whatever other codes you have
	inst:ListenForEvent("sanitydelta", bonus)
	return inst
end

 

Link to comment
Share on other sites

7 minutes ago, RedHairedHero said:

It should be structured like this.


local function bonus(inst)
	if inst.components.sanity.currentsanity <= 10 then
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
    	inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
    	inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end
end
                                                
local common_postinit = function(inst)
  --whatever other codes you have
end

local master_postinit = function(inst)
   	--whatever other codes you have
	inst:ListenForEvent("sanitydelta", bonus)
	return inst
end

 

Thanks for the quick reply, and after structuring it the way you said I get this error when I load up the game

Line 72 is this btw, 

	if inst.components.sanity.currentsanity <= 10 then

error.PNG

Edited by Luahh
Link to comment
Share on other sites

if inst ~= nil then

	if inst.components.sanity.currentsanity <= 10 then
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
    	inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
    	inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end

end

Link to comment
Share on other sites

11 hours ago, RedHairedHero said:

if inst ~= nil then


	if inst.components.sanity.currentsanity <= 10 then
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
    	inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
    	inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end

end

Thanks, but even after adding that my game still crashes. However I managed to make it not crash by instead of using 

if inst ~= nil then

--I instead used this, and it did not crash

if inst.components.sanity.currentsanity ~= nil then

However even though it doesn't crash my game, It still doesn't work. And after putting print commands in my code to try to get a reference as to whether or not it's working, I never get the print. Also, sorry for not responding for a while had to go off.

EDIT: I figured out how to make it work a little different than I had hoped. It still works however once I get down in sanity the function doesn't stop printing a message to show me if it's working or not and I take double the damage still. This is my code:

 

local function bonus(inst)
	if inst.components.sanity.IsCrazy then
		print("Wraith is insane")
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
		inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
		inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end
end

local master_postinit = function(inst)

--All my codes, health, hunger, sanity ect.

inst:ListenForEvent("sanitydelta", bonus)
	return inst
end


 

 

 

 

 

Thanks, Luah.

 

Edited by Luahh
Knowledge
Link to comment
Share on other sites

4 hours ago, Luahh said:

Thanks, but even after adding that my game still crashes. However I managed to make it not crash by instead of using 


if inst ~= nil then

--I instead used this, and it did not crash

if inst.components.sanity.currentsanity ~= nil then

However even though it doesn't crash my game, It still doesn't work. And after putting print commands in my code to try to get a reference as to whether or not it's working, I never get the print. Also, sorry for not responding for a while had to go off.

EDIT: I figured out how to make it work a little different than I had hoped. It still works however once I get down in sanity the function doesn't stop printing a message to show me if it's working or not and I take double the damage still. This is my code:

 


local function bonus(inst)
	if inst.components.sanity.IsCrazy then
		print("Wraith is insane")
		inst.components.health.absorb = -1 --Take 100% more damage, which is double
		inst.components.combat.damagemultiplier = 2 --Deal double damage
	else
		inst.components.health.absorb = 0 --Take 0% more damage
		inst.components.combat.damagemultiplier = 1 --Deal normal damage
	end
end

local master_postinit = function(inst)

--All my codes, health, hunger, sanity ect.

inst:ListenForEvent("sanitydelta", bonus)
	return inst
end


 

 

 

 

 

Thanks, Luah.

 

This was my mistake. You can change it from...

 inst.components.sanity.currentsanity

to

 inst.components.sanity.current

 

Link to comment
Share on other sites

27 minutes ago, RedHairedHero said:

This was my mistake. You can change it from...


 inst.components.sanity.currentsanity

to


 inst.components.sanity.current

 

I tried this already and it wouldn't crash it just wouldn't work at all.

Sorry for all the trouble with this subject

Thanks, Luah.

Link to comment
Share on other sites

4 minutes ago, RedHairedHero said:

Can you share your prefab? That should be working as it works for me.

I can't right now as I can't get on my PC, but I will be sure to share it later.

Link to comment
Share on other sites

local function bonus(inst)
	print (inst) --This is to check what inst is. 
	--If the print keeps showing nil then uncomment the line below.
	--local inst = ThePlayer
	if inst ~= nil then
		if inst.components.sanity.current <= 10 then
			print("Wraith is insane")--This will trigger when your sanity is 10 or lower.
			inst.components.health.absorb = -1 --Take 100% more damage, which is double
			inst.components.combat.damagemultiplier = 2 --Deal double damage
		else
			print("Wraith is no longer insane") --This trigger when your sanity is 11 or more.
			inst.components.health.absorb = 0 --Take 0% more damage
			inst.components.combat.damagemultiplier = 1 --Deal normal damage
		end
	end
end

I changed the formatting you had for the bonus function from local bonus = function (inst) to local function bonus (inst).

I added a few comments in there, try to see what inst is with the print I included. If inst is always showing nil then you can use the local inst = ThePlayer. Other then that it should work.

Edited by RedHairedHero
Link to comment
Share on other sites

1 minute ago, AmIaPapaya said:

I just tested it and it works for me.

Is there any way you can transfer it to hunger? Lets say if your hunger is below 50% all the components activate? 

Should be, just look up the components for hunger, not sure about the sanitydelta bit though.
 

10 minutes ago, RedHairedHero said:

local function = bonus(inst)
	print (inst) --This is to check what inst is. 
	--If the print keeps showing nil then uncomment the line below.
	--local inst = ThePlayer
	if inst ~= nil then
		if inst.components.sanity.current <= 10 then
			print("Wraith is insane")--This will trigger when your sanity is 10 or lower.
			inst.components.health.absorb = -1 --Take 100% more damage, which is double
			inst.components.combat.damagemultiplier = 2 --Deal double damage
		else
			print("Wraith is no longer insane") --This trigger when your sanity is 11 or more.
			inst.components.health.absorb = 0 --Take 0% more damage
			inst.components.combat.damagemultiplier = 1 --Deal normal damage
		end
	end
end

I changed the formatting you had for the bonus function from local bonus = function (inst) to local function bonus (inst).

I added a few comments in there, try to see what inst is with the print I included. If inst is always showing nil then you can use the local inst = ThePlayer. Other then that it should work.

Also this does work I guess my testing method was just poor, I was using the Nightmare Amulet to simulate the effects of having 0 sanity but using it doesn't work. Thanks for all the help.
 

Link to comment
Share on other sites

5 minutes ago, AmIaPapaya said:

I just tested it and it works for me.

Is there any way you can transfer it to hunger? Lets say if your hunger is below 50% all the components activate? 

Yeah you can just change out anywhere it says sanity for hunger and it'll have the same effect.

Link to comment
Share on other sites

4 minutes ago, AmIaPapaya said:

Even "inst.components.sanity.IsCrazy"?

Would the "IsCrazy" change? Or would it be "IsHungery".

Try this

local function bonus(inst)
	if inst ~= nil then
		if inst.components.hunger.current <= 10 then
			inst.components.health.absorb = -1 --Take 100% more damage, which is double
			inst.components.combat.damagemultiplier = 2 --Deal double damage
		else
			inst.components.health.absorb = 0 --Take 0% more damage
			inst.components.combat.damagemultiplier = 1 --Deal normal damage
		end
	end
end

--and then put this at the bottom inside your master_postinit function
inst:ListenForEvent("hungerdelta", bonus)
return inst
Edited by Luahh
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...