Versiones comparadas

Clave

  • Se ha añadido esta línea.
  • Se ha eliminado esta línea.
  • El formato se ha cambiado.
Comentarios: Rewrote parts of the guide

Adding

...

a script to the

...

server

This guide works for Linux and Windows.

Lets assume we you have created a script called and saved it in a file called My_script.cpp in C:\Trinity\src\server\scripts\Custom directory, if you are reading this guide it means you can't figure out how to put it in your solution and compile it with the rest of the source.

Open the file: C:\Trinity\src\server\game\Scripting\ScriptLoader.cpp

We will assume that your linked your script using 'AddSC_my_script', if you don't know what that means, check other scripts to see how they are linked. You first need to add under ' /* This is where custom scripts' loading functions should be declared. / ' and under '/ This is where custom scripts should be added. */' at the bottom of the file the lines:

...

 in the source directory /src/server/scripts/Custom
For this guide lets use this script as an example of what My_script.cpp could look like:

Bloque de código
languagecpp
titleMy_script.cpp
linenumberstrue
#include "ScriptMgr.h"
// .. more includes

class my_script_class: CreatureScript
{
public:
    my_script_class() : CreatureScript("my_script") {}

    // more code .. 
};
 
void AddSC_my_script()
{
    new my_script_class();
} 

The example script uses the function called AddSC_my_script to link it to the core code. You need to use an unique name for your function - otherwise the compiler will error as there are two identical functions. 

 

First you need to add the declaration of the function that is used to link your script and then you add the call to the function - soon we show where to place them.
For our example code the function was called AddSC_my_script. Here is an example of how the declaration and the call look like:

Bloque de código
languagecpp
void AddSC_my_script(); // this is a declaration
AddSC_my_script();      // this is a 

Example

code
call


Open the file: /src/server/game/Scripting/ScriptLoader.cpp and scroll to the bottom of it.
The declaration needs to be added
below the line that says /* This is where custom scripts' loading functions should be declared. */
H
ere is an example of how the code in ScriptLoader.cpp can look after adding the function declaration to it:

Bloque de código
languagecpp
#ifdef SCRIPTS 
/* This is where custom scripts' loading functions should be declared. */
void AddSC_my_script(); 
#endif

and

code

Next you add the function call under the line that says /* This is where custom scripts should be added. */
Again here is an example of how the code could look afterwards: 

Bloque de código
languagecpp
void AddCustomScripts(){
#ifdef SCRIPTS
     /* This is where custom scripts should be added. */
     AddSC_my_script(); 
#endif 
}

Adding the script to the Scripts project

Windows

This information is deprecated, all scripts should be added to the proper cmakelists file as shown in the Linux instructions below.

Open the TrinityCore solution file (~build-directory/TrinityCore.sln using the designated compiler that you selected during the CMake process).

Under the scripts project (left hand side of the screen), press the arrow on the left to open the project dependencies. Then right click on the Source Files folder, and select 'Add' -> 'Existing Item'. Navigate to the location of your custom script (should be something like C:/Trinity/src/server/scripts/custom), select your script and press 'Add' or 'OK'.

You are done! Now you can build the solution.

Linux

Start by placing scripts in source directory:

Bloque de código
~/TrinityCore

 

Now you need to link the script file to the compilation with cmake. Lets do that for our example script. Open up the file /src/server/scripts/Custom

...

Next link each script into the build:

...

/CMakeLists.txt
Our script file is located in /src/server/scripts/Custom

...

add each script with a line break before the Parenthesis and after the Curly Brace with <font color="darkblue">Custom/</font> in front of each script.

...

 and it is called My_script.cpp so we need to add the line "Custom/My_script.cpp" to the file.
Example:

Bloque de código
languagetext
# file(GLOB_RECURSE sources_Custom Custom/*.cpp Custom/*.h)

set(scripts_STAT_SRCS

   

 ${scripts_STAT_SRCS}

    Custom/CS3.cpp
    Custom/CS2.cpp
    Custom/CS1.cpp
)
message("  -> Prepared: Custom")

Last but not least compile

Adding the script to the Database

...


# ${sources_Custom}
 "Custom/My_script.cpp"
)

message(" -> Prepared: Custom")

 

Next you need to use cmake and compile.

Linking the script in the database

You usually need to link your script to the appropriate areatrigger/creature/gameobject/instance/item that the script is for.
Note: Some script types (, such as CommandScripts) , PlayerScripts and WorldScripts, do not need to be linked to anything in the DBExample.

You should be able to find a scriptname column in the database for the entity you need to link the script to. Very often it is in the same database table the entity itself is in.
To this column you should place the script name - which is supposed to be unique. The script name in our example script is my_script and can be found in the code in this part: CreatureScript("my_script")

Examples - note that only the creature_template SQL is correct for our example script

Bloque de código
UPDATE `areatrigger_scripts` SET ScriptName='my_script' WHERE `entry`=XXXX;
UPDATE `creature_template` SET ScriptName='my_script' WHERE `entry`=XXXXX;
UPDATE `gameobject_template` SET ScriptName='my_script' WHERE `entry`=XXXXXX;
UPDATE `instance_template` SET ScriptName='my_script' WHERE `map`=XXX;
UPDATE `item_template` SET ScriptName='my_script' WHERE `entry`=XXXXX;


Note:
You might need to edit the creature/gameobject/etc to make sure the script has effect. For example, if you have created a gossip script for a creature, you will need to set creature_template.npcflag=1 to enable gossip flag on the creature or the script will not work.