DFHack / dfhack

Memory hacking library for Dwarf Fortress and a set of tools that use it
Other
1.86k stars 465 forks source link

Lua sleep/timeout/join #1588

Closed ralpha closed 3 years ago

ralpha commented 4 years ago

From a Lua script is it possible to sleep/wait on the main thread for something to be loaded?

I know it is possible to use:

dfhack.timeout(120, 'frames', function() dosomething() end)

But this returns the main thread back (don't know if it is handled by threads or something else be still the same). If I want to wait for some loadingscreen to be done loading I have to do some recursive waiting in order to make this work. The other solution is apparently to use:

local script = require 'gui.script'
script.start(function()
  dosomething()
end)

But is there some way of doing a thread.join() for the main thread to wait for the execution of the script until it is done?

It is related to: http://www.bay12forums.com/smf/index.php?topic=176439.msg8151854#msg8151854

lethosor commented 4 years ago

The gui.script module is a wrapper around Lua coroutines, which aren't really the same as threads. There isn't a way to tell if one is "done", as far as I know, so detecting when exportlegends has finished would be pretty hard.

If you're interested in waiting for a world to load, test/main.lua does something a bit hacky that works for the title screen - script.delay() delays a coroutine, which is a simple way to poll for a viewscreen change without repeated calls to dfhack.timeout(): https://github.com/DFHack/dfhack/blob/5d05cfc7ccadbad5dceab045f8e66bb064a2c43c/test/main.lua#L94-L112

Just like exportlegends, test/main will return without waiting for coroutines to finish. It does support running another command when tests have finished, though. Maybe exportlegends could do this too.

lethosor commented 3 years ago

Assuming this was figured out in https://github.com/DFHack/scripts/pull/152