Skip to content

Commit 18c182a

Browse files
authored
script hooks must be declared within global_hooks so that both FRED and FSO can see them (#5031)
1 parent c9291e3 commit 18c182a

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

code/scripting/global_hooks.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,33 @@ const std::shared_ptr<Hook<>> OnCheat = Hook<>::Factory("On Cheat",
368368
{ "Cheat", "string", "The cheat code the user typed" },
369369
});
370370

371+
const std::shared_ptr<Hook<>> OnMissionAboutToEndHook = Hook<>::Factory("On Mission About To End",
372+
"Called when a mission is about to end but has not run any mission-ending logic",
373+
{});
374+
375+
const std::shared_ptr<OverridableHook<>> OnMissionEndHook =
376+
scripting::OverridableHook<>::Factory("On Mission End", "Called when a mission has ended", {});
377+
378+
const std::shared_ptr<Hook<>> OnStateAboutToEndHook = Hook<>::Factory("On State About To End",
379+
"Called when a game state is about to end but has not run any state-ending logic",
380+
{
381+
{"OldState", "gamestate", "The game state that has ended."},
382+
{"NewState", "gamestate", "The game state that will begin next."},
383+
});
384+
385+
const std::shared_ptr<OverridableHook<>> OnStateEndHook = OverridableHook<>::Factory("On State End",
386+
"Called when a game state has ended",
387+
{
388+
{"OldState", "gamestate", "The game state that has ended."},
389+
{"NewState", "gamestate", "The game state that will begin next."},
390+
});
391+
392+
const std::shared_ptr<Hook<>> OnCameraSetUpHook = Hook<>::Factory("On Camera Set Up",
393+
"Called every frame when the camera is positioned and oriented for rendering.",
394+
{
395+
{"Camera", "camera", "The camera about to be used for rendering."},
396+
});
397+
371398
// ========== DEPRECATED ==========
372399

373400
const std::shared_ptr<OverridableHook<ObjectDeathConditions>> OnDeath = OverridableHook<ObjectDeathConditions>::Factory("On Death",

code/scripting/global_hooks.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ extern const std::shared_ptr<Hook<>> OnDialogClose;
7171

7272
extern const std::shared_ptr<Hook<>> OnCheat;
7373

74+
extern const std::shared_ptr<Hook<>> OnMissionAboutToEndHook;
75+
extern const std::shared_ptr<OverridableHook<>> OnMissionEndHook;
76+
extern const std::shared_ptr<Hook<>> OnStateAboutToEndHook;
77+
extern const std::shared_ptr<OverridableHook<>> OnStateEndHook;
78+
extern const std::shared_ptr<Hook<>> OnCameraSetUpHook;
79+
7480
// deprecated
7581
extern const std::shared_ptr<OverridableHook<ObjectDeathConditions>> OnDeath;
7682

freespace2/freespace.cpp

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -357,34 +357,6 @@ UI_TIMESTAMP Player_died_popup_wait;
357357

358358
UI_TIMESTAMP Multi_ping_timestamp;
359359

360-
361-
const auto OnMissionAboutToEndHook = scripting::Hook<>::Factory(
362-
"On Mission About To End", "Called when a mission is about to end but has not run any mission-ending logic", {});
363-
364-
const auto OnMissionEndHook = scripting::OverridableHook<>::Factory(
365-
"On Mission End", "Called when a mission has ended", {});
366-
367-
const auto OnStateAboutToEndHook = scripting::Hook<>::Factory(
368-
"On State About To End", "Called when a game state is about to end but has not run any state-ending logic",
369-
{
370-
{"OldState", "gamestate", "The game state that has ended."},
371-
{"NewState", "gamestate", "The game state that will begin next."},
372-
});
373-
374-
const auto OnStateEndHook = scripting::OverridableHook<>::Factory(
375-
"On State End", "Called when a game state has ended",
376-
{
377-
{"OldState", "gamestate", "The game state that has ended."},
378-
{"NewState", "gamestate", "The game state that will begin next."},
379-
});
380-
381-
const auto OnCameraSetUpHook = scripting::Hook<>::Factory(
382-
"On Camera Set Up", "Called every frame when the camera is positioned and oriented for rendering.",
383-
{
384-
{"Camera", "camera", "The camera about to be used for rendering."},
385-
});
386-
387-
388360
// builtin mission list stuff
389361
int Game_builtin_mission_count = 92;
390362
fs_builtin_mission Game_builtin_mission_list[MAX_BUILTIN_MISSIONS] = {
@@ -861,14 +833,14 @@ static void game_flash_diminish(float frametime)
861833

862834
void game_level_close()
863835
{
864-
if (OnMissionAboutToEndHook->isActive())
836+
if (scripting::hooks::OnMissionAboutToEndHook->isActive())
865837
{
866-
OnMissionAboutToEndHook->run();
838+
scripting::hooks::OnMissionAboutToEndHook->run();
867839
}
868840

869841
//WMC - this is actually pretty damn dangerous, but I don't want a modder
870842
//to accidentally use an override here without realizing it.
871-
if (!OnMissionEndHook->isActive() || !OnMissionEndHook->isOverride())
843+
if (!scripting::hooks::OnMissionEndHook->isActive() || !scripting::hooks::OnMissionEndHook->isOverride())
872844
{
873845
// save player-persistent variables and containers
874846
mission_campaign_save_on_close_variables(); // Goober5000
@@ -948,9 +920,9 @@ void game_level_close()
948920
Error(LOCATION, "Scripting Mission End override is not fully supported yet.");
949921
}
950922

951-
if (OnMissionEndHook->isActive())
923+
if (scripting::hooks::OnMissionEndHook->isActive())
952924
{
953-
OnMissionEndHook->run();
925+
scripting::hooks::OnMissionEndHook->run();
954926
}
955927
}
956928

@@ -3192,8 +3164,8 @@ camid game_render_frame_setup()
31923164
if(Viewer_mode & VM_FREECAMERA) {
31933165
Viewer_obj = nullptr;
31943166

3195-
if (OnCameraSetUpHook->isActive()) {
3196-
OnCameraSetUpHook->run(scripting::hook_param_list(
3167+
if (scripting::hooks::OnCameraSetUpHook->isActive()) {
3168+
scripting::hooks::OnCameraSetUpHook->run(scripting::hook_param_list(
31973169
scripting::hook_param("Camera", 'o', scripting::api::l_Camera.Set(cam_get_current()))));
31983170
}
31993171

@@ -3327,8 +3299,8 @@ camid game_render_frame_setup()
33273299
main_cam->set_position(&eye_pos);
33283300
main_cam->set_rotation(&eye_orient);
33293301

3330-
if (OnCameraSetUpHook->isActive()) {
3331-
OnCameraSetUpHook->run(scripting::hook_param_list(
3302+
if (scripting::hooks::OnCameraSetUpHook->isActive()) {
3303+
scripting::hooks::OnCameraSetUpHook->run(scripting::hook_param_list(
33323304
scripting::hook_param("Camera", 'o', scripting::api::l_Camera.Set(Main_camera))));
33333305
}
33343306

@@ -5100,18 +5072,18 @@ void game_leave_state( int old_state, int new_state )
51005072
break;
51015073
}
51025074

5103-
if (OnStateAboutToEndHook->isActive() || OnStateEndHook->isActive())
5075+
if (scripting::hooks::OnStateAboutToEndHook->isActive() || scripting::hooks::OnStateEndHook->isActive())
51045076
{
51055077
auto script_param_list = scripting::hook_param_list(
51065078
scripting::hook_param("OldState", 'o', scripting::api::l_GameState.Set(scripting::api::gamestate_h(old_state))),
51075079
scripting::hook_param("NewState", 'o', scripting::api::l_GameState.Set(scripting::api::gamestate_h(new_state))));
51085080

5109-
if (OnStateAboutToEndHook->isActive())
5110-
OnStateAboutToEndHook->run(script_param_list);
5081+
if (scripting::hooks::OnStateAboutToEndHook->isActive())
5082+
scripting::hooks::OnStateAboutToEndHook->run(script_param_list);
51115083

5112-
if (OnStateEndHook->isActive() && OnStateEndHook->isOverride(script_param_list))
5084+
if (scripting::hooks::OnStateEndHook->isActive() && scripting::hooks::OnStateEndHook->isOverride(script_param_list))
51135085
{
5114-
OnStateEndHook->run(script_param_list);
5086+
scripting::hooks::OnStateEndHook->run(script_param_list);
51155087
return;
51165088
}
51175089
}
@@ -5535,13 +5507,13 @@ void game_leave_state( int old_state, int new_state )
55355507
}
55365508

55375509
//WMC - Now run scripting stuff
5538-
if (OnStateEndHook->isActive())
5510+
if (scripting::hooks::OnStateEndHook->isActive())
55395511
{
55405512
auto script_param_list = scripting::hook_param_list(
55415513
scripting::hook_param("OldState", 'o', scripting::api::l_GameState.Set(scripting::api::gamestate_h(old_state))),
55425514
scripting::hook_param("NewState", 'o', scripting::api::l_GameState.Set(scripting::api::gamestate_h(new_state))));
55435515

5544-
OnStateEndHook->run(script_param_list);
5516+
scripting::hooks::OnStateEndHook->run(script_param_list);
55455517
}
55465518
}
55475519

0 commit comments

Comments
 (0)