Question regarding Monster:getHealth()

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
SeyProdumen
Posts: 18
Joined: Fri Jul 26, 2013 7:00 pm
Location: Norway
Contact:

Question regarding Monster:getHealth()

Post by SeyProdumen »

Hello everyone,

I'm fairly new to scripting and I'm currently experimenting with creating a monster bossfight.
I understand that you can get a monster's HP through Monster:getHealth(). However, I'd like to create a script along the following lines:

Code: Select all

findEntity("monster")
if monster:getHealth("< 50") then
monster:destroy()
Fairly simple, but it doesn't seem to work, as getHealth() only registers the exact amount of HP, or does it?
I read that LUA does know the >/< symbols. Do I need to create a hook for this purpose?

Thanks in advance.
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Question regarding Monster:getHealth()

Post by JohnWordsworth »

Just a few slight tweaks with regards to the structure of Lua are required. I'll post a version that should work, and then explain!

Code: Select all

local monster = findEntity("monster");
if monster:getHealth() < 50 then
  monster:destroy();
end
So, first of all, you find the monster entity. I think if your monster is always going to have the ID monster, you can actually skip that line, as typing 'monster:getHealth()' tells the engine to look for an entity with that ID anyway.

Then you want to call the getHealth() method on the monster, (monster:getHealth()) which returns a number. You can compare this number to the value 50 in the if statement (if value < 50 then). Every if then also requires an end somewhere to balance it out too. This tells lua when to return to code to always run regardless of the if branch.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
SeyProdumen
Posts: 18
Joined: Fri Jul 26, 2013 7:00 pm
Location: Norway
Contact:

Re: Question regarding Monster:getHealth()

Post by SeyProdumen »

Thank you for your quick answer!

I'm not sure I got it right though. The player is supposed to beat a strong monster by inserting torches into torch holders. Once this is completed, the monster should be destroyed and a number of other things happen, such as a door opening.
However there is the chance that a player just chews through the monster's endless hitpoints, in which case the door would not open, et cetera.
What I'm trying to do now is make sure that the monster is destroyed once a certain amount of damage is dealt.
It could not make it work with the script you provided, but I'm fairly sure the mistake is on my side.

However I'm beginning to realize that the lack of XP at the end of the fight would make it very unrewarding anyway, and I should definitely look into hooks... :D
Ryeath_Greystalk
Posts: 366
Joined: Tue Jan 15, 2013 3:26 am
Location: Oregon

Re: Question regarding Monster:getHealth()

Post by Ryeath_Greystalk »

SeyProdumen wrote:Thank you for your quick answer!

I'm not sure I got it right though. The player is supposed to beat a strong monster by inserting torches into torch holders. Once this is completed, the monster should be destroyed and a number of other things happen, such as a door opening.
However there is the chance that a player just chews through the monster's endless hitpoints, in which case the door would not open, et cetera.
What I'm trying to do now is make sure that the monster is destroyed once a certain amount of damage is dealt.
It could not make it work with the script you provided, but I'm fairly sure the mistake is on my side.

However I'm beginning to realize that the lack of XP at the end of the fight would make it very unrewarding anyway, and I should definitely look into hooks... :D
One option come to mind. Make the monster immune to damage by using an onDamage hook and return false. That way the player can beat on the monster all day long and it will not die. Then when the player places a torch in the holder you can reduce the monsters health through scripting, or quite possibly not even messing with the monsters health, just leave him at full health until the torches are complete and then destroy him.
If you want to make the torches damage the monster then you may need to differentiate between player caused damage and scripted torch damage in your onDamage hook, which I haven't looked into the possibility of that yet.
As for experience you can always just give the players experience when the torches are completed.
User avatar
msyblade
Posts: 792
Joined: Fri Oct 12, 2012 4:40 am
Location: New Mexico, USA
Contact:

Re: Question regarding Monster:getHealth()

Post by msyblade »

here is a simple Exp script for you ;)

Code: Select all

function exp() 
 hudPrint ("Gained 250 exp.") 
for i=1,4 do  
party:getChampion(i):gainExp(250)    
	end
end
And to do ryaeth's solution, connect the torchholders to a counter set to how many torch's need placed total. you may want to set the torchholder to decrement the counter on activation (torch placed) and increment the counter on deactivation (torch removed) so the player cannot just place and remove one torch on all of the holders. Then the counter connects to the script entity and activates when it reaches 0. I can also give you a script that does not allow torches to be removed at all if you like :)
Hope this all helps, and please don't hesitate to keep us updated on your progress or ask questions if any problems arise ;)
Currently conspiring with many modders on the "Legends of the Northern Realms"project.

"You have been captured by a psychopathic diety who needs a new plaything to torture."
Hotel Hades
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Question regarding Monster:getHealth()

Post by bongobeat »

msyblade wrote:here is a simple Exp script for you ;)

Code: Select all

function exp() 
 hudPrint ("Gained 250 exp.") 
for i=1,4 do  
party:getChampion(i):gainExp(250)    
	end
end
And to do ryaeth's solution, connect the torchholders to a counter set to how many torch's need placed total. you may want to set the torchholder to decrement the counter on activation (torch placed) and increment the counter on deactivation (torch removed) so the player cannot just place and remove one torch on all of the holders. Then the counter connects to the script entity and activates when it reaches 0. I can also give you a script that does not allow torches to be removed at all if you like :)
Hope this all helps, and please don't hesitate to keep us updated on your progress or ask questions if any problems arise ;)
Hi there,

I think this a nothing to do with this post, but as I ve see this exp script:
do you know a way with a script to get xp from killing any monster with a special weapon?

I ve created a custom weapon who spawn a custom spell, but it dont gives any xp when killing a monster with it.
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
SeyProdumen
Posts: 18
Joined: Fri Jul 26, 2013 7:00 pm
Location: Norway
Contact:

Re: Question regarding Monster:getHealth()

Post by SeyProdumen »

Thank you for all your answers! I read them just now as I was on holiday in the past 2 weeks. I haven't decided yet on how I'll do the boss fight in the end, but I'll have some more questions for sure. :mrgreen:
Post Reply