v3.5.0 Documentation
Coding Tips

This is a collection of quick tips for scripting Fungus from code.

Executing Blocks

You first need to get a reference to your flowchart object. Add a public Flowchart property to your component and set it to reference your flowchart in the inspector.

1 using UnityEngine;
2 using Fungus;
3 
4 class MyComponent : public MonoBehaviour
5 {
6  public Flowchart flowchart;
7 }

To execute a named Block in the Flowchart:

1 flowchart.ExecuteBlock("BlockName");

To start execution at a specific command index:

1 flowchart.ExecuteBlock("BlockName", 3);

To tell if a Flowchart has any executing Blocks:

1 if (flowchart.HasExecutingBlocks())
2 {
3  // Do something
4 }

Block Signals

You can use the BlockSignals class to listen for events from the Block execution system.

1 using Fungus;
2 
3 public MyComponent : MonoBehaviour
4 {
5  void OnEnable()
6  {
7  // Register as listener for Block events
8  BlockSignals.OnBlockStart += OnBlockStart;
9  }
10 
11  void OnDisable()
12  {
13  // Unregister as listener for Block events
14  BlockSignals.OnBlockStart -= OnBlockStart;
15  }
16 
17  void OnBlockStart(Block block)
18  {
19  Debug.Log("Block started " + block.BlockName);
20  }
21 }

Writer Signals

You can use the WriterSignals class to listen for a variety of events from the text writing system.

1 using Fungus;
2 
3 public MyComponent : MonoBehaviour
4 {
5  void OnEnable()
6  {
7  // Register as listener for Writer state change events
8  WriterSignals.OnWriterState += OnWriterState;
9  }
10 
11  void OnDisable()
12  {
13  // Unregister as listener for Writer state change events
14  WriterSignals.OnWriterState -= OnWriterState;
15  }
16 
17  void OnWriterState(Writer writer, WriterState writerState)
18  {
19  if (writerState == WriterState.Start)
20  {
21  Debug.Log("Writing started");
22  }
23  }
24 }