Running this GDScript produces an error, {ERROR: res://MapGen.gd:8 - Trying to get a return value of a method that returns "void"}. The script functions entirely as intended, as far as I can tell, so the error message is the only visible problem.
@tool
#extends GridMap
extends EditorScript
func Main(Self):
print(str(Self.get_meta('Map_Data')))
var X = Self.get_meta('Map_Data')
Self.set_meta('Map_Data', X.append([Vector3i(2,0,0),1]))
#Self.set_cell_item(Vector3i(0,0,0), 1)
The error occurs because the append() method modifies the array in place and returns void (nothing). You're trying to use this return value as the second parameter for set_meta(), which expects actual data. Here's the fix:
This fixes the "void return value" error while preserving all intended functionality.
User
Why does the script work at all?
Assistant
The script "works" (appears functional) due to reference semantics in GDScript, combined with error suppression in tool scripts. Here's why:
1. Arrays are Passed by Reference
When you do var X = Self.get_meta('Map_Data'), you're getting a reference to the array stored in metadata, not a copy.
X.append()modifies the original array directly through this reference, even though it returns void.
2. What Actually Happens
# Original (error version):
Self.set_meta('Map_Data', X.append([Vector3i(2,0,0),1]))
X.append(...) modifies the array in-place (successful change)
append() returns void → equivalent to null
Self.set_meta('Map_Data', null) executes (sets metadata to null)
3. Why You See "Working" Behavior
Before the error:
Your print() shows the original array (unmodified).
After the error:
The array was already modified by X.append() (step 1)
The subsequent set_meta(..., null) fails silently in @tool context
Your array changes appear to persist because:
The array reference still exists in memory
Other parts of your code likely access the same array reference
4. The Hidden Danger
While the array modification "sticks", you've corrupted your metadata by setting it to null. This will cause bugs later when you try to access 'Map_Data' again and get null instead of your array.
Visual Representation of the Problem:
func Main(Self):
var original_array = Self.get_meta('Map_Data') # e.g. [A, B, C]
print(original_array) # Prints [A, B, C]
# PROBLEM LINE:
original_array.append([D]) # Array becomes [A, B, C, D] ✅
Self.set_meta('Map_Data', null) # ❌ Metadata now = null
# Later code trying to use the metadata:
var data = Self.get_meta('Map_Data') # Now returns NULL! 💥
The Fix (Recap):
var X = Self.get_meta('Map_Data')
X.append([Vector3i(2,0,0), 1]) # Modify via reference
Self.set_meta('Map_Data', X) # Explicitly update metadata
This ensures:
The array is modified
Metadata correctly references the updated array
No void-return error occurs
Key Insight: The script appeared functional due to reference manipulation, but was silently corrupting your metadata storage. Always update metadata explicitly after modifying reference types (Arrays, Dictionaries).
User
How I am supposed to obtain a copy of an array in GDScript? Does GDScript follow the same rules as python in this regard?
Assistant
Array Copying in GDScript vs Python
GDScript handles array copying differently than Python. Here's a clear comparison:
GDScript Array Copying
Arrays are passed by reference by default
var a = [1, 2, 3]
var b = a # b references the same array as a
b[0] = 99
print(a) # Output: [99, 2, 3] (modified through reference)
To create a copy, use duplicate():
var a = [1, 2, 3]
var b = a.duplicate() # Creates a shallow copy
b[0] = 99
print(a) # Output: [1, 2, 3] (original unchanged)
print(b) # Output: [99, 2, 3]
Shallow copy limitation:
var original = [1, [2, 3], 4]
var copy = original.duplicate()
# Modifying top-level items:
copy[0] = 99 # Only affects copy
print(original) # [1, [2, 3], 4]
# Modifying nested items:
copy[1][0] = 88 # Affects BOTH arrays!
print(original) # [1, [88, 3], 4]
Python Array Copying
Lists are also passed by reference by default:
a = [1, 2, 3]
b = a # Reference assignment
b[0] = 99
print(a) # [99, 2, 3]
Shallow copy options:
# Slice copy
b = a[:]
# copy() method
b = a.copy()
# copy module
import copy
b = copy.copy(a)
Deep copy for nested structures:
import copy
a = [1, [2, 3], 4]
b = copy.deepcopy(a) # Full independent copy
b[1][0] = 99
print(a) # [1, [2, 3], 4] (unchanged)
Key Differences
Feature
GDScript
Python
Default behavior
Reference assignment
Reference assignment
Shallow copy method
duplicate()
copy(), [:], copy.copy()
Deep copy method
Manual implementation
copy.deepcopy()
Built-in deep copy
❌ Not available
✅ Available
Deep Copy Workaround in GDScript
User
Okay, so this works:
@tool
#extends GridMap
extends EditorScript
func Main(Self):
print(str(Self.get_meta('Map_Data')))
var X = Self.get_meta('Map_Data').duplicate(true)
X.append([Vector3i(2,0,0),1])
Self.set_meta('Map_Data', X)
func Main(Self):
print(str(Self.get_meta('Map_Data')))
var X = Self.get_meta('Map_Data').duplicate(true) # Create deep copy
X.append([Vector3i(2,0,0),1]) # Modify the COPY
Self.set_meta('Map_Data', X) # Explicitly save modified COPY
This works correctly because:
You create a deep copy of the array
Modify the copy
Explicitly save the modified copy back to metadata
Result: Clean state management with no side effects
2. Your Alternative Script (Problematic)
func Main(Self):
print(str(Self.get_meta('Map_Data')))
var X = Self.get_meta('Map_Data')
X.append([Vector3i(2,0,0),1]) # Modify original array
# No set_meta() call!
This will appear to "work" but has serious issues:
What Bad Things Happen?
1. Metadata Corruption
The array in metadata is modified directly via reference
But Godot doesn't know it was modified (no explicit set)
Result: Undo/redo systems won't work correctly
Risk: Data might not save properly in scene files
2. Inconsistent Editor Behavior
Since this is an @tool script running in the editor:
The editor UI might not refresh to show changes
Scene modifications might not be marked as "dirty" (asterisk in scene tab)
Result: You might lose changes if you close the scene without saving
3. Hidden Data Sharing
var original = Self.get_meta('Map_Data')
var X = original # Both point to SAME array
X.append(new_item) # Modifies BOTH original and X
# Later in code...
print(Self.get_meta('Map_Data')) # Shows modified array
print(original) # Also shows modified array
Risk: Other parts of code might get unexpected modified data
Debugging nightmare: Changes appear "magical" with no obvious source
4. Thread Safety Issues
If accessed from multiple places (even in single-threaded GDscript):
No clear "owner" of the data
Modification order becomes unpredictable
User
Is { append() } accurate for this use case? Are there any alternatives and if so, which alternative is most standard GDScript?
Assistant
Array Appending in GDScript: Best Practices
append() is absolutely appropriate and standard for adding a single element to an array in GDScript. It's the most common and efficient way to add items to the end of an array.
Why append() is Perfect Here
Correct Operation: You're adding a single element to the array
Clear Intent: The function name clearly communicates what you're doing
Efficiency: append() operates in O(1) time for most cases
Standard Practice: This is the idiomatic way to add elements in GDScript
Alternative Methods (When to Use Them)
Method
Use Case
Example
Why Not Better Here
push_back()
Same as append()
X.push_back([Vector3i(2,0,0),1])
Just an alias for append()
+= operator
Adding multiple elements
X += [[item1], [item2]]
Creates a new array (less efficient)
insert()
Adding at specific position
X.insert(0, item)
You're adding to the end
Direct index assignment
When you know the index
X[X.size()] = item
Less readable, requires size calculation
Best Practice for Your Specific Case
func Main(Self):
# Get reference to the array
var map_data = Self.get_meta('Map_Data')
# Append new element
map_data.append([Vector3i(2,0,0), 1])
# Update metadata to notify engine
Self.set_meta('Map_Data', map_data)
Why Your Minimal Version is Problematic
func Main(Self):
print(str(Self.get_meta('Map_Data')))
var X = Self.get_meta('Map_Data')
X.append([Vector3i(2,0,0),1]) # Only this line
What happens:
You get a reference to the array stored in metadata
You modify the array via that reference
But you never notify Godot about the change
The consequences:
Undo/redo breaks: Changes won't be tracked properly
User
Is { append() } accurate for this use case? Are there any alternatives and if so, which alternative is most standard GDScript?
Assistant
Using append() in GDScript Arrays
append() is absolutely accurate and standard for your use case of adding a single element to the end of an array. It's the most common and efficient way to add elements to arrays in GDScript.
Why append() is appropriate:
Standard Practice: It's the primary method for adding elements to arrays in GDScript
Efficiency: Operates in O(1) time complexity for most cases
Clarity: Clearly communicates the intent to add an item to the end
Native Support: Works with all array types in Godot
Alternative Methods:
Method
Use Case
Example
Recommendation
append()
Adding single item to end
arr.append(item)
✅ Best for your case
push_back()
Alias for append()
arr.push_back(item)
✅ Same as append
+= operator
Adding multiple items
arr += [item1, item2]
Good for multiple items
push_front()
Adding to beginning
arr.push_front(item)
❌ Less efficient
insert()
Insert at specific position
arr.insert(2, item)
When position matters
Array Literal
Creating new arrays
var new = arr + [item]
Creates new copy
For Your Specific Use Case:
# Your current approach - PERFECTLY FINE
X.append([Vector3i(2,0,0), 1])
# Alternative using += operator (slightly less efficient)
X += [[Vector3i(2,0,0), 1]]
Important Considerations:
Nested Array Structure: You're appending an array [Vector3i, int] to another array. This is valid, but consider if a dictionary might be clearer:
For nested structures, implement your own deep copy function:
func deep_copy(value):
if typeof(value) == TYPE_ARRAY:
var new_array = []
for item in value:
new_array.append(deep_copy(item))
return new_array
elif typeof(value) == TYPE_DICTIONARY:
var new_dict = {}
for key in value:
new_dict[key] = deep_copy(value[key])
return new_dict
else:
# Built-in types are value types (copied automatically)
return value
# Usage:
var nested = [1, [2, 3], {a = 4}]
var deep_copy = deep_copy(nested)
When to Use Copies in Your Script
In your original script, you don't need a copy because:
You want to modify the original array stored in metadata
You're immediately setting it back after modification
The duplicate() method would be inefficient for this use case
Your fixed code is correct:
var X = Self.get_meta('Map_Data')
X.append([Vector3i(2,0,0), 1]) # Modify the original array
Self.set_meta('Map_Data', X) # Update metadata reference
Only create copies when:
You need to preserve the original state
You want to manipulate data without affecting the original
Working with nested structures where you need full isolation
Result: Race conditions and inconsistent state
5. Performance Illusion
Seems "faster" because you avoid:
duplicate() call (copy operation)
set_meta() call (notification system)
But in reality:
You lose editor integration features
Risk of data corruption outweighs micro-optimizations
When Would This "Work"?
It might appear functional if:
You're only running it once
You immediately use the modified data
You never use undo/redo
You manually save the scene after every run
No other code accesses the metadata simultaneously
But even then, it's fragile and unpredictable. The working version with explicit set_meta() is:
Stick with your first approach, but consider optimizing:
func Main(Self):
var map_data = Self.get_meta('Map_Data')
# Only duplicate if needed for modification
var modified = map_data.duplicate(true) # Deep copy
modified.append([Vector3i(2,0,0), 1])
# Only set if actually changed
if modified != map_data:
Self.set_meta('Map_Data', modified)
This gives you:
Explicit state management
Minimal copying (only when needed)
Change detection to avoid unnecessary sets
Full editor integration
Save issues: Scene might not be marked as "dirty" (modified)
No change notification: Dependent systems won't update
Editor glitches: Inspector might not show current values
Data loss risk: Changes might not persist between sessions
The Golden Rule for Editor Scripts
Always explicitly set modified properties/metadata even if you're modifying by reference. This ensures:
The engine knows about changes
Undo history is maintained
The scene is properly marked as modified
All dependent systems are notified
// GOOD - Engine knows about changes
map_data.append(item)
Self.set_meta('Map_Data', map_data)
// BAD - Engine might miss changes
map_data.append(item) // No set_meta call
Final Recommendation
Stick with append() + set_meta() pattern:
func Main(Self):
var map_data = Self.get_meta('Map_Data')
map_data.append([Vector3i(2,0,0), 1])
Self.set_meta('Map_Data', map_data)
This is:
✅ Most efficient
✅ Most readable
✅ Standard GDScript practice
✅ Editor-safe
✅ Undo/redo compatible
# Alternative structure - may be more readable
X.append({
"position": Vector3i(2, 0, 0),
"tile_id": 1
})
Performance: For large arrays (10,000+ elements), appending is still efficient, but building arrays piecemeal might be slower than:
# Pre-allocate if you know final size
var new_items = []
new_items.resize(10000)
Type Safety: Godot 4.x has typed arrays. Consider using them:
# Define a custom type if you have a specific structure
class_name MapTile
var position: Vector3i
var tile_id: int
# Then create and append
var new_tile = MapTile.new()
new_tile.position = Vector3i(2,0,0)
new_tile.tile_id = 1
X.append(new_tile)
Recommendation:
Stick with append() - it's the most standard, efficient, and readable approach for adding single elements to arrays in GDScript. Your current implementation is perfectly valid.
The only improvement I'd suggest is adding type hints for clarity: