Skip to content

Commit 0895834

Browse files
Update to use a wrapper function to accept unit OR histfig
1 parent 9f9ae70 commit 0895834

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

deathcause.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ local function getDeathStringFromCause(cause)
2626
end
2727

2828
-- Returns a cause of death given a unit
29-
function getDeathCauseFromUnit(unit)
29+
local function getDeathCauseFromUnit(unit)
3030
local str = unit.name.has_name and '' or 'The '
3131
str = str .. dfhack.units.getReadableName(unit)
3232

@@ -104,7 +104,7 @@ local function getDeathEventForHistFig(histfig_id)
104104
end
105105

106106
-- Returns the cause of death given a histfig
107-
function getDeathCauseFromHistFig(histfig)
107+
local function getDeathCauseFromHistFig(histfig)
108108
local histfig_unit = df.unit.find(histfig.unit_id)
109109
if not histfig_unit then
110110
qerror("Cause of death not available")
@@ -149,6 +149,15 @@ local function get_target()
149149
return selected_item.hist_figure_id, df.unit.find(selected_item.unit_id)
150150
end
151151

152+
-- wrapper function to take either a unit or a histfig and get the death cause
153+
function getDeathCause(target)
154+
if df.unit:is_instance(target) then
155+
return getDeathCauseFromUnit(target)
156+
else
157+
return getDeathCauseFromHistFig(target)
158+
end
159+
end
160+
152161
if dfhack_flags.module then
153162
return
154163
end
@@ -161,7 +170,7 @@ elseif hist_figure_id == -1 then
161170
if not selected_unit then
162171
qerror("Cause of death not available")
163172
end
164-
print(dfhack.df2console(getDeathCauseFromUnit(selected_unit)))
173+
print(dfhack.df2console(getDeathCause(selected_unit)))
165174
else
166-
print(dfhack.df2console(getDeathCauseFromHistFig(df.historical_figure.find(hist_figure_id))))
175+
print(dfhack.df2console(getDeathCause(df.historical_figure.find(hist_figure_id))))
167176
end

docs/deathcause.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@ commandline interface with ``dfhack.run_script()`` or via the API functions
2323
defined in :source-scripts:`deathcause.lua`, available from the return value of
2424
``reqscript('deathcause')``:
2525

26-
* ``getDeathCauseFromHistFig(histfig)``
26+
* ``getDeathCause(unit or historical_figure)``
2727

28-
Returns a string with the historical figure's cause of death, sometimes with more information
29-
than with a unit.
28+
Returns a string with the unit or historical figure's cause of death. Note that using a historical
29+
figure will sometimes provide more information than using a unit.
3030

31-
* ``getDeathCauseFromUnit(unit)``
32-
33-
Returns a string with the unit's cause of death.
3431

3532
API usage example::
3633

0 commit comments

Comments
 (0)