Jump to content

Recommended Posts

Hello everyone, i'll make my topic short.

I want to implement sanity to myitem.lua, specifically gaining sanity from killing anything BUT small creatures (rabbits, butterflies...), how do i do this? What's the code for that? (also need that the sanity amount is changeable, like 2 from every kill or 4). Please if i need to change anything in the code, like adding my ítems name on it, tell me, i'm a big noob.

The second thing is probably really easy, but i can't get it to work. I need to lose sanity while having the ítem equiped. So far i've tryed a lot in the fn part of the lua, like:

 inst:AddComponent("dapperness")
    inst.components.dapperness.dapperness = TUNING.CRAZINESS_SMALL

or

 inst:AddComponent("dapperness")
 inst.components.sanity.dapperness_mult = 16

or

 inst:AddComponent("dapperness")
 inst.components.dapperness.dapperness = 16

But it doesn't work, either i get a crash when trying to open the game with the ítem mod activated using this codes, or the game and mod Works but no sanity drain.

Thanks in advance, i'd been trying this for a couple of days and i still have no succes.

Edited by Jpianist
Got the help i needed

If you want to lose sanity while equipped with a tool this is the code for it

"inst.components.equippable.dapperness = TUNING.CRAZINESS_MED"

 

Make sure to have all the things equippable needs like.

local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("", "", "")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

local function onunequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
end

inst:AddComponent("equippable")
inst.components.equippable:SetOnEquip(onequip)
inst.components.equippable:SetOnUnequip(onunequip)

For real just look in the nightsword.lua that should help you with some stuff, sorry I wasn't really helpful :?.

Edited by SuperDavid
32 minutes ago, SuperDavid said:

If you want to lose sanity while equipped with a tool this is the code for it

"inst.components.equippable.dapperness = TUNING.CRAZINESS_MED"

Thanks for taking some time to help me, i appreciate it. I just tryed the code but i get this message:

attempt to index field 'equippable' (a nil value)

Got to clarify that my ítem Works perfectly, this happends if i add the code for the sanity drain.

Also with other codes i've got the same message but changing equippable to sanity:

attempt to index field 'sanity' (a nil value)

You know if it be much easier for people to help you if you post your weapon's code right here because this " attempt to index field 'equippable' (a nil value) " I think that means your weapon isn't a equippable, I think...

@Jpianist That code needs to be after you add the equippable component. Which you have to add in order for it to be equippable.

As for gaining sanity when things die, do you want it to be sanity gained specifically when things are killed with this item, or just sanity gained when things nearby die while the item is equipped?

 

32 minutes ago, rezecib said:

@Jpianist That code needs to be after you add the equippable component. Which you have to add in order for it to be equippable.

As for gaining sanity when things die, do you want it to be sanity gained specifically when things are killed with this item, or just sanity gained when things nearby die while the item is equipped?

 

Omg that explains a lot, i'm using this before the equippable component, which i do have:

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )

So now i'm gonna try this:

    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquip )
    inst.components.equippable:SetOnUnequip( OnUnequip )
 inst.components.equippable.walkspeedmult = 1.6
 inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(50)
 inst.components.weapon.blinking = true
 inst.components.weapon:SetRange(7, 2)
 inst.components.weapon.onattack = onattack
 
 --Sanity drain codes
 inst:AddComponent("dapperness")
 inst.components.equippable.dapperness = TUNING.CRAZINESS_MED
 -----
 
How does it look?
About your question, i wanna gain sanity when killing things with the item, not if they happend to die nearby.
 
I'm gonna try adding this sanity code after the component right now, this could be the answer to 3 days of failing hahaha. Thanks!
 
EDIT: OMG it worked! All i needed was to use the code AFTER the equippable component, i told you when i made the topic that it was probably a really easy thing to do, i knew it was something really simple, now i feel ashamed hahaha but greatful for having you guys helping me. Is it possible to configure the sanity droping with numbers? Like say, losing 0.5 every second, or idk 20 sanity for minute (havent yet decided the exact number, would depend on what happends with the killing sanity gain code).
Edited by Jpianist

@Jpianist There is no dapperness component in DST, so don't add that.

For sanity gained when killing with the item:

local function onentitydeath(world, data)
	if data.inst and not data.inst:HasTag("prey") and not data.inst:HasTag("veggie")
	and not data.inst:HasTag("structure") and not data.inst:HasTag("butterfly") then
		--"prey" will rule out bunnies, birds, etc; "veggie" rules out eyeplants from lureplants, "structure" rules out houndius shootius I think
		if data.afflicter and data.afflicter.components.sanity and data.cause and data.cause.prefab == "thisweaponsprefabname" then
			--afflicter should be the player that killed it; cause should be either the player (if punching) or the weapon
			data.afflicter.components.sanity:DoDelta(2) --you might want to scale this value by the mob? like maybe health
			--to scale by health instead, for example:
			data.afflicter.components.sanity:DoDelta(math.clamp(data.inst.components.health.maxhealth/100, 2, 4))
			--arguments for math.clamp are (suggested_amount, min_amount, max_amount)
		end
	end
end

local function onequip(inst, owner)
	--the code you already have
	inst:ListenForEvent("entity_death", onentitydeath, TheWorld)
end

local function onunequip(inst, owner)
	--the code you already have
	inst:RemoveEventCallback("entity_death", onentitydeath, TheWorld)
end

 

Edited by rezecib
added exception for butterfly

@rezecib That code looks amazing man, i will definitely mention you in the mod when i get it done, i'm really thankful for your help.

First thing i'll do is delete "inst:AddComponent("dapperness")" because DST doesn't have dapperness component, but ofc i have to say that after moving the code to where you told me now i do have sanity drain when equipping the ítem, which i'm really thankful for.

As for this amazing code, let me see if i'm on the same page. Afflicter should be replaced by "mycharname", and ofc "thisweaponsprefabname" is the name of the weapon i'm coding.

And to scale by health, the math number means: /100, 2, 4   First the Hp of the mob to take in count for the minimum, then the mínimun sanity i get for said HP number, and then the max sanity i can get. Right? Also this means that a mob can give no sanity based on the minimun number i put there?

7 minutes ago, Jpianist said:

As for this amazing code, let me see if i'm on the same page. Afflicter should be replaced by "mycharname", and ofc "thisweaponsprefabname" is the name of the weapon i'm coding.

Exactly.

7 minutes ago, Jpianist said:

And to scale by health, the math number means: /100, 2, 4   First the Hp of the mob to take in count for the minimum, then the mínimun sanity i get for said HP number, and then the max sanity i can get. Right? Also this means that a mob can give no sanity based on the minimun number i put there?

Not sure if you got it from that, but it's HP/100, then make it at least 2 and no more than 4. So 100 hp = 2 sanity, 200 hp = 2 sanity, 300 hp = 3 sanity, 400 hp = 4 sanity, 500 hp = 4 sanity, etc.

@rezecib Allright so i tested the code, killed some spiders, pigs and a tailbird, but none of them seemed to give me sanity.

For testing purposes i added the code like this into myweapon.lua: (i'll actually use the scale by health math code, but for now testing with DoDelta(4))

local function onentitydeath(world, data)
	if data.inst and not data.inst:HasTag("prey") and not data.inst:HasTag("veggie")
	and not data.inst:HasTag("structure") and not data.inst:HasTag("butterfly") then
		if data.mycharname and data.mycharname.components.sanity and data.cause and data.cause.prefab == "myweaponname" then
			data.mycharname.components.sanity:DoDelta(4)
		end
	end
end

Then:

local function fn(colour)

    local function OnEquip(inst, owner) 
        owner.AnimState:OverrideSymbol("swap_object", "swap_chaosrevenge", "swap_chaosrevenge")
        owner.AnimState:Show("ARM_carry") 
        owner.AnimState:Hide("ARM_normal")
        health_drop(inst)	
		inst:ListenForEvent("entity_death", onentitydeath, TheWorld)
    end

    local function OnUnequip(inst, owner) 
        owner.AnimState:Hide("ARM_carry") 
        owner.AnimState:Show("ARM_normal")
		health_stop(inst)
		inst:RemoveEventCallback("entity_death", onentitydeath, TheWorld)
    end

But it doesn't give me sanity when i tested it by killing spiders and other mobs.

4 hours ago, rezecib said:

This should be data.afflicter.

I though you said i had to replace it with mycharname Look:

16 hours ago, Jpianist said:

As for this amazing code, let me see if i'm on the same page. Afflicter should be replaced by "mycharname", and ofc "thisweaponsprefabname" is the name of the weapon i'm coding.

 

16 hours ago, rezecib said:

Exactly.

So the code should be like this?

local function onentitydeath(world, data)
	if data.inst and not data.inst:HasTag("prey") and not data.inst:HasTag("veggie")
	and not data.inst:HasTag("structure") and not data.inst:HasTag("butterfly") then
		if data.afflicter and data.afflicter.components.sanity and data.cause and data.cause.prefab == "myweaponname" then
			data.mycharname.components.sanity:DoDelta(4)
		end
	end
end

 

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