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.
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)
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.
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)
PhysicsShapeQueryParameters2D: 0 shapes overlap the circleWhy 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
- renames_map_3_to_4.cpp line 1569, Physics2DShapeQueryParameters to PhysicsShapeQueryParameters2D
- gdscript_analyzer.cpp line 4687, the error with the rename hint
- gdscript_analyzer.cpp line 3210, where the hint looks up Godot 3 names
- PhysicsShapeQueryParameters2D class reference
- PhysicsDirectSpaceState2D class reference
- World2D.direct_space_state, access from _physics_process
- ProjectSettings: debug/gdscript/warnings/renamed_in_godot_4_hint
Run on Godot 4.7.2, .