4

I am trying to learn how to use Godot, as I find it easier to learn than Unity, and have been following a tutorial on their official documentation page, I've managed to get to the 'Preparing for Collisions' section, and have created the Hit signal, however, when I linked the nodes together, it created the function, and I wrote the code it said to put into it:

public void OnPlayerBodyEntered(PhysicsBody2D body)
{
    Hide(); // Player disappears after being hit.
    EmitSignal("Hit");
    GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
}

But when I run it and it gives this error:

Feature 'top-level statements' is not available in C# 7.3. Please use language version 9.0 or greater.

I assumed this meant I have to update C#, which I didn't think would be that much of an issue, I tried updating it through the dotnet-sdk thing, but I'm not really sure what it is or how it works, and cannot find any other way of updating it in Godot. I apologise if this is a stupid question, I'm very new to Godot. Thanks in advance for any help.

Lordimass
  • 97
  • 2
  • 6

2 Answers2

6

I am not entirely sure if that's the snippet of code which produces the top-level statement error. Top-level statement errors usually come from, typically Program.cs, or the code file that contains the Main method.

Top-level statements were introduced with C# 9: MSDN - Top-level statements

In order to enable C# 9.0 in your project, you need to edit your .csproj file and add the following:

<PropertyGroup>
   <LangVersion>9.0</LangVersion>
</PropertyGroup>

Alternatively, you can use the following configuration to target the latest C# version, currently 10.0:

<PropertyGroup>
   <LangVersion>latest</LangVersion>
</PropertyGroup>
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Codehack
  • 323
  • 2
  • 13
3

While the language C# is fully supported in Godot, the integration of the IDE is not the best (OK, it is bad integration).

I (...) have created the Hit signal, however, when I linked the nodes together, it created the function, and I wrote the code it said to put into it

The issue is that it created it in the wrong place. Godot adds the handler method at the end of the file. Which is not correct. It should be a method inside the class that shares name with the file. Move the method there.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • Thanks, that was also a problem, but not the one with the error I posted, had to work that one out too, and yeah, I've noticed its not integrated the best, bit of a pain, thanks again for the help – Lordimass Nov 21 '21 at 14:41