Check Monster Dead? Help!

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Check Monster Dead? Help!

Post by Mysterious »

Hello all.

I have tried a few methods of finding out if a Monster is dead or not, but have failed badly :( I don't know how to do the Coding for this at all as I have discovered.

What I need is to find out if eg: forest_ogre_1 is dead, if he is then it runs a script function. If he is still alive then it would run a different script function.

I basically want to use this code for a couple of quests. Any help on this please, thank you.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Check Monster Dead? Help!

Post by minmay »

MonsterComponent:isAlive() returns true if the monster is alive, and false if the monster is dead.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Check Monster Dead? Help!

Post by Isaac »

Mysterious wrote:What I need is to find out if eg: forest_ogre_1 is dead, if he is then it runs a script function. If he is still alive then it would run a different script function.
Just as minmay said above; the general solution.

In your case, for your monster, it should probably be the following:

Code: Select all

function deadCheck()
  if findEntity("forest_ogre_1") and forest_ogre_1.monster and not forest_ogre_1.monster:isAlive() then
     print("forest_ogre_1 is dead!")
    else
     print("forest_ogre_1 is alive!")
   end
end
SpoilerShow
Alternatively:

Code: Select all

function deadCheck(monster_id)
 local target = findEntity(monster_id)
  if target ~= nil then
     if target.monster and not target.monster:isAlive() then
       print(monster_id.." is dead!")
      else
       print(monster_id.." is alive!")
       return false --not dead
     end
  end
 return true --dead	
end

script_entity_1.script.deadCheck("zarchton_1")
*Edited to swap target for findEntity
Last edited by Isaac on Tue Apr 19, 2016 7:34 am, edited 5 times in total.
User avatar
Eleven Warrior
Posts: 752
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

Re: Check Monster Dead? Help!

Post by Eleven Warrior »

Thxs matey this is cool I was looking for something like this the other day. Thxs :)
User avatar
Zo Kath Ra
Posts: 944
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Check Monster Dead? Help!

Post by Zo Kath Ra »

How about this:

Code: Select all

function monsterIsAlive(monster_id)
	local entity = findEntity(monster_id)
	
	if (entity) and (entity.monster) and (entity.monster:isAlive()) then
		return true
	else
		return false
	end
end
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Check Monster Dead? Help!

Post by THOM »

I don't understand, why you all are asking for the monster-component? And the isAlive could be important if the monster was killed a few seconds before the query of the script. But isn't it in most cases enough to ask simply

if ogre_1 ~= nil then :?:
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Check Monster Dead? Help!

Post by minmay »

No. The parent object hangs around for a few seconds after the monster is killed, to handle the death particle effect and so on. You must use MonsterComponent:isAlive().
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Post Reply