0 votes

Hi folks:

I have a code fragment where I need to connect and disconnect a signal with parameters.

After read https://docs.godotengine.org/en/latest/classes/class_object.html#class-object-method-connect I found that the preferred way to do this is using the next sintax:

player.Hit.Connect(OnPlayerHit, new Godot.Collections.Array {"sword", 100 });

My code look like this:

private void HealthCharacter(Node body)
{
    if (body is Character)
    {
        Character character = body as Character;
        character.IsInvulnerable = true;
        // if (Timer.IsConnected())
        // {
        Timer.Timeout.Connect(OnDoHealth, new Godot.Collections.Array { character });
        // }
        Timer.Start();
    }
}

But this do not compile throwing the next error:

 Health.cs(38,19): El evento 'Timer.Timeout' solo puede aparecer a la izquierda de += o -=.

This translate to:

The 'Timer.Timeout' event can only appear to the left of += or -=.

So I really don't know how could I connect and disconnect signals with parameters on the new Godot

Godot version 4.0-beta10_mono_win64
in Engine by (18 points)

1 Answer

0 votes

Hey PowerPotato!

The new Godot 4 C# Syntax to Connect Signals is

private void HealthCharacter(Node body)
{
    if (body is Character)
    {
        Character character = body as Character;
        character.IsInvulnerable = true;
        // if (Timer.IsConnected())
        // {
                Timer.Timeout **+=** OnDoHealth;
        // }
        Timer.Start();
    }
}
by (14 points)

Yes but with that syntax we are no able to pass arguments. I know we can do something like this

Timer.Timeout += () => OnDoHealth(character);

But in that case I don't, know how to disconnect, because you can not do somithing like this after that:

Timer.Timeout -= () => OnDoHealth(character);

So I think i'm missing something

The code is correct, maybe a bug or you just nothing get a return from your args func.. Try put a Print as log to OnDoHealth(character); and see if it return anything

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.