Skip to content

How can I prevent monsters from being attacked in scripts?

User Avatar
#7
Auto-translated
Jack_of_shadows
From the manual in my signature:

How to execute an action when touching a specific monster?

As a rule, it is better to make a monster with non-standard behavior visually different from a
normal one, so as a bonus to the touch trigger, I add a change of the cursor from the
standard sword to a peaceful horse, removing the monster's "skirt" and changing its
name:
-- setting up creatures-NPCs on the map
-- npc – the name of the monster assigned in the editor (string)
-- func – the touch handler function (string)
-- name – the name of the file with the creature's name (string)
-- func and name – optional parameters, they can not be passed
function SetNpcFunc(npc, func, name)
-- disabling the standard touch trigger of the monster (battle)
SetObjectEnabled(npc, nil);
-- setting a peaceful cursor when hovering over the monster
SetDisabledObjectMode(npc, DISABLED_INTERACT);
sleep(1)
-- disabling the "skirt" (faction symbol under the monster)
SetMonsterSelectionType(npc, 0);
-- setting your own touch trigger (optional parameter)
if (func ~= nil) then
Trigger(OBJECT_TOUCH_TRIGGER, npc, func);
end
-- setting the monster's name (optional parameter)
if (name ~= nil) then
local full_name = GetMapDataPath()..name;
SetMonsterNames(npc, MONSTER_NAME_SINGLE, full_name);
end
end

-- examples of using the function:
-- the monster simply stops reacting to touches
SetNpcFunc(‘SCRIPT_NAME_OF_THE_MONSTER’);
-- a touch trigger is assigned to the monster, the name remains the same
SetNpcFunc(‘SCRIPT_NAME_OF_THE_MONSTER’, ‘NpcTouch’);
-- a touch trigger is assigned to the monster and a new name is given
SetNpcFunc(‘SCRIPT_NAME_OF_THE_MONSTER’, ‘NpcTouch’,
‘FILE_NAME_WITH_NAME’);

-- monster touch handler
-- hero – the script name of the hero who touched the NPC monster
-- npc – the script name of the monster that was touched
function NpcTouch(hero, npc)
YOUR CODE
end

Thank you very much, this is very useful information.