godotgodot4

Why is intersect_ray saying i have too many arguments in godot 4.2.2?


I'm a beginner at Godot, I'm currently playing around with hitscan weapons etc., and using intersect_ray()

While trying to write code using Intersect_ray() i keep geting an error message "Too many arguments for "intersect_ray()" call. Expected at most 1 but received 2." i don't know what i did wrong? Here's the code: var collision = bullet.intersect_ray(muzle.transform.origin, aimcast.get_collision_point

if it helps, im using a tutorial: https://www.youtube.com/watch?v=4jbfIN4t83k&list=PLJJ-tyPiN1L_68DjYLfn7c1Yq8Kx6NWPw&index=7


Solution

  • welcome to SO!

    The answer to your specific problem is that your tutorial is old and many APIs changed from Godot 3 to Godot 4. The intersect_ray() method now takes a single argument of a special type that holds all the necessary parameters. So you need to create an instance of this type, PhysicsRayQueryParameters3D, and then pass it to the method.

    See this reddit thread where someone asked the exact same question: https://www.reddit.com/r/godot/comments/u0fboh/intersect_ray_too_many_arguments_godot_40/

    Also see the Godot docs on raycasting: https://docs.godotengine.org/en/stable/tutorials/physics/ray-casting.html

    The general way to figure out such an error message is to look at the method signature. It will tell you what kind of arguments it expects and how many. To do this, CTRL-click on the method in the editor or select the method and hit F12 (I think). You will then be taken to the docs for that method.