From e6fd25aaa005c597ecbab957cbbe366dbefb1394 Mon Sep 17 00:00:00 2001 From: Vicente Dragicevic <3252614+vdrg@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:56:24 -0300 Subject: [PATCH] Add basic docs --- docs/pages/world/systems.mdx | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/pages/world/systems.mdx b/docs/pages/world/systems.mdx index 5cd3cde5ea..ef3884bfcb 100644 --- a/docs/pages/world/systems.mdx +++ b/docs/pages/world/systems.mdx @@ -227,6 +227,43 @@ If your `System` needs run both from the root namespace and from other namespace +#### System Libraries + + + This is an experimental feature and is not recommended for production use yet. Its API is expected to change in the + future. + + +By setting the `generateSystemLibraries` codegen setting to true in your MUD config, the `worldgen` CLI will automatically generate a Solidity library for each system in your project. This feature simplifies inter-system communication by providing a convenient way of calling functions between systems. + +Each generated library defines and exports a custom user-defined type that includes all the functions defined in the corresponding system. Internally, these system libraries utilize the World's `call` and `callFrom` methods to execute the actual system contracts. + +```solidity filename="MySystem.sol" +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.24; + +import { System } from "@latticexyz/world/src/System.sol"; + +// Library generated for MoveSystem, +import { moveSystem } from "./codegen/libraries/MoveSystemLib.sol"; + +contract MySystem is System { + function someAction(uint256 x, uint256 y) external { + // All functions defined in the MoveSystem system are available globally for the exported `moveSystem` value + moveSystem.move(x, y); + + // You can also use delegations (if they exist) by using `callFrom` + moveSystem.callFrom(_msgSender()).move(x, y); + + // If MySystem is a root system, you need to use `callAsRoot` + moveSystem.callAsRoot().move(x, y); + + // If MySystem is a root system, it can call other systems on behalf of any address with `callAsRootFrom` + moveSystem.callAsRootFrom(_msgSender()).move(x, y); + } +} +``` + ## Registering systems For a `System` to be callable from a `World` it has to be [registered](https://github.com/latticexyz/mud/blob/main/packages/world/src/modules/init/implementations/WorldRegistrationSystem.sol#L115-L178).