Overview
TheVectorDisplay2D node is the core component of the addon. It extends Node2D and provides real-time visualization of Vector2 properties from any node in your scene.
This is a child node that visualizes vectors from its parent or any specified target node.
Exported Properties
The node exposes three key properties in the Godot Inspector:target_node
vector_display_2d.gd:5
- If not set, automatically uses the parent node
- Validated in
_ready()to ensure it exists - Checked each frame for validity using
is_instance_valid()
Leaving
target_node empty is the recommended approach when visualizing the parent node’s vectors. This makes the addon work out-of-the-box.- Visualize a CharacterBody2D’s velocity
- Show a RigidBody2D’s linear velocity
- Display custom movement vectors from player scripts
target_property
vector_display_2d.gd:7
Vector2 property or variable to display.
Default: "velocity" (works with CharacterBody2D and RigidBody2D)
Requirements:
- Must be a valid property name on the target node
- Property must be of type
Vector2 - Can be built-in properties or custom script variables
Example: Custom property
Example: Custom property
If you have a player script with a custom vector:Set
target_property to "aim_direction" to visualize it.settings
vector_display_2d.gd:9
VectorDisplaySettings resource containing all visual configuration.
Features:
- Shared across multiple VectorDisplay2D nodes
- Modifiable at runtime
- Automatically triggers redraw on changes
- Must be set (no default value)
Settings are covered in detail in the Settings Resource page.
Internal Variables
The node maintains two internal state variables:vector_display_2d.gd:13-14
current_vector
Stores the processed vector after scaling and length mode transformations. Used for:- Drawing the visual representation
- Comparison to detect changes (smart redraw)
- Position and color calculations
current_raw_length
Stores the original vector magnitude before length mode is applied. Used for:- Color dimming calculations
- Preserving true magnitude information when clamped/normalized
- Change detection with floating-point tolerance
Tracking both processed and raw values ensures effects like dimming work correctly even when using Clamp or Normalize length modes.
Lifecycle Methods
_ready()
Initializes the node and sets up the settings connection:vector_display_2d.gd:18-22
- Validate target node and property
- Auto-assign parent node if needed
- Connect settings change signal
- Display errors for invalid configuration
Validation details
Validation details
The validation function (
vector_display_functions.gd:6-21) checks:_process(delta)
Runs every frame to capture and process the target vector:vector_display_2d.gd:26-40
- Safety check - Verify target node still exists
- Read - Get current property value using reflection
- Scale - Apply visual scaling factor
- Transform - Apply length mode (Normal/Clamp/Normalize)
- Compare - Check if vector actually changed
- Update - Store new values and queue redraw
The smart comparison prevents unnecessary redraws, significantly improving performance when vectors are static.
_draw()
Handles all rendering using Godot’s 2D drawing API:vector_display_2d.gd:43-62
- Check if vectors should be shown
- Calculate colors (rainbow, dimming)
- Get main vector positions (pivot mode)
- Draw main vector line and arrowhead
- If enabled, draw X/Y component axes
_unhandled_key_input(event)
Handles keyboard shortcuts for toggling visibility:vector_display_2d.gd:91-93
Using in Your Scenes
Basic Setup
- Add
VectorDisplay2Das a child of the node with vectors - Create or assign a
VectorDisplaySettingsresource - Set
target_propertyto match your vector variable
Advanced Setup
For visualizing vectors from a different node:Why attach to a separate node?
Why attach to a separate node?
Attaching the visualizer to a separate node is useful when:
- You want the visualization at a different position
- The target node might be deleted (visualizer persists)
- You’re visualizing vectors from multiple nodes
- You need different positioning or layering
Runtime Modification
All properties can be modified at runtime:Changing
target_node or target_property at runtime requires the new configuration to be valid, but there’s no automatic re-validation. Make sure the new target and property exist.Position and Transform
SinceVectorDisplay2D extends Node2D, it supports all standard 2D transformations:
- Position - Moves the vector origin
- Rotation - Rotates the entire visualization
- Scale - Scales the visual size (in addition to
vector_scale)
The node’s position determines where the vector origin appears. For following a moving object, make the VectorDisplay2D a child so it inherits the parent’s transform.
Performance Considerations
Optimizations Built-In
- Smart redraw - Only redraws when vectors change
- Settings signals - Automatic updates on configuration changes
- Early returns - Skips rendering when disabled
- Validity checks - Prevents errors from deleted nodes
Best Practices
- Share
VectorDisplaySettingsresources across multiple visualizers - Use
show_vectors = falseinstead of removing nodes to temporarily hide - Avoid changing
target_nodeevery frame (expensive reflection) - Keep
vector_scalereasonable to avoid extremely large draw calls
The addon is designed to be performant even with many instances running simultaneously thanks to the smart redraw system.
