ExampleModule.cs 5.8 KB

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