React.Godot
Let's bring declarative and functional UI programming into Godot 4!
I'm mimicking the pattern popularized by React.js.
The idea
When you use React.Godot library, you define your UI tree
- in a declarative and functional style, resulting in clean and maintainable code,
- with extra discipline,
- without any caching or optimization.
The React.Godot library automatically caches information and optimize the information propagation for you.
Discipline: what you have to adhere to
A react node must (Recommended: copy from "ReactNodeTemplate.cs"):
- Implements IReactable.
- Props are private and marked with [ReactProp].
- States are private and marked with [ReactState].
- All other fields must be immutable after _Ready().
- Has exactly three public methods: SetProps(...), React(), and _Ready().
- SetProps(...) and React() are auto-generated by metaprogramming.
- Its constructor takes no arguments.
- In Declare(), read only props and states, and nothing else.
- e.g. don't read system time. Don't read Godot node attributes.
- The control flow should visit each godot node attribute assignment exactly once. e.g.,
- don't
return
from Declare() early.
- don't put a node attribute assignment in an
if
branch.
- Similarly, visit each
SubNode.SetProps(...)
exactly once.
- Props (and state values) must be immutable.
The python metaprogrammer
- Populates SetProps(...) according to props.
- Provides
{state_name}_
attributes as shorthands for getting and setting states.