BreynGINE

Your own BreynFlow pieces (tutorial)

Making it on a phone or a tablet? This is the version for computers: go to the version for phones, with the touch interface and pictures of the phone.
You're reading the tutorial on the web. In the editor on a computer it's in Help > Tutorials, next to your project. The links that copy drawings and sounds into your project in the editor download them as a ZIP here; on a phone, BreynGINE offers to copy them when you open the tutorial.

Chapter 1Your AI Can Help You

Before you start, something important: you don't have to do this alone. BreynGINE can make a file that teaches your AI assistant (ChatGPT, Claude, Gemini, Grok...) how BreynFlow works: every piece with its fields, the ready-made objects and how pieces of your own are made. With it, the AI can write you a whole piece, explain each line or fix the one that doesn't work.

1
Open the menu Help > Guide for Your AI...
2
If you want the AI to know your game, leave Include my project checked (its scenes, objects and rules). The file isn't sent anywhere: it stays with you.
3
Press Save File and attach it to your conversation with the AI, or press Copy and paste it.
The Guide for Your AI: a file with everything the AI needs to know about BreynFlow.
The Guide for Your AI: a file with everything the AI needs to know about BreynFlow.
4
Ask for whatever you want in your own words: "Make me a BreynFlow piece that makes an object pulse like a heart".

The AI will give you the code of the piece ready to paste, and it can also give you rules as text. Those rules are pasted in one go: in the BreynFlow screen, press Paste Rules..., paste the text and press Add the Rules. Before adding them you'll see how they read, sentence by sentence, and any problem they have.

Paste Rules: the text the AI writes, read as sentences before adding it.
Paste Rules: the text the AI writes, read as sentences before adding it.
Tip: it also works the other way round. In a rule's menu (the three dots ⋮), Copy Rule also copies it as text, and Paste Rules... has Copy This Object's Rules. Paste them to the AI and ask it why they don't do what you expect.

If you'd rather learn to do it yourself (or understand what the AI wrote for you), read on: in this tutorial you make a piece step by step.

Chapter 2What You'll Do

BreynFlow comes with lots of pieces: events (WHEN), conditions (IF) and actions (DO). But sometimes you're missing exactly one. That's what pieces of your own are for: a short GDScript file that BreynFlow shows in the finder and uses like any other piece.

You'll make Pulse, an action that makes an object grow and go back to its size a few times, like a heart. In the rule it will read like this: "make this object pulse 3 times in 1 s", and you'll be able to tap each value to change it, like in BreynGINE's pieces.

Then you'll see how to change its code, what namespaces are and how to gather your pieces in a pack to share them or sell them.

Tip: you don't need to know how to program to follow this tutorial: we give you the code and explain what each part does. If you've never used BreynFlow, do the welcome tutorial first.

Chapter 3Get an Object Ready

You can use any game project. If you want to start from scratch, create a new one with the 2D (camera) template.

1
In the Hierarchy, press the + button and choose Sprite. Call it Heart (double-click its name). If you have a drawing of a heart, drag it onto its Texture (in the Inspector); if not, the provisional drawing will do.
2
With Heart selected, go to the Inspector, scroll to the very bottom and press Add Component > BreynFlow (Rules Without Code).
3
At the top, in the bar of screens, press BreynFlow.
4
Press New Rule and choose the event Scene Starts.

Now you have a rule that says "When the scene starts". What it does is missing: that's where your piece goes.

Chapter 4Create the Piece

1
In the rule, press + Action. The piece finder opens.
2
At the bottom, press Create My Own Piece...
The piece finder. Under each piece, small, its namespace; at the bottom, "Create My Own Piece...".
The piece finder. Under each piece, small, its namespace; at the bottom, "Create My Own Piece...".
3
In Name of the piece write Pulse. It's the name you'll see in the finder.
4
In Where it's saved leave My Pieces (this project).
5
Press Create.
Create My Own Piece: its name and where it's saved.
Create My Own Piece: its name and where it's saved.

Three things happen at once: the file res://piezas/pulse.gd is created with a template, the piece is added to your rule and its code opens in the Script screen.

Chapter 5What the Template Has

The template already works: it's a sample piece with comments (the lines starting with #) that explain each part. These are the important ones:

The template of an action, in the Script screen.
The template of an action, in the Script screen.
  • @tool: lets the editor use the piece while you work (to show it in the finder and in the rules). It always goes first.
  • class_name Pulse: the name of the piece in the code.
  • extends FlowAction: says it's an action. A condition would be FlowCondition and an event, FlowEvent.
  • The @export var lines: the fields filled in in the rule. A FlowTarget field is "which object" (empty means this object).
  • _get_piece_info(): how the piece looks. Its name, its sentence (each field goes between braces, like {target}), its category, its description and an example.
  • _execute(context): what the piece does. It returns true when it's done.

Chapter 6Write Pulse

Now swap the template for the real piece. Select all the code (Ctrl+A) and replace it with this one (Copy the code puts it in the clipboard; paste it with Ctrl+V):

@tool
class_name Pulse
extends FlowAction

# The fields of the piece: which object pulses (empty: this object), how many times,
# in how many seconds and how much it grows (1.3 is 30% bigger).
@export var target: FlowTarget
@export var beats := 3
@export var seconds := 1.0
@export var size := 1.3

func _get_piece_info() -> Dictionary:
	return {
		"name": "Pulse",
		"sentence": "make {target} pulse {beats} times in {seconds} s",
		"category": "My Pieces",
		"description": "Makes an object grow and go back to its size a few times, like a heart.",
		"example": "The scene starts: make this object pulse 3 times in 1 s.",
	}

# It starts: keeps the size of each object and sets the clock to zero.
func _execute(context: FlowContext) -> bool:
	var start := {}
	for node in context.get_targets(target):
		if "scale" in node:
			start[node] = node.scale
	context.set_state(self, &"start", start)
	context.set_state(self, &"time", 0.0)
	return false # Not done yet: the rule waits.

# Every frame, until it returns true: it grows and shrinks.
func _process_action(context: FlowContext, delta: float) -> bool:
	var time: float = context.get_state(self, &"time", 0.0) + delta
	context.set_state(self, &"time", time)
	var done := time >= seconds
	var grow := 0.0 if done else absf(sin(time / seconds * beats * PI))
	var start: Dictionary = context.get_state(self, &"start", {})
	for node in start:
		if is_instance_valid(node):
			node.scale = start[node] * (1.0 + (size - 1.0) * grow)
	return done

Save with Ctrl+S.

The piece, finished (in the picture, in Spanish: "Latir").
The piece, finished (in the picture, in Spanish: "Latir").

How It Works

  • _execute() is called once, when the rule gets to the piece. It writes down the size of each object and returns false: "I'm not done yet".
  • While it isn't done, BreynFlow calls _process_action() every frame. The piece works out how much to grow with a wave (the sin function) and returns true when the seconds have passed. Then the rule goes on with the next action.
  • get_state() and set_state() keep the values of each time the piece is used. It matters: if ten enemies have the same rule, they share the piece, so what changes while the game runs isn't kept in its variables.
Tip: the label of each field in the rule comes from its variable's name: beats shows as "Beats". That's why it's worth naming them in your language.

Chapter 7Use It in the Rule

Go back to the BreynFlow screen. Your rule now says:

The rule with your piece, just like BreynGINE's.
The rule with your piece, just like BreynGINE's.

"When the scene starts: make this object pulse 3 times in 1 s". Tap any highlighted value to change it: make it 5 times, or 2 seconds.

Press ▶ (at the top) to try the game: the heart pulses when it starts.

Your piece is in the finder for good, in the My Pieces category. You can use it in any rule of this project, with any event: when touching the player, when receiving a message, every 2 seconds...

Changing the Code Later

In the rule, the piece's menu (the three dots ⋮) has Edit the Piece's Code: it opens its file in the Script screen. Change whatever you want and save: every rule that uses it changes at once.

The menu of a piece of your own: "Edit the Piece's Code".
The menu of a piece of your own: "Edit the Piece's Code".

Chapter 8Namespaces

Look at the piece finder: under the name of each piece, small, it says where it comes from. It's its namespace:

  • BreynGINE: the pieces that come with BreynGINE.
  • My Pieces: those of your project, like Pulse.
  • author.pack: those of a pack of pieces (the next chapter).

With the drop-down next to the search you can see only those of one of them.

The namespace filter: All, BreynGINE, My Pieces and each pack.
The namespace filter: All, BreynGINE, My Pieces and each pack.

Thanks to namespaces, two pieces can have the same name without mix-ups: the "Pulse" of your project and the "Pulse" of a pack you download are two different pieces, and in the finder you know which is which.

Chapter 9Make a Pack

A pack gathers several pieces to share them with other people or sell them in the store. It's a folder res://addons/author/pack/ with the pieces inside and a pack.cfg file that says its name, its author, its version and its description.

To start one:

1
In the piece finder, press Create My Own Piece...
2
Write the name of the new piece and, in Where it's saved, choose A New Pack...
3
Write your name (or your studio's) and the name of the pack. Below you'll see where it will be saved.
4
Press Create. The pack's folder, its pack.cfg and the piece are created.
A piece in a new pack: its namespace will be author.pack.
A piece in a new pack: its namespace will be author.pack.

You create the next pieces of the pack the same way, choosing the pack in Where it's saved.

Tip: the pieces of a pack have no class_name: BreynFlow finds them by their folder. That way two packs can have pieces with the same name without clashing with each other or with yours.

Share It

To share a pack, share its folder (for example, zipped). Whoever copies it into the addons folder of their project gets your pieces in their finder, with your namespace. And soon you'll be able to publish it in BreynGINE's store.

Open pack.cfg (in the Project panel) to write its description and raise its version when you improve it.

Chapter 10Now It's Your Turn

You know how to make pieces of your own. Some ideas to go on:

  • A condition (FlowCondition): "the life is even", "it's night". Its function is _check(context) and it returns true or false.
  • An event (FlowEvent): "every 3 jumps", "the mouse is shaken". It fires the rule with flow.trigger_rule(rule_index).
  • Ask your AI for ideas with the Guide for Your AI: with your project included, it knows exactly which pieces you have and which ones you're missing.
Tip: if a piece doesn't show up in the finder, check that it starts with @tool, that it has no errors (the Script screen marks them in red) and that you saved it.

← All the tutorials