订阅强制转换对象时不会触发事件

Event won't trigger when subscribing to a casted object

提问人:Bawenang Rukmoko Pardian Putra 提问时间:8/15/2023 最后编辑:Bawenang Rukmoko Pardian Putra 更新时间:8/15/2023 访问量:29

问:

我有这个代码:

public class StateController
{
    public MainEventHandler EventHandler { get => eventHandler; }

    private State currentState;
    private MainEventHandler eventHandler = new();
    private MainRepository repository = new();
    private MainLogic logic = new();

    public void ActivateState(State toActivate)
    {
        if (toActivate is StateA) 
        {
            var properties = StateA.Properties();
            properties.eventHandler = eventHandler;
            properties.repository = repository;
            toActivate.Setup(properties);
        }
        else if (toActivate is StateB) 
        {
            var properties = StateB.Properties();
            properties.eventHandler = eventHandler;
            properties.logic = repository;
            toActivate.Setup(properties);
        }
    }

    public void Start()
    {
        if (eventHandler.onStart != null) 
            onStart();
    }

    public void DoLogic()
    {
        if (eventHandler.onCalculateLogic != null) 
            onCalculateLogic();
    }
}

public class MainEventHandler
{
    public delegate void OnStart();
    public event OnStart onStart;

    public delegate void OnCalculateLogic();
    public event OnCalculateLogic onCalculateLogic;
}

public class StateA: State
{
    public class Properties
    {
         public MainEventHandler eventHandler;
         public MainRepository repository;
    }

    private Properties props;

    public void Setup(object properties)
    {
        props = properties as Properties;
        props.eventHandler.onStart += Start;
    }

    public void CleanUp()
    {
        props.eventHandler.onStart -= Start;
    }

    private void Start() //Doesn't get triggered because onStart is null
    {
        ....
    }
}

public class StateB: State
{
    public class Properties
    {
         public MainEventHandler eventHandler;
         public MainLogic repository;
    }

    private Properties props;

    public void Setup(object properties)
    {
        props = properties as Properties;
        props.eventHandler.onCalculateLogic += Calculate;
    }

    public void CleanUp()
    {
        props.eventHandler.onCalculateLogic -= Calculate;
    }

    private void Calculate() //Doesn't get triggered because onCalculateLogic is null
    {
        ....
    }
}

public MainSceneController: Monobehavior
{
    private StateController stateController = new();
    private StateA firstState = new();
    private StateB secondState = new();
    Awake()
    {
        stateController.ActivateState(firstState);
    }

    Start()
    {
        if(stateController.EventHandler.onStart != null) stateController.EventHandler.onStart(); // stateController.EventHandler.onStart is null apparently even though I have set it in ActivateState()
        stateController.ActivateState(secondState);
    }

    Update()
    {
        if(stateController.EventHandler.onCalculateLogic != null) stateController.EventHandler.onCalculateLogic(); // stateController.EventHandler.onCalculateLogic is null apparently even though I have set it in ActivateState()

    }

}

我想从中传递一个对象,该对象具有可以从状态内部订阅的事件。的参数是一个对象,因为我们可以有多种类型的属性。而属性是国家与其外部系统交互的唯一方式。但不幸的是,这些事件根本不会被触发,因为它们总是空的。虽然正如你所看到的,我已经添加了StateControllerSetup()

`props.eventHandler.onStart += Start;`

`props.eventHandler.onCalculateLogic += Calculate;`

如何解决此问题?

谢谢。

C# unity-game-engine 事件 委托

评论

0赞 BugFinder 8/15/2023
我没有看到任何 start,因为虽然 Start 是单行为调用的,但 Statecontroller 不是。
0赞 Bawenang Rukmoko Pardian Putra 8/15/2023
啊,是的。也许我需要稍微编辑一下代码。谢谢。
0赞 derHugo 8/15/2023
实际上,由于 is 只能由类本身调用,而不能从外部通过 !这应该已经有编译器错误,甚至根本不允许运行onStarteventMainEventHandlerif(stateController.EventHandler.onStart != null) stateController.EventHandler.onStart();
0赞 derHugo 8/16/2023
请发布一个最小的可重现示例..您在此处显示的代码存在各种编译器错误/逻辑问题

答: 暂无答案