I'm trying to write a GDScript (using Godot 4) function that creates a new component at a specific
Vector2 position. This works quite well with the following code:
func _create_thing(at_position): var component = load("res://thing.tscn").instantiate() as Node2D component.position = at_position %PlayfieldNode2D.add_child(component)
Now I'm trying to restrict where players can create something. I want to define a polygon shape in the editor to determine the area where this is allowed. My thought was to create an
Area2D node with a
CollisionPolygon2D shape and then do something like this:
if %AllowedArea2D.contains_point(at_position): # creation logic here
This is pseudo-code, because I found no
contains_point(...) or similar method in the Area2D docs. I looked through the docs of other related Node types such as the
Polygon2D and several others, but found no solution yet.
Bottom line: how can I define a polygon shape in Godot 4 and later check if a specific
Vector2 is inside that polygon?