srx734
September 20, 2024, 2:45am
1
Godot Version
Godot 4.3 Mono
Question
Hello, new to Godot. I have a script that inherit from Node2D, that is attached to a Area2D node. The script has a exported Area2D field so I assign the node to it, and at runtime I get a invalid cast exception. Why would this happen? What does attaching a script to a node actually do under the hood?
srx734
September 20, 2024, 4:14am
3
This is the full exception info, I’m listening to the Area2D’s BodyEntered event:
E 0:00:00:0724 object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object): System.InvalidCastException: Unable to cast object of type ‘DodgeTheCreeps.Games.Player’ to type ‘Godot.Area2D’.
<C++ Error> System.InvalidCastException
<C++ Source> :0 @ object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object)
:0 @ object System.Runtime.CompilerServices.CastHelpers.ChkCastAny(System.Void*, object)
VariantUtils.generic.cs:387 @ T Godot.NativeInterop.VariantUtils.ConvertTo(Godot.NativeInterop.godot_variant&)
DodgeTheCreeps.Games.Player_ScriptProperties.generated.cs:39 @ bool DodgeTheCreeps.Games.Player.SetGodotClassPropertyValue(Godot.NativeInterop.godot_string_name&, Godot.NativeInterop.godot_variant&)
CSharpInstanceBridge.cs:57 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Set(nint, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_variant*)
paulloz
September 20, 2024, 9:00am
5
Short answer: yes, if you’re attaching a script to an Area2D
, then the class in the script needs to extend Area2D
.