📜  show hooks Foundry vtt (1)

📅  最后修改于: 2023-12-03 14:47:27.084000             🧑  作者: Mango

Show Hooks for Foundry VTT

Foundry Virtual Tabletop (Foundry VTT) is a popular virtual tabletop platform for playing tabletop games remotely. One of the many great features of Foundry VTT is its ability to allow module developers to extend its core functionality through Hooks.

Hooks are predefined events that allow developers to execute specific code at certain points in Foundry VTT's lifecycle. This enables developers to modify and enhance core functionality without modifying the source code directly.

Hook Types

There are several types of Hooks available in Foundry VTT. Some of the most commonly used include:

  • init - called when the game world initializes
  • ready - called when the server is ready and the game world is fully loaded
  • updateWorldTime - called when the game world time is updated
  • updateCombat - called when a combat turn ends
Registering Hooks

Developers can register their Hooks using the game's Hooks object. For example:

Hooks.on("ready", () => {
  console.log("The game is ready!");
});

This code registers a ready Hook that logs a message to the console when the game is ready.

Using Hooks

Developers can use Hooks to modify or enhance core Foundry VTT functionality. For example, they can use the preCreateXXX and createXXX Hooks to modify or add custom behavior to certain Foundry VTT objects.

Hooks.on("preCreateActor", (actor, options, userId) => {
  if (actor.data.type === "npc") {
    actor.data.flags = { isNPC: true };
  }
});

This code registers a preCreateActor Hook that adds a custom flag to NPC actors when they are created.

Conclusion

Hooks are a powerful feature in Foundry VTT that enable module developers to extend the platform's core functionality. By using Hooks, developers can modify and enhance Foundry VTT without modifying its source code directly.