Jump to content

Recommended Posts

Hey! I'm creating a character with the help of a friend, with these perks: 

> Gains Sanity near Spiders or Bats (+3.33/min)

> Loses Sanity if attack Spiders or Bats (-7.5 Sanity/attack)

 

I am extremely lost at what components to use so I can decrease his sanity when attacking those specific creatures and also gain sanity by being near them.

I tried using wathgrithr.onattack = OnAttack. And then check the tag one the victim but it's hitting me with an error.

 

I was testing without the tags just to see if it's decreasing his sanity, but it gives the same error.

local function OnAttack(inst, data)
    inst.components.sanity:DoDelta(-7.5)
end


local common_postinit = function(inst) 
	
	inst:AddTag("birdfriendly") -- Birds don't fly away
	inst.MiniMapEntity:SetIcon( "joaopedro.tex" )
end

local master_postinit = function(inst)
	inst.soundsname = "webber"
    inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	inst.components.health:SetMaxHealth(180)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(150)
	
    inst.components.combat.damagemultiplier = 1
	inst.components.hunger.hungerrate = 0.8 * TUNING.WILSON_HUNGER_RATE
	
	inst.components.eater:SetDiet({FOODGROUP.OMNI}, {FOODTYPE.VEGGIE})
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	
	inst.prefabs.wathgrithr.onattack = OnAttack -- This is the true line 80, that shows up on the error log

 

I haven't tried anything with the aura yet, cos I don't even know where to start without changing the spiders' and bats' prefabs.

 

I am a Game Dev student, but still very new to it. And I study C# and C++ mostly, so I'm not completely familiar with Lua, even though I can understand most of its structure.

Thanks in advance.

error.png

Edited by RedTarantula

Put this inside YOURCHAR.lua in the master_postinit to lose sanity when attack spiders or bats

Spoiler

inst:ListenForEvent("onattackother", function(inst, data)
	if data.target:HasTag("spider") or data.target:HasTag("bat") then
		inst.components.sanity:DoDelta(-7.5)
		--inst.components.talker:Say("No!")
	end
end)

 

put this inside modmain.lua do regain sanity around spiders or bats

Spoiler

if GLOBAL.TheNet:GetIsServer() then	
	
	AddComponentPostInit("sanityaura", function(self)
		local old = self.GetAura 
		function self:GetAura(observer) 
			if observer.prefab == "YOURCHARACTER" and self.inst:HasTag("spider") or self.inst:HasTag("bat") then
				return .1
			end
			return old(self,observer) 
		end
	end)	

end

 

hopefully it helps you :D!

 

On 31/12/2017 at 12:17 AM, SuperDavid said:

 

  Hide contents


if GLOBAL.TheNet:GetIsServer() then	
	
	AddComponentPostInit("sanityaura", function(self)
		local old = self.GetAura 
		function self:GetAura(observer) 
			if observer.prefab == "YOURCHARACTER" and self.inst:HasTag("spider") or self.inst:HasTag("bat") then
				return .1
			end
			return old(self,observer) 
		end
	end)	

end

 

hopefully it helps you :D!

2

Hey! It's been a while now, but I'm working on something else that requires that same code. However I found the aura range to be reeeeeally short. Any way of making it so the aura's range is wider. Way wider. I have to pretty much step on the creature to make the aura thing work. lmfao

1 hour ago, RedTarantula said:

Hey! It's been a while now, but I'm working on something else that requires that same code. However I found the aura range to be reeeeeally short. Any way of making it so the aura's range is wider. Way wider. I have to pretty much step on the creature to make the aura thing work. lmfao

I think you can increase the value from .1 to something higher for a bigger aura not really sure though

Edited by SuperDavid
8 hours ago, RedTarantula said:

 

Hey! It's been a while now, but I'm working on something else that requires that same code. However I found the aura range to be reeeeeally short. Any way of making it so the aura's range is wider. Way wider. I have to pretty much step on the creature to make the aura thing work. lmfao

Sanity auras use is a distance squared function with a cap of radius 1 and smaller being maximum effect.

To make it linear it's a bit of work to do.

There's example code and descriptions I've done here:

 

 

 

Also @SuperDavid's code is wrong.

if observer.prefab == "YOURCHARACTER" and self.inst:HasTag("spider") or self.inst:HasTag("bat") then

Should be:

if observer.prefab == "YOURCHARACTER" and (self.inst:HasTag("spider") or self.inst:HasTag("bat")) then

Otherwise the logic will short circuit and make every bat give sanity to everyone else.

Edited by CarlZalph
4 hours ago, CarlZalph said:

if observer.prefab == "YOURCHARACTER" and self.inst:HasTag("spider") or self.inst:HasTag("bat") then

Should be:


if observer.prefab == "YOURCHARACTER" and (self.inst:HasTag("spider") or self.inst:HasTag("bat")) then

Otherwise the logic will short circuit and make every bat give sanity to everyone else.

Oh didn't know that, thanks!

 

Btw does this mean when ever there is code which is using "or" it needs to be closed with () like

if inst and ( inst.components.burnable:IsBurning() or inst.components.burnable:IsSmoldering() ) then

if inst and ( inst.sg:HasStateTag("busy") or inst.sg:HasStateTag("working") or inst.sg:HasStateTag("doing") ) then

or does this only apply to certain things? Like when you're checking if " inst.prefab == "something" "

 

30 minutes ago, SuperDavid said:

Btw does this mean when ever there is code which is using "or" it needs to be closed with () like


if inst and ( inst.components.burnable:IsBurning() or inst.components.burnable:IsSmoldering() ) then

if inst and ( inst.sg:HasStateTag("busy") or inst.sg:HasStateTag("working") or inst.sg:HasStateTag("doing") ) then

or does this only apply to certain things? Like when you're checking if " inst.prefab == "something" "

 

If left without, then it acts like a ternary and each has their use.

<Ternary>

C: return (A != NULL)?A->thing:0

Lua: return A ~= nil and A.thing or 0

<Equiv. expression>

C: if(A != NULL) { return A->thing; } else { return 0; }

Lua: if A ~= nil then return A.thing else return 0 end

 

If you have the parenthesis, then it acts as a requirement for both things in the or portion of the statement.

For your code that I've quoted the or statements should all be in the parenthesis, because they all rely on inst being a value (not nil/false) to use.

Edited by CarlZalph
Tuple (list) -> Ternary (operator)

I keep updating my mod with more things and as ppl request new features. Last update I made some configurable things, like choosing how many pieces of beard you get by shaving on the first stage and that sort of stuff.

I wonder, however, if there is any chance of making the start inventory of a character configurable.

local start_inv =
{
    "item 1",
}

This is the default. I tried something like

if condition1 then
local start_inv =
{
    "item1",
}
elseif condition2 then
local start_inv =
{
    "item2",
}
end

but it didn't work.

 

Thanks for the help, in advance

EDIT: just fixed some grammar

Edited by RedTarantula
5 minutes ago, Aquaterion said:

you would probably want to do


local startinv = {}
if condition1 then
	startinv = { "item1" }
else
	startinv = { "item2" }
end

so the scope of startinv isn't in the if statement only

That worked! Thanks!

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