EnhancedGridMap
EnhancedGridMap is a powerful plugin for Godot 4.3 that extends the functionality of the built-in GridMap node. It provides additional features for grid-based game development, including custom cell states, multi-floor support, and advanced grid manipulation tools.
Install / Use
/learn @DanchieGO/EnhancedGridMapREADME
EnhancedGridMap Plugin for Godot 4.4
Overview
EnhancedGridMap is a powerful plugin for Godot 4.4 that extends the functionality of the built-in GridMap node. It provides additional features for grid-based game development, including custom cell states, multi-floor support, and advanced grid manipulation tools.
Features
- Custom grid size (columns and rows)
- Multi-floor support with floor management
- Auto-generation of grid
- Custom cell states with visual representation
- A* pathfinding with diagonal movement option
- Randomization of grid cells
- Fill grid with specific cell types
- Item swapping functionality
- Editor dock for easy manipulation of grid properties
- Floor-specific operations for generating, clearing, and filling grids
Installation
- Copy the
enhanced_gridmapfolder into your Godot project'saddonsdirectory. - Enable the plugin in Project Settings > Plugins.
EnhancedGridMap Dock
The EnhancedGridMap Dock provides a user-friendly interface to control and manipulate the EnhancedGridMap node. Here's a detailed explanation of all features available in the dock:
Grid Properties
- Columns: Set the number of columns in the grid.
- Rows: Set the number of rows in the grid.
- Floors: Set the number of floors in your grid.
- Current Floor: Select which floor to edit.
- Auto Generate: Toggle automatic grid generation when properties change.
Grid Operations
- Generate: Manually generate the grid based on current properties.
- Clear: Remove all cells from the grid.
- Randomize: Randomly assign cell types based on defined states.
- Fill: Fill the entire grid with a specific cell type.
- Swap Items: Replace all instances of one item type with another.
Cell States
The dock allows you to define and manage custom cell states:
-
Default States:
- Normal
- Hover
- Start
- End
- Non-Walkable
-
Custom States:
- Add new states with the "Add Item State" button.
- For each state, you can set:
- Name
- ID (used in scripts to reference the state)
- Include in Randomize (toggle)
- Randomize Percentage (when included in randomization)
-
State Management:
- Edit existing states
- Remove custom states
- Swap items between states
Floor Management
The new floor management system allows you to:
- Add/Remove Floors: Dynamically adjust the number of floors in your grid.
- Floor Navigation: Easily switch between different floors for editing.
- Floor-Specific Operations:
- Generate specific floors
- Clear individual floors
- Fill selected floors with specific items
Item Swapping
The new item swapping feature allows you to:
- Select Source Item: Choose the item type to be replaced.
- Select Target Item: Choose the new item type to replace with.
- Swap Items: Replace all instances of the source item with the target item.
- Floor-Specific Swapping: Option to swap items only on the current floor or across all floors.
A* Pathfinding
- Start Position: Set the X and Z coordinates for the pathfinding start point.
- End Position: Set the X and Z coordinates for the pathfinding end point.
- Find Path: Calculate and visualize the path between start and end points.
- Diagonal Movement: Toggle to allow diagonal movement in pathfinding.
Visual Grid Editor
The dock includes a visual representation of the grid:
- Each cell displays its coordinates.
- Drop-down menus in each cell allow you to change the cell's state directly.
In-Depth Feature Explanation
Custom Grid Generation
The EnhancedGridMap allows for flexible grid generation:
- Auto-generation: The grid can automatically update when properties change.
- Manual generation: Use the
generate_grid()method for custom generation logic. - Clear and regenerate: Useful for level resets or procedural generation.
Cell States
Cell states are central to the EnhancedGridMap's functionality:
- Default states: Predefined states for common use cases (normal, hover, start, end, non-walkable).
- Custom states: Create states for specific game mechanics (e.g., water, lava, ice).
- State properties:
- ID: Used in scripts to set or check cell states.
- Name: Human-readable label
- Randomize inclusion: Determine if the state should be included in randomization.
- Randomize percentage: Control the frequency of the state when randomizing.
A* Pathfinding
The integrated A* pathfinding system offers:
- Efficient pathfinding: Quickly find optimal paths between two points.
- Diagonal movement: Option to allow or disallow diagonal movement.
- Custom cost functions: Override
get_cell_cost()for complex movement rules. - Path visualization: Easily visualize calculated paths for debugging.
Grid Manipulation
Several methods for manipulating the grid:
set_cell_from_data(x, z, item_index): Set a specific cell's state.fill_grid(item_index): Fill the entire grid with a specific state.randomize_grid(): Randomly assign states based on defined probabilities.randomize_grid_custom(randomize_states): Use custom randomization rules.
Extending Functionality
The EnhancedGridMap is designed to be easily extended:
- Custom grid generation: Override
generate_grid()for specialized layouts. - Custom pathfinding costs: Implement
get_cell_cost()for complex movement rules. - Integration with game mechanics: Use signals like
grid_updatedto trigger game events.
Editor Integration
The plugin seamlessly integrates with the Godot editor:
- Visual editing: Manipulate the grid directly in the editor viewport.
- Real-time updates: Changes in the dock immediately reflect in the scene.
- Custom inspector: The EnhancedGridMap node has a custom inspector for easy property editing.
Plugin Usage
Basic Setup
- Add an
EnhancedGridMapnode to your scene. - Assign a
MeshLibraryto theEnhancedGridMap. - Use the
EnhancedGridMapdock in the editor to configure grid properties and cell states.
Scripting
You can also interact with the EnhancedGridMap through GDScript. Here's a basic example:
var enhanced_gridmap = $EnhancedGridMap
# Generate a 10x10 grid
enhanced_gridmap.columns = 10
enhanced_gridmap.rows = 10
enhanced_gridmap.generate_grid()
# Find a path from (0,0) to (5,5)
var path = enhanced_gridmap.find_path(Vector2(0, 0), Vector2(5, 5))
Sample Scene and Player Movement
The plugin includes a sample scene that demonstrates how to implement player movement using the EnhancedGridMap. This scene showcases pathfinding and grid-based movement.
Scene Setup
The sample scene (main.tscn) includes:
- An
EnhancedGridMapnode with a pre-configured grid. - A player character
CharacterBody3Dwith a custom script for grid-based movement. - A top-down camera for easy visualization.
Player Movement Script
The player.gd script provides a flexible implementation of grid-based movement using the EnhancedGridMap. Key features include:
- Click-to-move functionality
- Pathfinding using the EnhancedGridMap's A* algorithm
- Smooth movement along the calculated path
- Customizable cell size and offset
- Option for diagonal movement
Usage Example
To use the player movement script in your own scene:
- Add an
EnhancedGridMapto your scene. - Add a
CharacterBody3D(or other suitable node) for your player. - Attach the
player.gdscript to your player node. - Set the
enhanced_gridmap_pathandplayer_pathin the inspector. - Customize other properties like
cell_sizeanduse_diagonal_movementas needed.
Here's a minimal setup example:
extends Node3D
@onready var enhanced_gridmap = $EnhancedGridMap
@onready var player = $Player
func _ready():
player.enhanced_gridmap_path = enhanced_gridmap.get_path()
player.player_path = player.get_path()
player.cell_size = Vector3(2, 2, 2)
player.use_diagonal_movement = true
This setup allows for click-to-move functionality on your EnhancedGridMap, with the player finding and following optimal paths while avoiding non-walkable cells.
API Reference
Properties
columns: int- Number of columns in the gridrows: int- Number of rows in the gridauto_generate: bool- Whether to automatically generate the grid when properties changenormal_items: Array[int]- Array of item indices for normal cells (default: [0])non_walkable_items: Array[int]- Array of item indices for non-walkable cells (default: [4])hover_item: int- Item index for hover state cellsstart_item: int- Item index for pathfinding start cellsend_item: int- Item index for pathfinding end cellsnon_walkable_item: int- Item index for non-walkable cellsdiagonal_movement: bool- Whether to allow diagonal movement in pathfinding
Methods
Grid Management
generate_grid()- Generate the grid based on current propertiesclear_grid()- Clear all cells in the gridrandomize_grid()- Randomize cell types in the gridrandomize_grid_custom(randomize_states: Array)- Randomize cell types based on custom statesfill_grid(item_index: int)- Fill the entire grid with a specific item typevalidate_item_indices()- Validate and ensure all item indices are within valid range
Property Setters
set_columns(value: int)- Set the number of columns and update grid if auto-generate is enabledset_rows(value: int)- Set the number of rows and update grid if auto-generate is enabled- `set_floor
