0 votes

For my user interface I have developed an approach that allows me to attach the same script file to many nodes. (Example: buttons that toggle visibility of other controls.)

Every time I use the editor to attach a signal it attempts to insert a "helpful" function stub for the signal. Including when one already exists.

Is there a way to disable this code generation? I didn't see anything related in the dialog box for connecting a signal.

Note: I am using C#.

asked Jun 14 in Engine by JimArtificer (699 points)

1 Answer

–2 votes
Best answer

Is there a way to disable this code generation?

No. If you want more freedom, you have to connect a signal from code.

Every time I use the editor to attach a signal it attempts to insert a "helpful" function stub for the signal. Including when one already exists.

The second sentence is not true: if there already is a function with exactly the same name, Godot will connect to it and won't create a new method-stub.

Correction: This is not true when working with GDscript. But apparently is the case for C# (and potentially other scripting languages as well, see my comment below).

answered Jun 15 by njamster (8,990 points)
selected Jun 16 by JimArtificer

Your statement is incorrect. The problem may have something to do with using C#, that the script class is in a namespace, or some other factor, but it is easy to reproduce.

Sorry, I somehow missed that you're using C#. I corrected my answer.

I'm afraid that doesn't change much though: there is no way to disable the generation of signal-callbacks! Under Editor Settings > Text Editor > Tools you can set Create Signal Callbacks to false. Judging by the name, I'd guess it's supposed to do what you want. However, there is absolutely no documentation about it and the setting seems to be never used outside of the EdtiorSettings - so it doesn't work (neither in GDscript nor in C#). Your problem seems to be related to this issue, as connecting a signal will simply append the function declaration returned by make_function to your .cs-file. Consequentially connecting it twice will append the function twice as well.

Given that, my original answer still holds: if you want to avoid that, you'll have to connect the signal from code, which doesn't involve make_function.

Thanks, I appreciate the follow-up information and have reported the bug. I am going to mark your answer as correct, even though I disagree with the assertion you made about 'freedom'. The question is really just about an editor workflow.

I disagree with the assertion you made about 'freedom'.

When you connect a signal via code, you're free to choose not to create the callback and instead encounter an error when running your code. Doing the exact same thing in the editor will (as of now) force you to create a callback (even though your are free to remove it again). I think that is a sane default, as it prevents beginners from running into an rather obvious error (once you grow accustomed to the use of signals).

It's worth noting that connecting signals via the editor is limited in other ways as well: for example you cannot provide extra arguments to the callback other than by code. That's not to say I like it that way, but connecting a signal from code is undeniably more powerful, expressive and limitless than doing it via the editor as of now.

The question is really just about an editor workflow.

To clarify: I fully support an option to opt-out of the automatic stub-creation! I'd say a toggle (defaulting to true, unless specified otherwise via the - currently not working - flag in the editor settings) in the "Advanced" section of the "Connect..."-dialog would be optimal, benefiting both beginners and advanced users of the engine.

The advanced dialog option lets you add extra arguments. It may be helpful to view the editor as creating declarative code and the scripts as providing additional imperative code.

The advanced dialog option lets you add extra arguments.

True. My sentence was poorly formulated. What I meant is that you're limited to some basic variable types when adding extra arguments to a callback from the editor GUI. So something like this currently only works from code:

$Button1.connect("pressed", self, "_on_Button_pressed", [$Button1])
$Button2.connect("pressed", self, "_on_Button_pressed", [$Button2])

It may be helpful to view the editor as creating declarative code and the scripts as providing additional imperative code.

Again: I'm opposing that view in any way! I'm just stating how things currently are. I never said that it is a good thing that you cannot do those things from the GUI.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.