Jump to content

Damage and healing aura, hostile mob pacification [SOLVED]


Recommended Posts

I'm creating a set of high-cost magic light structures with various magical effects that only function at dusk/night. I'm an absolute beginner to coding, and it's mainly been a case of trial and error, peeking inside other people's mods/game files, and a smidge of common sense and "eureka!" moments. I learned how to use Spriter for this project, so I'm pretty proud of myself!

But now I've run into things I just can't seem to make sense of. I've managed to create prefabs with a sanity aura, heat and cold without too much issue, but now the other ideas are proving harder to make work. I really wish I could figure it all out for myself, but I don't think that's going to happen. Advice/help on any or all of them much appreciated. A copy of one of the prefabs is attached for reference.

Damage aura

I'm looking for the item to deal constant small but indiscriminate damage to any target (besides the player) within a small range. I've looked at Abigail's code extensively. I've managed to get a couple of code variations into the game that don't crash, but they also don't work. Mostly it's been crashes due to undeclared functions or nil values.

There was some talk of the sort of thing I'm after in this old thread, but not enough info to give me any direction as of what to try next.

Healing aura

An inverse of the above, a small aura of healing. I've looked at regen mods and the 'Occult Runes' mod, without much luck. I think in both cases I'm having trouble actually implementing the "aura" component and making it work? I don't know. I also tried making my own 'healingaura' component after seeing how sanityaura was handled, with no luck.

Hostile mob pacification

I'm looking at the code in the pinecone.lua about leif pacification and can't really make heads or tails about how to convert this into area of effect for a permanent structure. Ideally I'd like to end up with an item with a small effect range, that will make minor hostile mobs passive. Thinks spiders, frogs, angered pigs etc, though not bosses/giants.

Thank you in advance for any help or advice you can offer!

magiclantern_red.lua

Link to comment
Share on other sites

You probably didn't enable the Aura component: inst.components.aura:Enable(true). Keep in mind that Aura requires Combat, which requires Health.

For a one-time areal effect, you can use TheSim:FindEntities() to get all nearby creatures. To perform this regularly, use inst:DoPeriodicTask(). You can use this to deal damage or heal. e.g.

local function ArealHeal(inst)
	local pt = inst:GetPosition()
	-- in the following, 4 is the radius (equals one ground tile)
	for _, ent in pairs( TheSim:FindEntities(pt.x, pt.y, pt.z, 4, {"hascombatcomponent"}) do
		-- You can make sure the entity is valid and meets your criteria here
		if ent:IsValid() and inst.components.health then
			inst.components.health:DoDelta(10)
		end
	end
end

Pacifying mobs could be done from the custom prefab, but it is easier to tag the prefab something special (e.g. "peaceaura") and check if there's any of those near the creature from within the creature's combat component. You can edit components from modmain.lua using AddComponentPostInit().

Link to comment
Share on other sites

@Mobbstar Excuse me if I'm being absolutely dense, but I'm not sure how to implement the code example you offered.

I've got this:

local function AreaHeal(inst)
	local pt = inst:GetPosition()
	-- in the following, 4 is the radius (equals one ground tile)
	for _, ent in pairs (TheSim:FindEntities(pt.x, pt.y, pt.z, 4, {"hascombatcomponent"})) do
		-- You can make sure the entity is valid and meets your criteria here
		if ent:IsValid() and inst.components.health then
			inst.components.health:DoDelta(10)
		end
	end
end

And this in the local function fn:

if not GetClock():IsDay() then
	inst:DoPeriodicTask(2, function() AreaHeal(inst) end)
	end

Which goes into the game without a crash, but doesn't work. Where am I going wrong?

Link to comment
Share on other sites

24 minutes ago, justjasper said:

Where am I going wrong?

The "if not GetClock():IsDay() then" is the fault, because that checks once, when loading. Move the line between "local function ArealHeal" and "local pt =" (and move the "end" too, of course). That way, it checks every time the prefab tries to heal.

Link to comment
Share on other sites

16 hours ago, Mobbstar said:

The "if not GetClock():IsDay() then" is the fault, because that checks once, when loading. Move the line between "local function ArealHeal" and "local pt =" (and move the "end" too, of course). That way, it checks every time the prefab tries to heal.

Do I need to change the "for _,"? I've seen in other instances of FindEntities used it has a letter value (eg in RoG's firesuppressor), paired with an index = _.

Link to comment
Share on other sites

1 hour ago, justjasper said:

Do I need to change the "for _,"? I've seen in other instances of FindEntities used it has a letter value (eg in RoG's firesuppressor), paired with an index = _.

for k, v in ... uses "k" and "v" as variables (that's names given to certain values, the variable can be anything, including "_", but it has to be unique in the context). There's no real problem in this case, I don't think there is.

Link to comment
Share on other sites

On 19/10/2016 at 1:24 PM, Mobbstar said:

for k, v in ... uses "k" and "v" as variables (that's names given to certain values, the variable can be anything, including "_", but it has to be unique in the context). There's no real problem in this case, I don't think there is.

No joy. Sorry to be a pain, but I'm attaching the prefab file I'm working with, and hoping if you're willing to take a look that it'll just be something relatively simple I'm overlooking in my lack of experience with this sort of coding. I cannot stress enough how much of a 'smash things together and see if things work out' type deal this is on my end.

magiclantern_purple.lua

EDIT: @Mobbstar

I managed to get the peace aura working, and from there I worked out how to do the health/damage variant. Your guidance was really helpful! This is the code I ended up using:

local function AreaHurt (inst) 
	if not GetClock():IsDay() then
	local pt = inst:GetPosition()
  	--healing
	local ents = TheSim:FindEntities(pt.x,pt.y,pt.z, 6, {"hascombatcomponent"}, {"ghost", "shadow"}, nil)
	
	for k,v in pairs(ents) do
		
	if v.components.health then
				v.components.health:DoDelta(-2)
			end
			end
end 
end

I'm not sure it's the most elegant/efficient coding, but it works! Thank you again for your help.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...