2

I'm building a plugin to a system. My plugin's update() method is called occasionally by the system.

In my plugin's update() method, I'm modifying the systems state, however the system state isn't updated until the system has had a chance to do so (this happens between invocations of the update method).

So whenever I do a system updating method I'll have to return all the way out of the update(), for it to return, and re-enter, and then I'll have to try to get back to where I was. I've been considering a smarter way to do this, namely by saving call frame, and such. And then loading that call-frame on return.

But instead of starting to implement this on my own, I was considering whether there was already a way to do this. The plugin I'm writing is in Lua, via the NLua library for C#.

I'm able to access the C# environment, if needed!

I have an idea, that I need something like a continuation?

Example of what I'd love to have;

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.
Skeen
  • 4,614
  • 5
  • 41
  • 67

2 Answers2

1

I was indeed looking for coroutines, they seem to do exactly what I want. I ended up wrapping my update() can in a method to run the actual update function as a coroutine, and wrapping all my system state changing methods, with coroutine.yield()

Skeen
  • 4,614
  • 5
  • 41
  • 67
0

Although the coroutine route is quite nice, another option might be, depending on constraints (like if you can pass obj to script) for the stack & registers to be put in a struct that is given as part of call:

 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
Oliver
  • 27,510
  • 9
  • 72
  • 103