Skip to content

Commit 853cd16

Browse files
committed
Script API: implemented a number of global events for Dialogs
* eEventDialogStart * eEventDialogStop * eEventDialogRun * eEventDialogOptionsOpen * eEventDialogOptionsClose
1 parent b595344 commit 853cd16

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

Editor/AGS.Editor/Resources/agsdefns.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,13 @@ enum EventType {
17201720
eEventLeaveRoomAfterFadeout = 11,
17211721
eEventGameSaved = 12,
17221722
#endif
1723+
#ifdef SCRIPT_API_v362
1724+
eEventDialogStart = 13,
1725+
eEventDialogStop = 14,
1726+
eEventDialogRun = 15,
1727+
eEventDialogOptionsOpen = 16,
1728+
eEventDialogOptionsClose = 17,
1729+
#endif
17231730
};
17241731

17251732
#ifdef SCRIPT_API_v350

Engine/ac/dialog.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "ac/dialogtopic.h"
2121
#include "ac/display.h"
2222
#include "ac/draw.h"
23+
#include "ac/event.h"
2324
#include "ac/gamestate.h"
2425
#include "ac/gamesetupstruct.h"
2526
#include "ac/global_character.h"
@@ -1197,7 +1198,8 @@ void DialogOptions::End()
11971198
int run_dialog_entry(int dlgnum)
11981199
{
11991200
DialogTopic *dialog_topic = &dialog[dlgnum];
1200-
// Run global event kScriptEvent_DialogRun
1201+
// Run global event kScriptEvent_DialogRun for the startup entry (index 0)
1202+
run_on_event(kScriptEvent_DialogRun, RuntimeScriptValue().SetInt32(dlgnum), RuntimeScriptValue().SetInt32(0));
12011203
return run_dialog_script(dlgnum, dialog_topic->startupentrypoint, 0);
12021204
}
12031205

@@ -1208,6 +1210,9 @@ int run_dialog_option(int dlgnum, int dialog_choice, int sayChosenOption, bool r
12081210
int &option_flags = dialog_topic->optionflags[dialog_choice];
12091211
const char *option_name = dialog_topic->optionnames[dialog_choice];
12101212

1213+
// Run global event kScriptEvent_DialogRun for the new option
1214+
run_on_event(kScriptEvent_DialogRun, RuntimeScriptValue().SetInt32(dlgnum), RuntimeScriptValue().SetInt32(dialog_choice + 1));
1215+
12111216
option_flags |= DFLG_HASBEENCHOSEN;
12121217
bool sayTheOption = false;
12131218
if (sayChosenOption == SAYCHOSEN_YES)
@@ -1265,9 +1270,14 @@ int show_dialog_options(int dlgnum, bool runGameLoopsInBackground)
12651270
return last_opt; // only one choice, so select it
12661271
}
12671272

1273+
// Run the global DialogOptionsOpen event
1274+
run_on_event(kScriptEvent_DialogOptionsOpen, RuntimeScriptValue().SetInt32(dlgnum));
1275+
12681276
DialogOptions dlgopt(dtop, dlgnum, runGameLoopsInBackground);
12691277
dlgopt.Show();
12701278

1279+
// Run the global DialogOptionsClose event
1280+
run_on_event(kScriptEvent_DialogOptionsClose, RuntimeScriptValue().SetInt32(dlgnum), RuntimeScriptValue().SetInt32(dlgopt.GetChosenOption()));
12711281

12721282
return dlgopt.GetChosenOption();
12731283
}
@@ -1375,6 +1385,9 @@ void do_conversation(int dlgnum)
13751385
if (loaded_game_file_version <= kGameVersion_272)
13761386
play.mouse_cursor_hidden = 0;
13771387

1388+
// Run the global DialogStart event
1389+
run_on_event(kScriptEvent_DialogStart, RuntimeScriptValue().SetInt32(dlgnum));
1390+
13781391
DialogExec dlgexec(dlgnum);
13791392
dlgexec.Run();
13801393
// CHECKME: find out if this is safe to do always, regardless of number of iterations
@@ -1384,6 +1397,9 @@ void do_conversation(int dlgnum)
13841397
remove_screen_overlay(OVER_COMPLETE);
13851398
play.in_conversation--;
13861399
}
1400+
1401+
// Run the global DialogStop event; NOTE: DlgNum may be different in the end
1402+
run_on_event(kScriptEvent_DialogStop, RuntimeScriptValue().SetInt32(dlgexec.DlgNum));
13871403
}
13881404

13891405
// end dialog manager

Engine/ac/event.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ void run_claimable_event(const String &tsname, bool includeRoom, int numParams,
107107
}
108108

109109
// runs the global script on_event function
110-
void run_on_event(int evtype, RuntimeScriptValue &wparam)
110+
void run_on_event(AGSScriptEventType evtype, const RuntimeScriptValue &data1, const RuntimeScriptValue &data2)
111111
{
112-
RuntimeScriptValue params[]{ evtype , wparam };
113-
QueueScriptFunction(kScTypeGame, "on_event", 2, params);
112+
RuntimeScriptValue params[]{ evtype , data1, data2 };
113+
QueueScriptFunction(kScTypeGame, "on_event", 3, params);
114114
}
115115

116116
void run_room_event(int id) {

Engine/ac/event.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ enum AGSScriptEventType
114114
kScriptEvent_RoomAfterFadein = 10, // enter after fade-in
115115
kScriptEvent_RoomAfterFadeout = 11, // after fade-out, right before unloading
116116
kScriptEvent_GameSaved = 12,
117+
kScriptEvent_DialogStart = 13, // before game enters a "dialog" state
118+
kScriptEvent_DialogStop = 14, // after game returns from a "dialog" state
119+
kScriptEvent_DialogRun = 15, // a dialog option is run
120+
kScriptEvent_DialogOptionsOpen = 16, // before dialog options are displayed on screen
121+
kScriptEvent_DialogOptionsClose = 17, // after dialog options are removed from screen
117122
};
118123

119124

@@ -200,8 +205,8 @@ struct AGSEvent
200205
};
201206

202207
void run_claimable_event(const AGS::Common::String &tsname, bool includeRoom, int numParams, const RuntimeScriptValue *params, bool *eventWasClaimed);
203-
// runs the global script on_event fnuction
204-
void run_on_event (int evtype, RuntimeScriptValue &wparam);
208+
// runs the global script on_event function
209+
void run_on_event(AGSScriptEventType evtype, const RuntimeScriptValue &data1 = RuntimeScriptValue(), const RuntimeScriptValue &data2 = RuntimeScriptValue());
205210
void run_room_event(int id);
206211
// event list functions
207212
void setevent(const AGSEvent &evt);

Engine/script/script.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,10 @@ void post_script_cleanup() {
741741
for (const auto &script : copyof.ScFnQueue) {
742742
old_room_number = displayed_room;
743743
RunScriptFunctionAuto(script.ScType, script.Function, script.ParamCount, script.Params);
744+
// FIXME: this is some bogus hack for "on_call" event handler
745+
// don't use instance + param count, instead find a way to save actual callback name!
744746
if (script.ScType == kScTypeRoom && script.ParamCount == 1)
745747
{
746-
// some bogus hack for "on_call" event handler
747748
play.roomscript_finished = 1;
748749
}
749750

0 commit comments

Comments
 (0)