Godot C#:信号、委托和事件

问题描述 投票:0回答:1

我不知道这是否是戈多的限制...... 我试图在我的 C# 文件中声明一些事件。 当您在 Godot 中的委托声明之前写入“[Signal]”时,引擎会在内部声明相应的事件。然后您可以使用它的“连接”功能,它取代了标准的“EventName += Method;” 不幸的是,它只有在“[Signal] delegate...”在公共分部类内部声明时才有效,该类在 Godot 节点系统中使用。但是,如果我这样做,接口将不会包含事件声明。

在这种情况下有没有办法同时使用接口和【信号】?

using Godot;

namespace Game.Component {
  public delegate void CurrentLifeChangedEventHandler(GodotObject sender, int amount); 
  public delegate void MaximumLifeChangedEventHandler(GodotObject sender, int amount); 
  public delegate void ExtraLifeChangedEventHandler(GodotObject sender, int amount); 
  public delegate void CurrentLifeReachedZeroEventHandler(GodotObject sender); 
  public delegate void MaximumLifeReachedZeroEventHandler(GodotObject sender); 
  public delegate void ExtraLifeReachedZeroEventHandler(GodotObject sender); 
  
  public interface ILifeComponent {
    event CurrentLifeChangedEventHandler CurrentLifeChanged; 
    event MaximumLifeChangedEventHandler MaximumLifeChanged; 
    event ExtraLifeChangedEventHandler ExtraLifeChanged; 
    event CurrentLifeReachedZeroEventHandler CurrentLifeReachedZero; 
    event MaximumLifeReachedZeroEventHandler MaximumLifeReachedZero; 
    event ExtraLifeReachedZeroEventHandler ExtraLifeReachedZero; 

    void InitializeHealth();
    void IncreaseMaximumLife(int amount);
    void IncreaseExtraLife(int amount);
    void TakeDamage(IDamage damage);  
    void HealLife(IHeal heal);
  }

  public partial class LifeComponent : Node, ILifeComponent{
    public event CurrentLifeChangedEventHandler CurrentLifeChanged; 
    public event MaximumLifeChangedEventHandler MaximumLifeChanged; 
    public event ExtraLifeChangedEventHandler ExtraLifeChanged; 
    public event CurrentLifeReachedZeroEventHandler CurrentLifeReachedZero; 
    public event MaximumLifeReachedZeroEventHandler MaximumLifeReachedZero; 
    public event ExtraLifeReachedZeroEventHandler ExtraLifeReachedZero; 

using Godot;
using Godot.Collections;

namespace Game.Component {

  public partial class ResistanceVulnerabilityComponent : Node, IResistanceVulnerabilityComponent {
    [Signal] public delegate void GainedResistanceEventHandler();
    [Signal] public delegate void GainedVulnerabilityEventHandler();
    [Signal] public delegate void LostResistanceEventHandler();
    [Signal] public delegate void LostVulnerabilityEventHandler();

使用 C# 事件 使用戈多信号

我们必须在 godot 节点中使用“公共部分类”,这有点奇怪。

c# events delegates signals godot
1个回答
0
投票

我认为你可以做这样的事情:

using Godot;
using Godot.Collections;
    
namespace Game.Component {
public interface IResistanceVulnerabilityComponent
{
    event ResistanceVulnerabilityComponent.GainedResistanceEventHandler GainedResistance;
}

public partial class ResistanceVulnerabilityComponent : Node, IResistanceVulnerabilityComponent {
    public event GainedResistanceEventHandler GainedResistance;
    [Signal]
    public delegate void GainedResistanceEventHandler();
}
}
© www.soinside.com 2019 - 2024. All rights reserved.