Teleport an enemy without a teleporter.

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!
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Teleport an enemy without a teleporter.

Post by Isaac »

trancelistic wrote:I wish I understood more about "caller" I never used such a thing... Cuz yeah I never learned it haha:)
It's an arbitrary name ~but one that suits.

When the hook calls the function, it sends itself as the first argument. Calling it "caller" (or any valid name) sets up a variable in the function that is whatever object called the function. It could be (and often is) named "self". ;)

The scripting reference page lists the components, and they (usually at the bottom of each description) list supported hooks. If you define the hook as a function, then that function gets called when that event happens in the game. If a monster has a defined onDie hook function, then that code is run when the monster is killed.
trancelistic
Posts: 275
Joined: Sat Jun 16, 2012 5:32 am

Re: Teleport an enemy without a teleporter.

Post by trancelistic »

Isaac wrote:
trancelistic wrote:I wish I understood more about "caller" I never used such a thing... Cuz yeah I never learned it haha:)
It's an arbitrary name ~but one that suits.

When the hook calls the function, it sends itself as the first argument. Calling it "caller" (or any valid name) sets up a variable in the function that is whatever object called the function. It could be (and often is) named "self". ;)

The scripting reference page lists the components, and they (usually at the bottom of each description) list supported hooks. If you define the hook as a function, then that function gets called when that event happens in the game. If a monster has a defined onDie hook function, then that code is run when the monster is killed.

Could you show the most easist exsample?
onHook or a caller..
Could script me something as easy as. pff "print the hud?" Something like that.

I think I could get on later on this.. or so<3
THanks
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Teleport an enemy without a teleporter.

Post by Isaac »

Each hook in the script reference lists the arguments given to the hook function; these can be different for different hooks.

When you pick your component, see what hooks it supports. They all begin with the word "on", like onInsertItem for surface components. So an altar definition can optionally include an onInsertItem function. When an object is placed on an altar ~if the altar [surface component] has a defined inInsertItem function, that function is run.

With a monster component for example, one of the optional hooks is onMove. If defined, then the onMove function runs every time the monster moves a step. Another hook for the monster component is onTurn, and as you can guess, that function gets called every time the monster turns in place.

Try this out on a map. Define it in objects.lua, and place an 'altar_verbose' object on the map, along with some food or other items. In the preview, placing items on the altar will print the item's name on the screen. The code for this is run by interacting with the altar ~via the hooks that get called by their respective events.

Code: Select all

defineObject{
	name = "altar_verbose",
	baseObject = "base_altar",
	components = {
		{
			class = "Model",
			model = "assets/models/env/dungeon_altar.fbx",
			staticShadow = true,
		},
				{
			class = "Surface",
			offset = vec(0, 0.88, 0),
			size = vec(2.1, 1.2),
			onInsertItem = function(self, item)
						hudPrint("") hudPrint("") hudPrint("") hudPrint("") hudPrint("")
						local itemName = string.gsub(item.go.name, "_", " ")
						hudPrint(string.upper(string.sub(itemName, 1,1))..string.sub(itemName, 2).." placed on the "..self.go.name..".")
						end, 	
						
			onRemoveItem = function(self, item)	
						hudPrint("") hudPrint("") hudPrint("") hudPrint("") hudPrint("")  --ensures a single line, by printing several blank lines before printing the message.
						local itemName = string.gsub(item.go.name, "_", " ") 		    -- replaces underscores in the item name with spaces.
						hudPrint(string.upper(string.sub(itemName, 1,1))..string.sub(itemName, 2).." removed from the "..self.go.name..".")
							   --^  prints first character of the name in uppercase --^ prints the rest of the name, starting with the 2nd letter.
						end,
		},
	},
	automapIcon = 152,
}
Post Reply