From 9b950b4202fc2cf4487a69152f7150daf6d0262e Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Sun, 4 Sep 2022 00:04:00 +0200 Subject: [PATCH] Add event for when difficulty changes Also nuke external library with the same event. Add compatibility for event `onRealDifficultyChanged`, referencing `onDifficultyChanged`. --- scripts/mod_loader/bootstrap/modApi.lua | 1 + scripts/mod_loader/compat/difficulty.lua | 17 +++++++++++++++++ scripts/mod_loader/compat/nuke.lua | 3 +++ 3 files changed, 21 insertions(+) diff --git a/scripts/mod_loader/bootstrap/modApi.lua b/scripts/mod_loader/bootstrap/modApi.lua index e05802ac..0f9968f3 100644 --- a/scripts/mod_loader/bootstrap/modApi.lua +++ b/scripts/mod_loader/bootstrap/modApi.lua @@ -54,6 +54,7 @@ t.onSettingsInitialized = Event() t.onProfileChanged = Event() t.onProfileCreated = Event() t.onProfileDeleted = Event() +t.onDifficultyChanged = Event() t.onHangarUiShown = Event() t.onHangarUiHidden = Event() diff --git a/scripts/mod_loader/compat/difficulty.lua b/scripts/mod_loader/compat/difficulty.lua index 3584a477..512bd85f 100644 --- a/scripts/mod_loader/compat/difficulty.lua +++ b/scripts/mod_loader/compat/difficulty.lua @@ -1,3 +1,20 @@ + +-- EVENTS +--------- + +local difficulty + +modApi.events.onFrameDrawStart:subscribe(function() + local prev_difficulty = difficulty + + difficulty = GetDifficulty() + + if difficulty ~= prev_difficulty then + modApi.events.onDifficultyChanged:dispatch(difficulty, prev_difficulty) + end +end) + + -- Legacy: originally GetDifficulty() returned the custom difficulty while GetRealDifficulty() returned vanilla GetRealDifficulty = GetDifficulty diff --git a/scripts/mod_loader/compat/nuke.lua b/scripts/mod_loader/compat/nuke.lua index 46e35510..bee8ab49 100644 --- a/scripts/mod_loader/compat/nuke.lua +++ b/scripts/mod_loader/compat/nuke.lua @@ -2,3 +2,6 @@ -- nuke libraries that have been migrated to the mod loader SquadEvents = { version = tostring(INT_MAX) } +DifficultyEvents = { version = tostring(INT_MAX) } + +modApi.events.onRealDifficultyChanged = modApi.events.onDifficultyChanged