To access the target TileMap inside the area script called "areacheck.gd", you need to establish a reference to that node from within the script. From the given scene code, the TileMap is a sibling node of the "areacheck" node. It looks like you've already written the code to do this with the following line:
gdscript
@onready var tilemap : TileMap = $root/target
This line fetches the "target" node and casts it to a TileMap, assuming that the script is at the top level of the scene tree. However, if areacheck is a child of the root node and target is a sibling, you should adjust the line as follows:
User
target:<TileMap#2264555070757>
res://addons/sprite_painter/src/floating/type_context/context_tileset.gd:15 - Stack overflow. Check for infinite recursion in your script.
editor/editor_node.cpp:8224 - Condition "plugins_list.has(p_plugin)" is true.
editor/editor_node.cpp:8224 - Condition "plugins_list.has(p_plugin)" is true.
editor/editor_node.cpp:8224 - Condition "plugins_list.has(p_plugin)" is true.
extends Node2D
var detection_radius = 100.0 # How far from the character to check
var grid_size = 10 # Define the grid size for your ‘square-cast’
var collision_mask = 1 # The collision mask we’re interested in
@onready var tilemap : TileMap = get_parent().get_node("target")
func _ready():
# Ensuring this node has a method for checking the physics state
assert(self.is_inside_tree(), "Node must be inside the tree to access the DirectSpaceState")
print(tilemap)
Assistant
User
Invalid set index 'collision_layer' (on base: 'TileMap') with value of type 'int'.
Assistant
The error message "Invalid set index 'collision_layer' (on base: 'TileMap') with value of type 'int'." indicates that you are trying to set a property called collision_layer on a TileMap object, but that property does not exist on the TileMap class in Godot.
In Godot, TileMap nodes don't have collision_layer or collision_mask properties. Instead, these properties belong to -based nodes such as , , , etc., which can collide with other physics bodies. nodes have and concepts for which you define collision shapes within the resource.
var detection_radius = 100.0 # How far from the character to check
var grid_size = 10 # Define the grid size for your ‘square-cast’
var collision_mask = 1 # The collision mask we’re interested in
@onready var tilemap : TileMap = $root/target
gdscript
@onready var tilemap : TileMap = get_parent().get_node("target")
get_parent() will fetch the parent of the areacheck node and get_node("target") will get the sibling node named "target".
In some cases, especially if areacheck were deeply nested or the structure of the scene could change, it's safer to use the get_node_or_null() method, which returns null if the specified node path does not exist:
gdscript
@onready var tilemap : TileMap = get_node_or_null("/root/NameOfTheRootNode/target")
Replace NameOfTheRootNode with the actual name of your root node.
Make sure you replace this code inside the actual script for "areacheck" so that it correctly references the "target" TileMap node. Then you can access the target TileMap within any function of the "areacheck" script like this:
gdscript
func example_function():
# Example code to access a tile
var tile_id = tilemap.get_cellv(Vector2(1, 1))
# Do something with the tile_id
If you encounter any issues, make sure that the node paths are correct according to the structure of your scene tree, and that the "target" node is indeed a TileMap.
code
if tilemap:
tilemap.collision_layer = 1
tilemap.collision_mask = 1
func remove_duplicates(array):
var unique_array = []
for item in array:
if item not in unique_array:
unique_array.append(item)
return unique_array
func detect_surroundings():
var space_state = get_world_2d().direct_space_state
var detected_objects = []
var center = global_position
var exclude = [self, tilemap]
code
for x in range(center.x - detection_radius, center.x + detection_radius, grid_size):
for y in range(center.y - detection_radius, center.y + detection_radius, grid_size):
var point = Vector2(x, y)
var point_parameters = PhysicsPointQueryParameters2D.new()
point_parameters.position = point
point_parameters.exclude = exclude
var result = space_state.intersect_point(point_parameters)
for collision in result:
if collision.collider:
detected_objects.append(collision.collider)
return remove_duplicates(detected_objects)
func _process(delta):
# Just for example, we’ll call our detection function every frame
var surroundings = detect_surroundings()
#for obj in surroundings:
# print(str(obj) + " at position " + str(obj.global_position))
# Now you have a list of objects in your surroundings to work with
# You can use this information to dynamically generate your platform view
The error messages you provided seem to suggest there's a problem with your TileMap and possibly an issue with a plugin (context_tileset.gd). The error message "Stack overflow. Check for infinite recursion in your script." is indicative of a method that is calling itself indefinitely, or two or more methods are calling each other in a cycle, causing the program to run until it runs out of call stack space.
Given that you included print(tilemap) right after the @onready variable assignment, if it's inside _ready() or another method that might be causing the issue, you may want to comment it out or move it to a safer place to check.
As for the other error messages about editor/editor_node.cpp:8224, these seem to be issues with editor plugins, possibly ones that interact with scenes or nodes in the editor. It's not clear how this relates to your script without more details on what the plugin does or if it modifies TileMap behavior, but it might be conflicting with your script execution or scene setup.
To troubleshoot your issue, here's what I recommend:
Comment out the print(tilemap) line in case it somehow is being triggered more than once or at a phase of the engine's execution where it shouldn't be.
Check any plugin or custom script that deals with the TileMap (context_tileset.gd for example) to make sure there are no conflicting logic or infinite loops/recursion occurring.
If you have any editor-related plugins enabled, try disabling them temporarily to see if the issues go away. If they do, then it's an interaction with those plugins causing the issues.
Revisit context_tileset.gd line 15, as per the error message, to see what's happening there. It might provide some context as to why there's a stack overflow issue.
If you have any custom signals or methods that are triggered by _ready() or _process() that interact with the tilemap node (like if you're trying to change a property every frame or reacting to property changes), ensure there's not an infinite loop caused by those callbacks. Make sure any connections between signals and methods don't lead to an infinite callback loop.
If you're able to fix the recursion issue and the plugin errors and you still cannot access the tilemap as expected, ensure that the scene tree structure at runtime matches what you expect. The code for accessing the tilemap seems correct for the structure you've outlined, however, runtime conditions such as scene instantiation, deferred executions, or script reloads may not reflect the structure you're assuming.
PhysicsBody2D
StaticBody2D
RigidBody2D
KinematicBody2D
TileMap
cell
tile
TileSet
If you want to set collision layers and masks for the tiles, you need to define them in the TileSet itself. Each tile in the TileSet can have individual collision shapes with their own layers and masks. Here’s how to set it up:
Open the TileSet resource in the Godot editor.
For each tile, you can add one or more collision shapes.
Select a collision shape that you've added to a tile.
In the Inspector, you'll find properties for the collision layer and mask for that specific collision shape.
You cannot set a global collision layer or mask for the entire TileMap node via script because that concept does not exist for TileMap. Collision properties are handled on a per-tile basis within the TileSet.
Remove or comment out the following lines in your script:
gdscript
if tilemap:
tilemap.collision_layer = 1 # Remove or comment out this line
tilemap.collision_mask = 1 # Remove or comment out this line
These lines are trying to set properties that the TileMap node type does not have, which is the cause of your error. Instead, set up your collision layers and masks within the TileSet resource as needed. If you're trying to define interaction rules like which layers can collide with which others, those should be set up in the relevant PhysicsBody2D-based nodes that interact with your TileMap.