ModuleInit Message: Difference between revisions
m 1 revision |
m Changed the target object in the AddHandler() call to Interface. |
||
| Line 9: | Line 9: | ||
public void Init(void) { | public void Init(void) { | ||
inherited(); | inherited(); | ||
AddHandler( | AddHandler(Interface, "World", "ModuleInit", "ModuleInitHandler"); | ||
} | } | ||
| Line 24: | Line 24: | ||
==See Also== | ==See Also== | ||
* [[List of Standard Messages]] | * [[List of Standard Messages]] | ||
* [[TrainzScript Message Reference]] | |||
Latest revision as of 02:13, 31 August 2026
Switching directly from Surveyor to Driver mode via the ^F2 shortcut or the Quick Drive button allows a Driver session to be launched directly from from Surveyor.
However Trainz carries out initialisation tasks for scripted objects when they are loaded into Surveyor and many of these do not need to be repeated when the user switches to Driver mode. Some tasks, such as monitoring threads, will be started by the game if Driver is the first module called, but not otherwise. In consequence these tasks will not run if Driver is launched via ^F2.
The message World,ModuleInit is provided to allow this issue to be overcome.
If this handler is added in Init()
public void Init(void) {
inherited();
AddHandler(Interface, "World", "ModuleInit", "ModuleInitHandler");
}
It will be possible to track the change between Driver and Surveyor and take any necessary corrective measures in the ModuleInitHandler method.
void ModuleInitHandler(Message msg) {
if (World.GetCurrentModule() == World.DRIVER_MODULE) {
InitialiseDriverTasks();
}
}
World.GetCurrentModule() allows the current module to be queried from script.