Cleanest method of making monster attack that damage both player and the executing monster

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Hey all,

As the title tells:

I want to make a monster attack that upon reaching a player(melee distance) executing the attack will damage player and explode the executing monster(kill itself).

In other words, suicide bombing monster.
Damage contacting player, Kill itself.
Any ideas on how to make it in the cleanest way?
Thanks.
 

Nekiro

Support Team
Support Team
Joined
Sep 7, 2015
Messages
1,872
Best answers
81
Reaction score
653
spell or onthink, checking distance between target and self (monster) if <= 1 delete monster and damage target or if should damage in radius use getSpectators with monster position or what I would personally use is monster target list.
 
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Can you translate it into a lua?
I'm not sure how to both inflict the damage to the players/s and delete the monster itself.
thanks!
 

Nekiro

Support Team
Support Team
Joined
Sep 7, 2015
Messages
1,872
Best answers
81
Reaction score
653
Something like that.

Lua:
-- used in onThink registered to monster
local target = creature:getTarget()
if not target then
    return false
end

local distance = creature:getPosition():getDistance(target:getPosition())
if distance > 1 then
    return false
end

local targets = creature:getTargetList()
if not targets or targets and #targets == 0 then
    return false
end

for _, target in ipairs(targets) do
    doTargetCombatHealth(creature:getId(), target, type etc...)
end

creature:remove()
 
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Wow thanks pal,

I'm trying to understand the target and targetlist thing.
as for the part
"
for _, target in ipairs(targets) do
doTargetCombatHealth(creature:getId(), target, type etc...)
end
"

Sorry for the noob question but what does the _, after the start of for loop stands for
also, DoTargetCombatHealth suppouse to deduct hp from the player in a negative value? not sure about what "type" will be attached for or other variables of the function?
 

Nekiro

Support Team
Support Team
Joined
Sep 7, 2015
Messages
1,872
Best answers
81
Reaction score
653
Look up the function parameters in luascript.cpp when it comes to dotargetcombathealth and yes you should supply negative values.

_ is just "garbage", table has index and the value, in this scenario _ is index, but I dont use it, so I just supplied some "garbage" variable for it as I dont need it.
 
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Hello and thanks again,

got these errors when trying to load:

data/spells/scripts/monster/suicide.lua:2: attempt to index global 'creature' (a nil value)
stack traceback:
[C]: in function '__index'
data/spells/scripts/monster/suicide.lua:2: in main chunk
[Warning - Event::checkScript] Can not load script: scripts/monster/suicide.lua


Code is as follows:

Code:
-- used in onThink registered to monster

local target = creature:getTarget()

if not target then

    return false

end



local distance = creature:getPosition():getDistance(target:getPosition())

if distance > 1 then

    return false

end



local targets = creature:getTargetList()

if not targets or targets and #targets == 0 then

    return false

end



for _, target in ipairs(targets) do

    doTargetCombatHealth(creature:getId(), target, COMBAT_FIREDAMAGE, -33, -99, CONST_ME_EXPLOSIONAREA)



end



creature:remove()
Edit: tried to wrap th function with "function onThink(creature, interval)" and i got this server error:
[Warning - Event::checkScript] Event onCastSpell not found. scripts/monster/suicide.lua


ALSO,
Do I have to call the LUA via attacks on the monster's XML or since it's onThink it's called somwhere else?
for example:
<attacks>
<attack name="suicide" interval="4000" chance="100"/>
</attacks>
 
Last edited:
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Final function works like this!
Code:
-- used in onThink registered to monster
function onThink(creature, interval)


local target = creature:getTarget()
if not target then
    return false
end

function onCastSpell(creature, var)
    return combat:execute(creature, var)
end

local distance = creature:getPosition():getDistance(target:getPosition())
if distance > 1 then
    return false
end

local targets = creature:getTargetList()
if not targets or targets and #targets == 0 then
    return false
end

for _, target in ipairs(targets) do
    doTargetCombatHealth(creature:getId(), target, COMBAT_FIREDAMAGE, -33, -99, CONST_ME_EXPLOSIONAREA)

end

creature:remove()

end
Added the function to creaturescripts.xml like this:
Code:
  <event type="think" name="TestScript" script="others/testscript1.lua" />
Afterward, calling it from creaturescripts instead of spell/attack like this from monster's XML :
Code:
<script>
<event name="TestScript"/>
</script>
Nekiro, you are a true hero!
 
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Hey Nekiro, Still here? :)

Is there a way that "creature:remove()" won't be activated upon killing the monster before he gets to "kill itself"?
Meaning that killing it before he executes will show the monster body etc instead of removing it?
Thanks.
 

Nekiro

Support Team
Support Team
Joined
Sep 7, 2015
Messages
1,872
Best answers
81
Reaction score
653
kill the monster, remove his health, creature:addHealth(-creature:getHealth())
 
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
Is there a way to implement creature delete upon "suicide" and creature kill upon kill?
Not sure where to put the creature:addHealth(-creature:getHealth()), since killing the monster normally(0 health) deletes it as well without body.
 

Nekiro

Support Team
Support Team
Joined
Sep 7, 2015
Messages
1,872
Best answers
81
Reaction score
653
Is there a way to implement creature delete upon "suicide" and creature kill upon kill?
Not sure where to put the creature:addHealth(-creature:getHealth()), since killing the monster normally(0 health) deletes it as well without body.
What, your tfs must be broken, or the monster has no corpse, setting monster hp to 0 kills it.
 
OP
D

DarkLua

New Member
Joined
Apr 29, 2019
Messages
11
Best answers
0
Reaction score
1
It was an otb item update issue, Fixed without modifying anything :)
 
Top