ExampleModule.tscript 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //This is our create function. It's pointed to, by name, via a field defined in
  2. //the ExampleModule.module file, which contains our module definition. It is called
  3. //when the module is initially loaded by the engine. Generally, only common, base-level
  4. //stuff is created(or destroyed, in the companion function), like things utilized or
  5. //shared on both the client and server, or things that need to be loaded before anything
  6. //else.
  7. function ExampleModule::onCreate(%this)
  8. {
  9. }
  10. //Similar to the create function, this is defined in thye module file, and called
  11. //when the module is destroyed, usually as part of the game shutting down.
  12. function ExampleModule::onDestroy(%this)
  13. {
  14. }
  15. //This is called when the server part of the application is initially created. Torque3D
  16. //assumes, even in a single player context, that there is ultimately a 'server' and a 'client'
  17. //So during initial launch and startup of the engine, the server side is initialized in
  18. //core/clientServer/scripts/server/server.tscript - in the initServer() function where this is called.
  19. //This is called on all modules that have this function defined. This is important for
  20. //any persistant parts of the server that always need to run such as gameplay scripts
  21. //
  22. //Importantly, when the gane session server is created, several functions are called to as part of the gamemode logic
  23. //The script below contains the callbacks so the gamemode can actually be set up, but the server-side callbacks in question:
  24. //ExampleGameMode::onMissionStart
  25. //ExampleGameMode::onMissionEnded
  26. //ExampleGameMode::onMissionReset
  27. //Are called during the startup, shut down, and resetting of any and all active gamemodes, as informed by the loaded scenes
  28. //when the game server is processed.
  29. //These callbacks are activated in core/clientServer/scripts/server/levelLoad.tscript
  30. function ExampleModule::initServer(%this)
  31. {
  32. //This script contains our ExampleGameMode logic
  33. %this.queueExec("./scripts/ExampleGamemodeScript");
  34. }
  35. //This is called when a game session server is actually created so the game may be played. It's called
  36. //from core/clientServer/scripts/server/server.tscript - in the createServer() function, which is called when
  37. //A game session is actually launched, and the server is generated so game clients can connect to it.
  38. //This is utilized to set up common things that need to be set up each time the game session server is
  39. //created, such as common variables, datablocks to be transmitted to the client, etc.
  40. function ExampleModule::onCreateGameServer(%this)
  41. {
  42. //In particular, the default client/server module handles the transmission of datablocks from
  43. //server to client automatically as part of the connection and prepping process alongside
  44. //validation and tramission of level objects. It does this in an abstracted way by adding
  45. //the file paths to a master DatablockFilesList array as per below. When the server is created in
  46. //onServerCreated(), it loads the datablocks via this array, and when when the server goes
  47. //to pass data to the client, it iterates over this list and processes it, ensuring all datablocks
  48. //are the most up to date possible for transmission to the connecting client
  49. //%this.registerDatablock("./datablocks/ExampleDatablock." @ $TorqueScriptFileExtension);
  50. }
  51. //This is called when a game session server is destroyed, when the game shuts down. It's called from
  52. //core/clientServer/scripts/server/server.tscript - in the destroyServer() function, which just cleans up anything
  53. //The module may have set up as part of the game server being created.
  54. function ExampleModule::onDestroyGameServer(%this)
  55. {
  56. }
  57. //Similar to initServer, this is called during the initial launch of the application and the client component
  58. //is set up. The difference is that the client may not actually be created, such as in the case for dedicated servers
  59. //Where no UI or gameplay interface is required. It's called from core/clientServer/scripts/client/client.tscript -
  60. //in the initClient() function. It sets up common elements that the client will always need, such as scripts, GUIs
  61. //and the like
  62. function ExampleModule::initClient(%this)
  63. {
  64. AssetDatabase.acquireAsset("ExampleModule:exampleDatablock");
  65. AssetDatabase.acquireAsset("ExampleModule:examplePostEffect");
  66. AssetDatabase.acquireAsset("ExampleModule:exampleGUI");
  67. //client scripts
  68. //Here, we exec out keybind scripts so the player is able to move when they get into a game
  69. %this.queueExec("./scripts/default.keybinds");
  70. %prefPath = getPrefpath();
  71. if(isFile(%prefPath @ "/keybinds." @ $TorqueScriptFileExtension))
  72. exec(%prefPath @ "/keybinds." @ $TorqueScriptFileExtension);
  73. %this.queueExec("./scripts/inputCommands");
  74. }
  75. //This is called when a game session client successfuly connects to a game server.
  76. //It's called from core/clientServer/scripts/client/connectionToServer.tscript - in the GameConnection::onConnectionAccepted() function
  77. //It's used for any client-side specific game session stuff that the client needs to load or pass to the server, such as profile data
  78. //account progress, preferences, etc.
  79. //
  80. //When a client is connected, the gamemode logic also has a callback activated - ExampleGameMode::onClientEnterGame().
  81. function ExampleModule::onCreateClientConnection(%this)
  82. {
  83. //This will push our keybind movemap onto the input stack, so we can control our camera in our ExampleGameMode
  84. ExampleMoveMap.push();
  85. }
  86. //This is called when a client game session disconnects from a game server
  87. //It's called from core/clientServer/scripts/client/connectionToServer.tscript - in the disconnectedCleanup() function
  88. //It's used to clean up and potentially write out any client-side stuff that needs housekeeping when disconnecting for any reason.
  89. //It will be called if the connection is manually terminated, or lost due to any sort of connection issue.
  90. //
  91. //When a client disconnects, the gamemode logic has a callback activated - ExampleGameMode::onClientLeaveGame().
  92. function ExampleModule::onDestroyClientConnection(%this)
  93. {
  94. //This will pop the keybind, cleaning it up from the input stack, as it no longer applies
  95. ExampleMoveMap.pop();
  96. }
  97. function ExampleModule::populateOptionsMenuCategories(%this)
  98. {
  99. addOptionsMenuCategory("Example Options", "testExampleOptions();");
  100. }
  101. function testExampleOptions()
  102. {
  103. OptionsMenuSettingsList.clear();
  104. OptionName.setText("");
  105. OptionDescription.setText("");
  106. addListOption("Test Option", "This is a test option", $testOptionValue, "OptionA\tOptionB");
  107. }