【问题标题】:Return directly to caller of caller and restore state on next call直接返回调用者的调用者并在下次调用时恢复状态
【发布时间】:2014-02-27 14:01:12
【问题描述】:

我正在为系统构建一个插件。系统偶尔会调用我插件的update() 方法。

在我的插件的update() 方法中,我正在修改系统状态,但是在系统有机会这样做之前系统状态不会更新(这发生在update 方法的调用之间)。

所以每当我执行系统更新方法时,我都必须从update() 一路返回,让它返回,然后重新进入,然后我必须尝试回到哪里我曾是。我一直在考虑一种更聪明的方法来做到这一点,即通过保存调用框架等。然后在返回时加载该调用框架。

但我并没有开始自己实现这一点,而是在考虑是否已经有办法做到这一点。我正在编写的插件是在 Lua 中,通过 C# 的 NLua 库。

如果需要,我可以访问 C# 环境!

我有一个想法,我需要像延续这样的东西?

我想拥有的示例;

update() --> ... --> helper_function() --> system_state_modifier()
// System state modifier changes system state, saves the current stack/registers,
// whatever else is needed, and returns directly to update()'s caller
// On the next call to update, the previous stack/registers and such
// is restored, and continued.

【问题讨论】:

  • 我想你想使用协程。

标签: c# lua continuations nlua


【解决方案1】:

我确实在寻找协程,它们似乎完全符合我的要求。 我最终将我的 update() 包装在一个方法中,以将实际更新功能作为协程运行,并使用 coroutine.yield() 包装我所有的系统状态更改方法

【讨论】:

    【解决方案2】:

    虽然协程路由非常好,但另一个选项可能是,取决于约束(例如是否可以将 obj 传递给脚本),将堆栈和寄存器放入作为调用的一部分给出的结构中:

     struct StackAndRegisters { ... }
     StackAndRegisters sar; // outside update loop
    
     // then at every call: 
     update(sar) -> -> ... --> helper_function(sar) --> system_state_modifier(sar)
     // system_state_modifier modifies sar
    

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      相关资源
      最近更新 更多