Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua sleep/timeout/join #1588

Closed
ralpha opened this issue Jun 9, 2020 · 2 comments
Closed

Lua sleep/timeout/join #1588

ralpha opened this issue Jun 9, 2020 · 2 comments

Comments

@ralpha
Copy link

ralpha commented Jun 9, 2020

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
Copy link
Member

lethosor commented Jun 9, 2020

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():

dfhack/test/main.lua

Lines 94 to 112 in 5d05cfc

function ensure_title_screen()
if df.viewscreen_titlest:is_instance(dfhack.gui.getCurViewscreen()) then
return
end
print('Looking for title screen...')
for i = 0, 100 do
local scr = dfhack.gui.getCurViewscreen()
if df.viewscreen_titlest:is_instance(scr) then
print('Found title screen')
break
else
scr:feed_key(df.interface_key.LEAVESCREEN)
delay(10)
end
end
if not df.viewscreen_titlest:is_instance(dfhack.gui.getCurViewscreen()) then
error('Could not find title screen')
end
end

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
Copy link
Member

Assuming this was figured out in DFHack/scripts#152

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants