---
title: "Logic Graphs"
description: "Visual scripting for MavonEngine — node graphs made in the level editor that run on the client, the server or both, with events, flow control, variables and your own nodes."
canonical_url: "https://mavonengine.com/core-concepts/logic-graphs"
---

> Index of every page on this site: [llms.txt](https://mavonengine.com/llms.txt)

A logic graph is game logic made of nodes instead of code: events start a run, exec edges put its steps in order, and value nodes work out what the steps need. You make them in the [level editor](https://mavonengine.com/core-concepts/level-editor)'s **Graphs** view, and a `LogicGraph` object in a [scene](https://mavonengine.com/core-concepts/scenes) runs one on the client, the server or both.

![The level editor's Graphs view with the multiplayer template's server-npc logic graph: groups for remembering where the NPC started, walking, turning around and answering clicks, with the graph's parameters on the right](https://mavonengine.com/static/logic-graph-editor.png)

## The template's NPC

The [multiplayer template](https://mavonengine.com/getting-started/installation#multiplayer) ships one: `graphs/server-npc.graph.json`. A `ServerNpc` in `scenes/main.json` is a networked entity that does nothing on its own. A `LogicGraph` object next to it runs `server-npc` on the server with the NPC as its Self, so the server walks it back and forth, and every player sees it move through the usual [state sync](https://mavonengine.com/networking/state-sync).

```json
{
  "id": "server_npc_logic",
  "type": "LogicGraph",
  "name": "Server NPC logic",
  "transform": { "position": [0, 0, -6], "rotation": [0, 0, 0], "scale": [1, 1, 1] },
  "props": {
    "graph": "server-npc",
    "side": "server",
    "target": "server_npc"
  }
}
```

**On Tick** moves the NPC along x by `dir × speed × delta` and turns it the way it walks. A **Branch** flips `dir` once it's further than `range` from where it started. `dir`, `speed` and `range` are the graph's parameters.

![The Walk and Turn around groups of the server-npc graph: On Tick runs Move By and Set Rotation, then a Branch compares how far the NPC got against range and flips dir](https://mavonengine.com/static/logic-graph-walk.png)

Clicking the NPC sends `CL_NPC_CLICK` to the server, which passes it on to the graphs acting on that NPC as a `clicked` message. **On Message** gets it, **Cooldown** lets it through at most once every 10 seconds, and the template's own **Say in Chat** node posts a line in the chat as the NPC:

![The Answer clicks group: On Message named clicked, then Cooldown of 10 seconds, then Say in Chat with the NPC's greeting](https://mavonengine.com/static/logic-graph-clicks.png)

```typescript
// server/src/Server.ts
channel.on(ClientCommand.CL_NPC_CLICK, (command) => {
  const cmd = command as CL_NPC_CLICK
  if (this.game.world.entities.items.get(cmd.npcId) instanceof ServerNpc)
    sendGraphMessage('clicked', player.name ?? '', cmd.npcId)
})
```

## Running a graph in a scene

A `LogicGraph` scene object loads a graph and runs it every tick. Add one with **Place in scene** in the Graphs view, or in the scene file:

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        graph
      </code>
    </td>
    
    <td>
      
    </td>
    
    <td>
      <code>
        name
      </code>
      
       for <code>
        graphs/<name>.graph.json
      </code>
      
      , <code>
        <package>:<name>
      </code>
      
       for a <a href="#graphs-from-plugins">
        plugin's
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        side
      </code>
    </td>
    
    <td>
      <code>
        'client'
      </code>
    </td>
    
    <td>
      Where it runs: <code>
        'client'
      </code>
      
      , <code>
        'server'
      </code>
      
       or <code>
        'both'
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        target
      </code>
    </td>
    
    <td>
      this object
    </td>
    
    <td>
      The id of the scene object the graph acts on as <strong>
        Self
      </strong>
      
      . Without one it moves itself, it has a transform of its own
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        parameters
      </code>
    </td>
    
    <td>
      the graph's defaults
    </td>
    
    <td>
      Values for the graph's parameters
    </td>
  </tr>
</tbody>
</table>

The editor shows it as a helper in the viewport, with the graph, where it runs and its Self in the Properties panel. A graph that runs only on the server is still built on the client, so the editor can show and move it.

The server reads graphs from the project's `graphs/` directory, so give it the `root` option (the template does). In dev it watches that directory: saving a graph in the editor restarts it in every object running it on the server, from On Start, and a graph that doesn't run any more keeps the old version and logs why. On the client the editor does the same as you edit, and the steps light up in the Graphs view as they run.

## Nodes

Steps have exec ports (white) and run in order along them. Value nodes have none: they're worked out when a step reads them, fresh for every step, so a step sees what the ones before it changed. A step's outputs keep their last value.

<table>
<thead>
  <tr>
    <th>
      Category
    </th>
    
    <th>
      Nodes
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      Events
    </td>
    
    <td>
      <strong>
        On Start
      </strong>
      
       (once), <strong>
        On Tick
      </strong>
      
       (every tick, with <code>
        delta
      </code>
      
      ), <strong>
        Every
      </strong>
      
       (every few seconds), <strong>
        On Key
      </strong>
      
       (a key by its code, e.g. <code>
        Space
      </code>
      
      , <code>
        KeyW
      </code>
      
      ; client only), <strong>
        On Message
      </strong>
      
       (a message by name, with its value)
    </td>
  </tr>
  
  <tr>
    <td>
      Flow
    </td>
    
    <td>
      <strong>
        Branch
      </strong>
      
      , <strong>
        Sequence
      </strong>
      
       (outputs 1, 2, 3 in turn), <strong>
        Do Once
      </strong>
      
      , <strong>
        Cooldown
      </strong>
      
       (sooner runs go to Blocked), <strong>
        Delay
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      Actions
    </td>
    
    <td>
      <strong>
        Log
      </strong>
      
       (to the game's log), <strong>
        Send Message
      </strong>
      
       (to every logic graph on this side)
    </td>
  </tr>
  
  <tr>
    <td>
      Entity
    </td>
    
    <td>
      <strong>
        Self
      </strong>
      
      , <strong>
        Scene Object
      </strong>
      
       (by id), <strong>
        Position
      </strong>
      
       / <strong>
        Rotation
      </strong>
      
       / <strong>
        Scale
      </strong>
      
      , <strong>
        Set …
      </strong>
      
      , <strong>
        Move By
      </strong>
      
       / <strong>
        Rotate By
      </strong>
      
       / <strong>
        Grow By
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      Variables
    </td>
    
    <td>
      <strong>
        Get Variable
      </strong>
      
      , <strong>
        Set Variable
      </strong>
      
      . A variable starts as the parameter of the same name, if there is one
    </td>
  </tr>
  
  <tr>
    <td>
      Logic
    </td>
    
    <td>
      <strong>
        Compare
      </strong>
      
       (<code>
        <
      </code>
      
      , <code>
        <=
      </code>
      
      , <code>
        ==
      </code>
      
      , <code>
        !=
      </code>
      
      , <code>
        >=
      </code>
      
      , <code>
        >
      </code>
      
      ), <strong>
        And
      </strong>
      
      , <strong>
        Or
      </strong>
      
      , <strong>
        Not
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      Input
    </td>
    
    <td>
      <strong>
        Float
      </strong>
      
      , <strong>
        Vec2
      </strong>
      
      , <strong>
        Vec3
      </strong>
      
      , <strong>
        Vec4
      </strong>
      
      , <strong>
        Color
      </strong>
      
      , <strong>
        Bool
      </strong>
      
      , <strong>
        Text
      </strong>
      
      , <strong>
        Parameter
      </strong>
      
      , <strong>
        Time
      </strong>
    </td>
  </tr>
  
  <tr>
    <td>
      Math, Vector
    </td>
    
    <td>
      Add, Subtract, Multiply, Divide, Min, Max, Power, Step, Sine, Cosine, Absolute, Floor, Fraction, Square Root, Negate, One Minus, Mix, Clamp, Smoothstep, Remap; Combine, Split, Length, Distance, Dot, Normalize, Cross
    </td>
  </tr>
</tbody>
</table>

Entity inputs take a scene object's id; left empty they mean Self. Math nodes work on numbers and vectors alike, component by component: a float and a vec3 give a vec3.

**Comment** and **Group** nodes do nothing when the graph runs. A comment describes a part of the graph, a group is a named box that moves the nodes in it together.

A run can't hang the game: one event runs at most 10 000 steps, then the graph logs that its exec edges probably loop.

## Messages

**Send Message** reaches every logic graph on the same side, and their **On Message** nodes with that name. From code, send one with `sendGraphMessage()`, to every graph or only to those acting on one scene object:

```typescript
import { sendGraphMessage } from '@mavonengine/core/Graph/Logic/logicGraphObject'

sendGraphMessage('alarm', 1)                 // every logic graph on this side
sendGraphMessage('clicked', 'Alice', npc.id) // only the graphs whose Self is npc
```

Messages stay on their side: a client graph doesn't hear a server graph. Send a [command](https://mavonengine.com/networking/client) across and call `sendGraphMessage()` on the other side, as the template does for clicks.

## Your own nodes

A node type is a `NodeDefinition`: its ports and a `run` for a step, or an `eval` for a value node. Register it on every side whose graphs use it; the editor needs it on the client to show it, even when the graph only runs on the server. This is the template's Say in Chat node:

```typescript
import type { NodeDefinition } from '@mavonengine/core/Graph/NodeRegistry'
import { registerNode } from '@mavonengine/core/Graph/NodeRegistry'

export const sayInChatNode: NodeDefinition = {
  type: 'chat.say',
  label: 'Say in Chat',
  category: 'Actions',
  description: 'Post a line in the chat as the graph\'s Self',
  kinds: ['logic'],
  inputs: {
    run: { type: 'exec', label: '' },
    message: { type: 'any', label: 'Message', default: 'Hello!' },
  },
  outputs: {
    then: { type: 'exec', label: '' },
  },
  run: ({ message }, { self }) => {
    post?.(self?.id ?? '', String(message ?? ''))
  },
}

registerNode(sayInChatNode)
```

<table>
<thead>
  <tr>
    <th>
      Field
    </th>
    
    <th>
      
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        type
      </code>
      
      , <code>
        label
      </code>
      
      , <code>
        category
      </code>
      
      , <code>
        description
      </code>
    </td>
    
    <td>
      Its key in graph files, and how the editor lists it
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        kinds
      </code>
    </td>
    
    <td>
      <code>
        ['logic']
      </code>
      
      , <code>
        ['material']
      </code>
      
       or both
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        inputs
      </code>
      
      , <code>
        outputs
      </code>
    </td>
    
    <td>
      Ports by name: <code>
        { type, label?, default? }
      </code>
      
      . Types: <code>
        float
      </code>
      
      , <code>
        int
      </code>
      
      , <code>
        bool
      </code>
      
      , <code>
        vec2
      </code>
      
      , <code>
        vec3
      </code>
      
      , <code>
        vec4
      </code>
      
      , <code>
        color
      </code>
      
      , <code>
        texture
      </code>
      
      , <code>
        string
      </code>
      
      , <code>
        entity
      </code>
      
      , <code>
        exec
      </code>
      
      , plus <code>
        number
      </code>
      
       (any number or vector) and <code>
        any
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        settings
      </code>
    </td>
    
    <td>
      Values set on the node itself rather than connected
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        eval(inputs, ctx)
      </code>
    </td>
    
    <td>
      A value node's outputs
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        run(inputs, ctx)
      </code>
    </td>
    
    <td>
      A step. Return <code>
        { next, outputs }
      </code>
      
       to pick the exec outputs to continue from (the first by default, none for <code>
        []
      </code>
      
      ) and set its outputs
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        trigger
      </code>
    </td>
    
    <td>
      Makes it an event: the runtime event (<code>
        start
      </code>
      
      , <code>
        tick
      </code>
      
      , <code>
        keydown
      </code>
      
      , <code>
        message
      </code>
      
      , or your own) that runs it. Its <code>
        run
      </code>
      
       returns <code>
        false
      </code>
      
       to let an event pass
    </td>
  </tr>
</tbody>
</table>

`run` gets `self`, `entity(id)`, `parameters`, `values`, `time`, `variables`, its own `state` kept between runs, `after(seconds, port)` to continue later, `emit(name, value)` and `log(message)`.

Plugins register nodes through their context instead, keyed as `<package>:<type>`, see [Plugins](https://mavonengine.com/tooling/plugins#graphs-and-nodes).

## Subgraphs

A graph with **Graph Input** and **Graph Output** nodes is a function: it shows up as a node in other graphs of its kind, under Subgraphs, with a port for each input and output. Subgraphs hold values only, no events or steps. The editor lists them with a *subgraph* badge.

## Graph files

Graphs are JSON files in the project's `graphs/` directory, next to `scenes/`: `graphs/<name>.graph.json`, names of letters, digits, `-` and `_`. The editor saves them through the dev server, and every release ships them.

```json
{
  "version": 1,
  "kind": "logic",
  "name": "server-npc",
  "nodes": [
    { "id": "tick", "type": "event.tick", "position": [0, 0] },
    { "id": "move", "type": "entity.addPosition", "position": [240, 0] }
  ],
  "edges": [
    { "from": { "node": "tick", "port": "then" }, "to": { "node": "move", "port": "run" } }
  ],
  "parameters": [
    { "name": "speed", "type": "float", "default": 1.5 }
  ]
}
```

### Graphs from plugins

A plugin can ship graphs in a `graphs/` directory of its package. They go by `<package>:<name>`, are served at `/graphs/<package>/<name>.graph.json` and ship in every release. The editor lists them read-only; **Copy to project** makes an editable copy.

## Running a graph from code

```typescript
import { readGraph } from '@mavonengine/core/Graph/loadGraphFile' // Node only
import { GraphRuntime } from '@mavonengine/core/Graph/Logic/GraphRuntime'

const runtime = new GraphRuntime(readGraph(root, 'server-npc'), {
  self,                         // a GraphEntity: get/set position, rotation, scale
  find: id => entityById(id),
  parameters: { speed: 3 },
  log: message => logger.info(message),
})

runtime.start()               // On Start
runtime.update(delta)         // Delays, then On Tick
runtime.message('clicked', 'Alice')
```

A graph with problems (unknown nodes, edges between ports that don't fit, a node that can't run in a logic graph) throws a `GraphRuntimeError` listing them. The editor shows the same list under **Problems**.

## Sitemap

See the full [sitemap](https://mavonengine.com/sitemap.md) for all pages.
