Page 1 of 2

Monsters and Scripting

Posted: Wed Oct 24, 2012 12:59 pm
by Ahmyo
Hello, I'm trying to use lua to script a room which has doors that will only open after a monster is killed, as there is no connecter option for monsters from Inspector, or anywhere else that I know of (future feature?) to make utilizing the death of a monster have an effect on other entities such as doors. I've put together my own code by piecing examples together, but I'm not knowledgeable enough in lua to know what I'm doing and need help. If anyone is willing to help me or provide some resources for monsters and scripting I would really appreciate it! :mrgreen:

Re: Monsters and Scripting

Posted: Wed Oct 24, 2012 1:23 pm
by Grimwold
There are a couple of good discussions about trigger effects when monsters die that you might find useful:
viewtopic.php?f=14&t=3239
viewtopic.php?f=14&t=3784#p38693

I've done some work in this area, so would be happy to answer any questions you have. My solutions have involved creating a clone of an existing monster and adding the onDie command to its definition.

Re: Monsters and Scripting

Posted: Wed Oct 24, 2012 1:34 pm
by Neikun
You will have to create a new monster that uses an onDie hook.
I have one, myself.

Code: Select all

cloneObject{
	name = "triggersnail",
	baseObject = "snail",
	onDie = function(self)
       	ballspawner1: activate()
	playSound("level_up")
  	 end,
}
When this snail dies, it triggers the spawner called ballspawner1
It also plays the level up sound.

EDIT: Ho ho! Who ninjas whom now? haha

Re: Monsters and Scripting

Posted: Wed Oct 24, 2012 5:28 pm
by LordGarth
You can have a key in the lootDrop.

LG

Re: Monsters and Scripting

Posted: Thu Oct 25, 2012 6:21 am
by Ahmyo
Thanks for all of the resources guys! I had thought to just have the monster drop a key, but I don't like that outcome personally. The link that had someone suggesting the use of hidden pressure plates in every square in the room was a good idea too, I might just try to use that if all else fails. Ultimately though, I think that using lua would be the easiest in the end, and the least cpu intensive. So, I've decided to use the onDie function but I don't understand how it should be implemented. This is what I've written into it.

cloneObject{
name = "welcomer",
baseObject = "spider",
onDie = function(self)
monsterdoor:open()
monsterdoor2:open()
end,
}

Yet, when I preview the map, I get an error "attempt to call global 'cloneObject' (a nil value) x"

Is there anything I can read to help me understand how and why this script is put together and what it all correlates to? For instance, 'onDie = function(self)' What does function(self) mean? I think I understand what its meant for, but just not what it means.. y'know what I mean?

Anyways, thanks again for this help I really appreciate it! :D

Re: Monsters and Scripting

Posted: Thu Oct 25, 2012 10:31 am
by Grimwold
I might be wrong, but have you put the clone in a script entity in the editor? If so, that's the reason it's failing.

Clones (and Definitions) of entities need to be added to the lua files associated with the dungeon.. not in script entities within it.

Depending on your os you should be able to find these files in:
C:\Users\YOUR USERNAME\Documents\Almost Human\Legend of Grimrock\Dungeons\YOUR DUNGEON\mod_assets\scripts\

As it's a monster, add the clone to the monsters.lua file.

Hope that helps. Apologies for not been very clear on this in my earlier post.

Re: Monsters and Scripting

Posted: Thu Oct 25, 2012 10:44 am
by Grimwold
as far as resources go, you could take a look at the official documentation:
http://www.grimrock.net/modding/
particularly the parts on scripting, hooks and asset definition.

to answer your question, a function is a way to isolate a piece of script and make it only run when you want it... usually a function will look something like

Code: Select all

function functionName(variable1,variable2)
 -- do some stuff here
end
so when we want to run that block of code, we call the function by name and pass any info we want to it via the variables.
e.g.

Code: Select all

functionName("spider",3)
With scripting hooks the format is a bit different, and the variables we use are determined by the type of hook. For example onDie will provide an entity for the actual monster that is dying.
hence

Code: Select all

onDie = function(self)
or you could write

Code: Select all

onDie = function(monster)
whatever word you put in the bracket you will be able to use to represent the monster that just died....

so a very simple example would be:

Code: Select all

onDie = function(monster)
  hudPrint(monster.name .. " just died")
end
which will print e.g. "spider just died" on the screen.

alternatively

Code: Select all

onDie = function(monster)
  hudPrint(monster.id .. " just died")
end
will print e.g. "spider_1 just died".

Re: Monsters and Scripting

Posted: Thu Oct 25, 2012 10:47 am
by Komag
that just taught me a lot! it would be good in the wiki too 8-)

Re: Monsters and Scripting

Posted: Fri Oct 26, 2012 9:06 am
by Ahmyo
Ah yes I was trying to put the code into a lua 'inspector shell' within the dungeon editor.. I'll put that into the directory you mentioned. This info helps out a lot. If I put this script where it needs to go, will it run automatically once the dungeon begins or do I then need to utilize the lua inspector shell to call the function when I need it? From stepping on a pressure plate or what have you? Or is it just 'watching' the monster I detail within it, and just always checking to see if its dead? Or..? I don't think I understand what you mean by clone when you refer to the script. I have yet to thoroughly go through the mod page though. I did skim it before this looking for a lua basics 'rubric' to use for piecing things together but didn't find what I was looking for. That's a little off topic though ha.

Re: Monsters and Scripting

Posted: Fri Oct 26, 2012 10:20 am
by Ahmyo
Ohhh I see, there is a brand new monster now..

I didn't understand that the script creates a completely brand new monster, I thought it was another code within the game which would be acting externally from any entities.

Edit:

Okay! It works now ha! How wonderful, now I can continue working on my dungeon :mrgreen: :mrgreen: :mrgreen:

Thank you for the help you guys!