C# Signal Typecasting of Arguments

Godot Version

4.2.2

Question

I am currently trying to catch a signal in my Area3D Node. I therefore created a method in my Area3D called public void _on_body_entered(Node3D body) as suggested by the documentation. I linked the signal of the Area3D body_entered to my internal on_body_entered method by using the GUI. Now however I am clueless why the typecast for the signal doesnt seem to work.

Whenever my player enters the area the signal gets correctly redirected to my method. However the typecast from RigidBody3D to Node3D doesnt seem to work.
Isn’t RigidBody3D automatically upcasted to its base class Node3D?

Method recieveing the signal:

	public void _on_body_entered(Node3D body)
	{
		GD.Print($"Body entered: {body.Name}");
	}

Error when player RigidBody3d enters the Area:

E 0:00:20:0783   object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object): System.InvalidCastException: Unable to cast object of type 'Godot.RigidBody3D' to type 'Node3D'.

Player Scene:
image

UPDATE:

Changing the method from

private void _on_body_entered(Node3D body)

to

private void _on_body_entered(Godot.Node3D body)

solved the issue. I have still no clue why Node3D namespace isnt clear. I have no other script with that name which could cause this issue.

Full Script:

using Godot;
using System;

public partial class DeathBarrier : Area3D
{
	private void _on_body_entered(Godot.Node3D body)
	{
		GD.Print($"Body entered: {body.Name}");
	}
}

This is just a small tip but in my opinion, if you use C#, you’re much better off using C# events built into Godot rather than signals. They’re a lot more friendly when working with C#, much more readable as well, and easy to deal with.

Hi! Thanks for the input.
As a small team with a game designer + programmer we went this route to also enable the designer to link signals.

I dont really feel comfortable letting sombody with no programming experience handle .cs files.

Never the less thanks for the tip!

1 Like

And my previous post contained the answer. The Game designer accidently created a blank Node3D and attached a script to it. This created a Node3D.cs file he immediatly closed (and saved).

This created the following problem:
All refrences with out the Godot namespace declared referenced this Node3D script now instead of the actual Godot.Node3D. Deleting that empty script fixed all issues.

Is there a setting which lets me add a default Pre / Suffix when a script name is automatically created? I want to make sure that this does not happen again.

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