Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: DFHack Job Triggers  (Read 2420 times)

Anon_404

  • Bay Watcher
    • View Profile
DFHack Job Triggers
« on: February 09, 2019, 06:34:10 pm »

Was curious if there are any options for having a lua-script be triggered when certain jobs are completed. Basically, looking for something like the dfhack.onStateChange but for jobs. Alternatively, if there is any way of triggering for lua-scripts when the job_next_id advances.

As I currently understand it, there are two options:
1.) Have a lua-script with a dfhack.timeout of 1 "frames" and have it check the existing job list for the job_type in question. My concern here is that this could be process intensive as it would be checking the job list every frame.

2.) Use a Ruby script similar to autounsuspend.rb which appears to check the job list for suspended buildings before unsuspending them if they meet certain criteria. (Seems to use df.onupdate_register as a trigger but not sure if it works with lua code)

Open to suggestions or thoughts on the matter.

Troll question: is Ruby superior to Lua or is this just a case of me being dumb?
Logged

Roses

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #1 on: February 10, 2019, 01:33:19 pm »

https://dfhack.readthedocs.io/en/stable/docs/Lua%20API.html#eventful


There is already an event manager trigger for onJobInitiated and onJobCompleted. For examples of other scripts that use the event manager triggers you can look at modtools/interaction-trigger and modtools/item-trigger

EDIT: Both of which are lua scripts
Logged

Anon_404

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #2 on: February 10, 2019, 02:47:33 pm »

https://dfhack.readthedocs.io/en/stable/docs/Lua%20API.html#eventful


There is already an event manager trigger for onJobInitiated and onJobCompleted. For examples of other scripts that use the event manager triggers you can look at modtools/interaction-trigger and modtools/item-trigger

EDIT: Both of which are lua scripts

Thank you. That is exactly what I was looking for. Didn't see those used in any of the built in-scripts and wasn't sure where else to look. Will give it a few tries and see what I can do with it.
Logged

Anon_404

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #3 on: February 12, 2019, 07:49:20 pm »

Really out of my depth when it comes to trying to use environment functions and having a hard timing understanding what I am doing wrong compared to what others have done. For example, the current problem I am having is that the below code throws an error saying "attempt to index a function (field 'onJobInitiated')
Code: [Select]
local ev=require 'plugins.eventful'

ev.onJobInitiated.test = function(JOB)
if enabled then
print("Job started!")
end
end

ev.enableEvent(ev.eventType.JOB_INITIATED, 0)

According to examples such as item-trigger.lua, innteraction-trigger.lua, etc there should be something there, but I do not understand what the rules are or what I am missing (removing the .test causes it to run without error but it does not seem to recognize when new jobs are started).

Any help would be appreciated.
Logged

Roses

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #4 on: February 12, 2019, 08:23:19 pm »

What version of DFHack are you using? and how are you doing this test? I just copied that code into a script and it worked for me. Where do you set what enabled is?
« Last Edit: February 12, 2019, 09:25:20 pm by Roses »
Logged

Anon_404

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #5 on: February 12, 2019, 10:03:36 pm »

What version of DFHack are you using? and how are you doing this test? I just copied that code into a script and it worked for me. Where do you set what enabled is?

Using DFHack 0.44.12-r2.

The enabled script is a tidbit that I should have removed, sorry.

Also, apparently functions do not like being called multiple times while previous iterations are running. I had been making changes to the code and re-running it without exiting DF between attempts, so there must have been hold-overs that didn't like me doing that. Bad habit and lack of foresight.

Thanks for the help and enjoy the laugh.
Logged

Roses

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #6 on: February 12, 2019, 11:28:23 pm »

Ah yes, if you had done ev.onJobInitiated = function blah and then tried to do ev.onJobInitiated.test = function blah that would explain the error of not being able to index a function
Logged

lethosor

  • Bay Watcher
    • View Profile
Re: DFHack Job Triggers
« Reply #7 on: February 13, 2019, 01:03:36 am »

Worth noting that restarting DFHack is usually overkill. In this case, you might be able to get by with "reload eventful" at the console and/or "reload('plugins.eventful')" in Lua. For most scripts in general (if they're not messing with global DFHack state outside of their own state), clearing or deleting dfhack.script_environment('script_name') will work.
Logged
DFHack - Dwarf Manipulator (Lua) - DF Wiki talk

There was a typo in the siegers' campfire code. When the fires went out, so did the game.