godot: New signal methods in C# are declared outside of its class, making the script invalid

Bugsquad edit: This long standing issue is still valid as of Godot 4.2. It doesn’t need further confirmation, but feel free to 👍 the OP to confirm it affects you. Any help welcome to solve this, it’s something the .NET team is having difficulty fixing properly, but it’s recognized as a major usability issue.


Godot version: 3.0.3 rc1 Mono Build

Issue description: When using the editor to connect a signal to a C# script, the new method declaration is added to the end of the file, and outside of the class. It should instead add the new method just before the final closing brace.

Steps to reproduce:

  1. Create a button node.
  2. Attach a new C# script to this node.
  3. Select the button in the editor, choose Node -> Signals
  4. Select the “pressed()” signal and click connect.
  5. Choose the same button as the “Node to Connect.” Leave everything default and hit connect.
  6. Notice that the new method is created outside of the class.

Example:

using Godot;
using System;

public class btn_move : Button
{
    public override void _Ready()
    {
    }
} //End of class


private void _on_Button_pressed()
{
    // Replace with function body
}

This is a low priority issue as the easy workaround is to just move the closing brace of the class, but this creates invalid code and could be confusing.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 49
  • Comments: 16 (5 by maintainers)

Most upvoted comments

This is still in an issue in 4.2.1!

I personally prefer the editor to do nothing to the C# code file, but save the generated code snip to the clip board. I can then paste the function signature to where I want, or do nothing if the method already exists. Hope there can be some option to enable that.

@BenBausch You can either connect the signal by code (Object.connect), or uncheck Make Function in the signal connection dialog.

Alternatively, just cut and paste the code snippet that was erroneously added at the end of the file and move it to the right place.

I use Rider as external editor. In this case, the code is not generated at all. Even not below the class.

This depends on #12908 first before a fix can be added for C#.

It would be nice to have a switcher to just dont generate the function at all.

There used to be one in Godot 3.1 and earlier, but it seems it was removed in 3.2 when redesigning this dialog:

image


But for the record, this was “fixed” in 4.3 by disabling callback generation for C# in #87952.

I’m keeping this issue open for now as it’s a common problem users run into, but we can close it once 4.3 is released.

It’s not a perfect solution of course as it still requires users to write the C# callback manually in their class, but it’s proving pretty difficult to find a way to insert code in the C# class at the right location, that would support external editors too.

Following the workaround(s) detailed here does not solve the issue. I can produce the function outside of the class. However, after the function is dragged into the class, the signal fails to call the function.