Page 1 of 4

Check if spawned monster is dead [Solved]

Posted: Fri Oct 31, 2014 12:18 am
by Jouki
I was trying to check if the monster spawned by spawner is dead but with no success, can anyone help? :cry:

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 5:25 am
by Lark
It looks like a spawned entity still exists for a few seconds, at least, after it is killed. You can probably put a hook into a cloned monster using onDie or something, but if you don't mind waiting a few seconds (according to my testing anyway), you can just use the findEntity method. This script demonstrates what I found:

Code: Select all

function spawnIt(c)
  c = c.go
  if not findEntity("turtle_guy") then
    spawn("turtle", c.level, c.x, c.y + 1, c.facing, c.elevation, "turtle_guy")
    delayedCall(self.go.id, 1, "checkIt")
    end
  end

function checkIt()
  if not findEntity("turtle_guy") then
    hudPrint("Your foe is no more!")
  else
    delayedCall(self.go.id, 1, "checkIt")
    end
  end
Attach a pressure plate to the spawnIt function (making sure there is a space to the south as per the c.y + 1 code). It will spawn a turtle if one hasn't already been spawned and it runs the checkIt function every second to see if the creature still exists. In my testing, the hudPrint triggers about 3 - 4 seconds after the turtle is killed. Now, this may be because it is actually destroyed or because the garbage collection got it. You'll just have to play with this idea to see if it meets your needs or not. If not, the cloning monster approach should work... not that I've tried it yet.

I hope this helps, -Lark

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 9:48 am
by Jouki
I'd say this delay is because editor has a long tick before next update. hmm actaully this could be fatal for me, thank you though it's a good start. I was thinking about doing via bossfight but without music and health bar but idk how to remove health bar.

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 9:49 am
by Doridion
Mouahahaha Lark, since modders seen that delayedCall function is here, everybody use it :p Gratz and thanks for help ;)

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 11:17 am
by antti
Jouki wrote:I'd say this delay is because editor has a long tick before next update.
Editor updates every frame as usual. The object needs to exist as long as it does anything, including the death sound and particles.

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 1:45 pm
by Jouki
antti wrote:
Jouki wrote:I'd say this delay is because editor has a long tick before next update.
Editor updates every frame as usual. The object needs to exist as long as it does anything, including the death sound and particles.
actaully I can see when I spawn him in editor (I dont know how to say specify spawner with other component features as handlight 1 and handlight2 or summoning undead etc... at skeleton commander for example) there's a method "isAlive()" that returns boolean and I guess it was immediate. (not sure)

edit:

ok I figured out a little bit but it's not completed. I need fix the condition when I start checking before I spawn the turtle. variable "yourFoe" is not declared in that situation. How could I declare this variable (instead of using counter)

Code: Select all

function spawnIt(c)
  c = c.go
  if not findEntity("turtle_guy") then
    yourFoe = spawn("turtle", c.level, c.x, c.y + 1, c.facing, c.elevation, "turtle_guy")
    end
  end

function checkIt()
  if yourFoe.monster:isAlive() ~= true then
    hudPrint("Your foe is no more!")
  else  end
end
by the way maybe it would be easier use the specify spawner just removing some of components but there's one problem. when I do this on undead their animation of spawning starts notwithstanding what I deactivate in components. It wouldnt be fatal for me but idk how to re-launch this animation of spawning undead :D

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 3:21 pm
by Prozail
i would do something like this instead.

Code: Select all


function spawnIt(c)
  c = c.go
  if not findEntity("turtle_guy") then
    yourFoe = spawn("turtle", c.level, c.x, c.y + 1, c.facing, c.elevation, "turtle_guy")

-- add this line to catch the "onDie" event on the spawned monster
	yourFoe.monster:addConnector("onDie",self.go.id,"nowimdead")

    end
  end

-- now this function gets alot simpler. 
function nowimdead()
	hudPrint("Your foe is no more!")
end


Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 3:36 pm
by Lark
Thanks Prozail! I knew onDie should work, but I just hadn't had time to try it yet. This is a much better solution! Thanks again, -Lark

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 3:51 pm
by Jouki
Prozail wrote:i would do something like this instead.

Code: Select all


function spawnIt(c)
  c = c.go
  if not findEntity("turtle_guy") then
    yourFoe = spawn("turtle", c.level, c.x, c.y + 1, c.facing, c.elevation, "turtle_guy")

-- add this line to catch the "onDie" event on the spawned monster
	yourFoe.monster:addConnector("onDie",self.go.id,"nowimdead")

    end
  end

-- now this function gets alot simpler. 
function nowimdead()
	hudPrint("Your foe is no more!")
end

perfect that's exactly what I need :P

Re: Check if spawned monster is dead

Posted: Fri Oct 31, 2014 5:13 pm
by Jouki
ok I hope I have the last question. What does exactly mean c=c.go and why function has this variable in parameter? does the pressure plate send any parameter?

edit: and wth?!

Code: Select all

  enemySkeleton1 = spawn("skeleton_trooper", c.level, c.x + 1, c.y, c.facing + 2, c.elevation, "skeletonTroop")
		enemySkeleton2 = spawn("skeleton_trooper_pair", c.level, c.x + 2, c.y + 2, c.facing + 2, c.elevation, "skeletonPair")

		enemySkeleton1.monster:addConnector("onDie",self.go.id,"deadCheckerSkeleton")
		enemySkeleton2.monster:addConnector("onDie",self.go.id,"deadCheckerSkeleton")
why is this not working but this yeah

Code: Select all

  enemySkeleton1 = spawn("skeleton_trooper", c.level, c.x + 1, c.y, c.facing + 2, c.elevation, "skeletonTroop")
		enemySkeleton2 = spawn("skeleton_trooper", c.level, c.x + 2, c.y + 2, c.facing + 2, c.elevation, "skeletonPair")

		enemySkeleton1.monster:addConnector("onDie",self.go.id,"deadCheckerSkeleton")
		enemySkeleton2.monster:addConnector("onDie",self.go.id,"deadCheckerSkeleton")
Only change is the second one is not pair but classic trooper. I guess it's about "pair" monsters are not "monster" but something else, but what...

ok I'm starting to think I'm the dumbest modder ever xD problem was pair is "monstergroup"