September 20, 2022
The global script, we name it the main script in the editor, it is just a script entry in the global environment when the game is running. Typically, the usefulness of global scripts is to divide code scripts into three broad categories based on the environment in which they are run. The first type is the code script running on the server side; the second type is the code script running on the client side; the third type is the code script running on both the client side and the server side, which we collectively refer to as public code scripts .
Entry location of global script
in the editorToolbar > Game Settings > Events and ScriptsYou can see the entry of the global script in .
Management of global scripts
In the above description, we can know that the role of global scripts is mainly to divide code scripts into three categories, and in order to distinguish these three categories of code scripts, the editor provides us with server-side script management, client-side script management, and client-side script management. The three repositories of management and public script management make it easier and more intuitive to distinguish and manage these three types of code scripts. There will be a main script in each script repository. This script will be automatically loaded in the corresponding operating environment of the script repository. Its main function is to determine whether other code scripts in the script repository need to be loaded immediately.
The execution timing of the global script and the main script of each script repository
On the server, the execution sequence when the server starts is: global script > server script repository main script > public script repository main script.
On the client, the execution order of the client startup is: global script > client script repository main script > public script repository main script.
If there are special needs, we can also swap the execution order of the server or client script repository main script with the main script of the public script repository, just move their loading order in the global script.
How to load other code scripts in the script repository
In the main script in the script repository, you can directly use require to load the script that needs to be loaded. Note that the starting path of require is different depending on the script repository. The starting path in the server-side script repository is "script_server.", the client repository is "script_client.", and the public repository is "script_common." , the subsequent path is the location path of the directory list where the script to be loaded is located.
Take a chestnut:
We create a test script in the server repository:
Then load the test script in the main script of the server-side script repository: