How to pass function from script to gui_script

okay, I know this is not possible, as mentioned here. But I still got stuck.

I have a interface to popup a confirmation dialog, like this

It dynamically create gui nodes and compose into a window.

when used in gui_scripte,
popup_confirm_dialog(self.background, 0, "Sell", "Are you sure to sell Scroll of Knowledge?", { on_confirmed = ... }) ,
feels natural like normal lua.

But I also need to call this in logical script (say, when our player want to eat a potion at full health), how can I reuse this?

How do we usually handle this situation? Is there a idiom to handle this?

Thanks for your help! Thanks in advance!

My consideration of using message passing is,

  1. send a “confirmed”/“cancelled” to the sender in gui_scripte. However, my sender here is a single “world.script” (most of my logic then contains in a “World.lua”), then I need to maintain stacks of receivers.
  2. or use some shared variables in a lua module between gui_script and script.

Neither sounds like a good/simple idea.

or perhaps the problem is my code organization? Btw, I’m making a rougelike with losts of Items/skills, so this may be called anywhere in the future.