Godot 3 to 4 rename

Physics2DShapeQueryParameters in Godot 4: it is PhysicsShapeQueryParameters2D now

Physics2DShapeQueryParameters is called PhysicsShapeQueryParameters2D in Godot 4: the 2D moved to the end of the name, as it did for Physics2DDirectSpaceState and Physics2DServer. A Godot 3 script that still uses the old name fails to parse, and the error names the new class.

What Godot prints

A Godot 3 shape query in a Godot 4 project: a Node2D that builds Physics2DShapeQueryParameters for a circle.

repro.gd
extends Node2D

func _physics_process(_delta):
	var shape = CircleShape2D.new()
	shape.radius = 16.0
	var query = Physics2DShapeQueryParameters.new()
	query.set_shape(shape)
	query.transform = Transform2D(0.0, Vector2(100, 100))
	var hits = get_world_2d().direct_space_state.intersect_shape(query)
	print(hits.size())
	set_physics_process(false)
Output, Godot 4.7.2
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

SCRIPT ERROR: Parse Error: Identifier "Physics2DShapeQueryParameters" not declared in the current scope. Did you mean to use "PhysicsShapeQueryParameters2D"?
   at: GDScript::reload (res://answers/physics2dshapequeryparameters-in-godot-4/repro.gd:6)
ERROR: Failed to load script "res://answers/physics2dshapequeryparameters-in-godot-4/repro.gd" with error "Parse error".
   at: load (modules/gdscript/gdscript_resource_format.cpp:46)

The fix

The same query with PhysicsShapeQueryParameters2D, run once from _physics_process, where the space state can be read.

fix.gd
extends Node2D

func _physics_process(_delta: float) -> void:
	var shape := CircleShape2D.new()
	shape.radius = 16.0
	var query := PhysicsShapeQueryParameters2D.new()
	query.shape = shape
	query.transform = Transform2D(0.0, Vector2(100, 100))
	var hits := get_world_2d().direct_space_state.intersect_shape(query)
	print(query.get_class(), ": ", hits.size(), " shapes overlap the circle")
	set_physics_process(false)
Output, Godot 4.7.2
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org

PhysicsShapeQueryParameters2D: 0 shapes overlap the circle

Why it happens

GDScript's analyzer resolves every identifier when the script loads. Physics2DShapeQueryParameters is not a class in Godot 4, so the script fails to parse and Godot does not load it: the run above printed no hit count.

The editor build of Godot looks an unknown identifier up in its own Godot 3 to 4 rename tables and adds the new name to the error: Did you mean to use "PhysicsShapeQueryParameters2D"? The project setting debug/gdscript/warnings/renamed_in_godot_4_hint turns that hint off; it is on by default.

PhysicsShapeQueryParameters2D holds the query's shape, transform, motion, margin, collision_mask and exclude list, and whether it collides with bodies and areas. PhysicsDirectSpaceState2D's intersect_shape, cast_motion, collide_shape and get_rest_info all take it.

World2D.direct_space_state is the space those methods run on. When physics runs on its own thread, the class reference limits access to it to _physics_process on the main thread, which is where the fix reads it.

Sources

Run on Godot 4.7.2, .

Related