godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.78k stars 3.07k forks source link

Connecting signal via code by directly accessing it doesn't works in C# #8013

Closed AstrooLabe closed 6 months ago

AstrooLabe commented 12 months ago

Your Godot version: 4.1.1

Issue description: The 3D tutorial tasks us to connect the OnMobSquashed() method to the Squashed signal by directly calling mob.Squashed (the global doc for signals display a similar solution) but Godot throws an error when I try to build the project with this solution.

Had to replace the line mob.Squashed += GetNode<ScoreLabel>("UserInterface/ScoreLabel").OnMobSquashed; by the line mob.Connect("Squashed", new Callable(GetNode<ScoreLabel>("UserInterface/ScoreLabel"), "OnMobSquashed")); to make it works.

URL to the documentation page: https://docs.godotengine.org/en/stable/getting_started/first_3d_game/08.score_and_replay.html https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_signals.html

jynus commented 12 months ago

The syntax is correct, this -for example- works for Godot 4.1.1:

using Godot;
using System;

public partial class Node2D : Godot.Node2D
{
    public override void _Ready()
    {
        var button = GetNode<Button>("Button");
        button.Pressed += OnButtonPressed;
    }

    private void OnButtonPressed()
    {
        GD.Print("Pressed");
    }
}

What you used is the less idiomatic syntax- I wonder if the tutorial doesn't make clear enough to use a delegate and have a concrete ending name for the signal at Jumping and squashing monsters.

Can you show your code declaring the signal? Maybe it is something else, but having the full code will help.

AstrooLabe commented 12 months ago

image image

I've followed the syntax in the documentation and the signal appears correctly in the node inspector. In a similar fashion, using SignalName.Hit instead of "Hit" inside the Emit method raise an error in VS Code but at least it doesn't block the build.

paulloz commented 12 months ago

In a similar fashion, using SignalName.Hit instead of "Hit" inside the Emit method raise an error in VS Code but at least it doesn't block the build.

The error in VSCode is due to the new versions of ms-dotnettools.csharp not working properly when using Roslyn. You need to revert to Omnisharp.

AstrooLabe commented 12 months ago

The error in VSCode is due to the new versions of ms-dotnettools.csharp not working properly when using Roslyn. You need to revert to Omnisharp.

Thanks a lot, it indeed fixed this issue, but also the error with the signal connection as specified in the documentation! Since it's not something specified in the details of the C# for Godot extension, it would make a nice addition.

paulloz commented 12 months ago

We probably need to be more clear about the C# for Godot extension not being needed (or even useful in any amount) with Godot 4.

skyace65 commented 6 months ago

Closing this issue as references to the C# tools for Godot VSCode extension were removed by #8826.