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,
}