How to use (C #) to implement global signals in GDScript
Asked Answered
D

2

0

I have encountered some problems: in GDScript, it is easy to link the signal of B in A(A.abc.connect(b)), but I am using C #. How can I implement this function. I reported an error in the code below
`public partial class GameEvents : Node
{

[Signal]
public delegate void GameEventsExpManagerEventHandler(float number); 

public override void _Ready()
{
	this.GameEventsExpManager += EmitExperivalCollected;
}


public void EmitExperivalCollected(float number){
	GD.Print("do something...");
}}`

`public partial class ExperienceVial : Node2D
{
private void OnAreaEntered(Area2D area2D){
EmitSignal(GameEvents.SignalName.GameEventsExpManager,1.0f);
this.QueueFree();
}

public override void _Ready()
{
	GetNode<Area2D>("Area2D").AreaEntered += OnAreaEntered;
}

}
`

error:E 0:00:06:0834 NativeCalls.cs:6254 @ int Godot.NativeCalls.godot_icall_2_699(nint, nint, Godot.NativeInterop.godot_string_name, Godot.Variant[], Godot.NativeInterop.godot_string_name): Can't emit non-existing signal "GameEventsExpManager".
<C++ 错误> Condition "!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name)" is true. Returning: ERR_UNAVAILABLE
<C++ 源文件> core/object/object.cpp:1083 @ emit_signalp()
<栈追踪> NativeCalls.cs:6254 @ int Godot.NativeCalls.godot_icall_2_699(nint, nint, Godot.NativeInterop.godot_string_name, Godot.Variant[], Godot.NativeInterop.godot_string_name)
GodotObject.cs:602 @ Godot.Error Godot.GodotObject.EmitSignal(Godot.StringName, Godot.Variant[])
ExperienceVial.cs:10 @ void ExperienceVial.OnAreaEntered(Godot.Area2D)
Area2D.cs:739 @ void Godot.Area2D.AreaEnteredTrampoline(object, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
DelegateUtils.cs:62 @ void Godot.DelegateUtils.InvokeWithVariantArgs(nint, System.Void, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant)

Is using event delegation? How should I use C # event delegation to complete?

Downall answered 19/2 at 9:24 Comment(0)
W
1
EmitSignal(GameEvents.SignalName.GameEventsExpManager,1.0f);

This should EmitSignal on the class or instance that defines the signal, e.g. GameEvents.EmitSignal(/* ... */);.

Whoredom answered 19/2 at 11:25 Comment(0)
D
0

Whoredom If you directly use (GameEvents. SignalName. GameEventsExpManager), it will show that the signal is undefined.What I want is A to define signal a, and B to connect signal a to trigger its own method b. However, thank you all the same. I have already completed this function

Downall answered 20/2 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.