Overview
TheVectorDisplay2D class extends Node2D and provides real-time visualization of Vector2 properties from any node. It renders vectors with customizable colors, scaling, and display modes.
Class Definition
Properties
The node whose vector property will be visualized. If not set, automatically assigns to the parent node on ready.
Name of the Vector2 attribute or variable in the target node’s script to display.
Vector display settings resource. Create your own using a
VectorDisplaySettings resource to customize appearance and behavior.Internal Variables
Stores the current processed vector value for rendering.
Stores the raw length of the vector before any length mode transformations.
Methods
_ready()
- Validates target node, property, and settings using
VectorDisplayFunctions.check_targets_and_settings() - Connects settings resource’s
changedsignal toqueue_redraw()for automatic visual updates
_process()
_delta: Time elapsed since the previous frame (unused)
- Returns early if target node is invalid
- Gets vector from
target_propertyand appliesvector_scale - Applies length mode transformation via
VectorDisplayFunctions.apply_lenght_mode() - Only triggers redraw when vector or length changes (performance optimization)
_draw()
queue_redraw() is triggered.
Behavior:
- Returns early if
settings.show_vectorsis false - Calculates colors based on rainbow and dimming settings
- Renders main vector line with arrowhead
- Renders X and Y axis components if
settings.show_axesis true - Uses antialiasing for smooth lines
_draw_arrowhead()
start: Starting position of the vector lineposition: End position where the arrowhead should be drawncolor: Color to render the arrowhead
- Returns early if
settings.arrowheadis false - Calculates arrowhead size based on
settings.arrowhead_sizeand line width - Hides arrowhead if vector is too small to display it properly
- Renders triangular arrowhead rotated 30 degrees on each side
_unhandled_key_input()
event: Input event to check against the shortcut
- Checks if event matches the display shortcut using
VectorDisplayFunctions.check_shortcut() - Marks input as handled to prevent event propagation and echo errors
Usage Examples
Basic Setup
Custom Target and Property
Displaying Force Vectors
Runtime Visibility Toggle
Notes
- The VectorDisplay2D node automatically positions itself at the target node’s location due to transform inheritance
- Performance is optimized to only redraw when the vector actually changes
- The node validates all settings on
_ready()and provides helpful error messages in the console - If no target node is specified, it automatically uses the parent node
