Get_world_3d() returns a null value

Godot Version

4.3.stable

Question

I’m making a “traceline” function for motion, similar to Quake’s. It was working normally, but when I started using it for a “categorize_position” function inside of the Player’s code, it crashes my game. The categorize_position() function is called in physics process, which might be an issue.

The error I get is "Cannot call method 'get_direct_space_state' on a null value.", with another one being "Condition "!is_inside_world()" is true. Returning: Ref<World3D>".

Here’s a portion of the traceline code:

var fraction: float
var endpos: Vector3

func traceline(v1: Vector3, v2: Vector3, forent, shape: Shape3D):
	var space := get_world_3d().get_direct_space_state()
	
	var query := PhysicsShapeQueryParameters3D.new()
	query.shape = shape
	query.transform.origin = v1
	query.exclude = [forent]
	
	var result = space.cast_motion(query)
	if result:
		fraction = result[0]
		endpos = v1 + ((v2 - v1) * fraction) # I don't know if this works, check later
	else:
		fraction = 1
		endpos = v2
		return

And here’s a portion of the categorize_position function.

func categorize_position():
	var point := global_position + Vector3.DOWN * 0.1
	
	var trace := Trace.new()
	trace.traceline(global_position, point, true, self, col.shape)
	
	if trace.fraction == 1:
		state = FALLING
	else:
		state = GROUNDED

Any help is appreciated. Thanks!

Edit: Forgot to mention the “fixes” I’ve tried, which didn’t work.
1: Tried to turn off physics process for a bit with set_physics_process() in the ready function before turning it back on, changed nothing.
2: Tried checking if the 3d world was null in the traceline code, then returned if it was (if get_world_3d() == null: return). This just made the console spam the second error i mentioned.

Managed to find a fix! Instead of getting the World3D straight from the Trace node, I instead get it from the forent variable!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.