Chapter 6: Scripting

Wir verwenden Cookies, um Inhalte und Anzeigen zu personalisieren, Dienste bereitzustellen und die Zugriffe auf unsere Website zu analysieren. Außerdem werden durch unsere Partner Informationen zu Ihrer Nutzung für soziale Medien, Werbung und Analysen erfasst. Weitere Informationen

  • Information about the scripting for EMERGENCY 4.
    In this part of the master tutorial the scripting for EMERGENCY 4 is the main topic. You don't need any programming skills in order to change scripts, but you should read the comments in the scripts very carefully to make the right changes. As mentioned in the chapter 'The first concept' we will include a start script, a dispatch script and aparking script into the modification.

    The start script

    The start script will be loaded at the start of the freeplay mode, immediately after the loading screen. For this modification we want to place the vehicles directly onto the map, so the automatic positioning of the vehicle won't be needed in this project. In our script, the script will assign speed limits and key bindings to the vehicles.

    Quellcode

    1. void Start()
    2. {
    3. //Block//Vehicles////////////////////////////////////////////////////////////////////////////////
    4. VehicleList vl("Fahrzeug_1"); //Name of the vehicle on the map
    5. if(vl.GetNumVehicles() == 0)
    6. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    7. else
    8. {
    9. Vehicle v(vl.GetVehicle(0));
    10. v.SetSpeed(10.0f); //Speed value
    11. }
    12. //Block//Vehicles////////////////////////////////////////////////////////////////////////////////
    13. //If there are additional vehicles, copy the block and change values
    14. //Block//Dispatchers////////////////////////////////////////////////////////////////////////////////
    15. PersonList pl("Disponent_1");
    16. if(pl.GetNumPersons() == 0)
    17. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    18. else
    19. Game::AddToGroup(pl.GetPerson(0), 0); //Last number is equal to the code block
    20. //0 equal to code block 1
    21. //1 equal to code block 2
    22. //2 equal to code block 3
    23. //...
    24. //Block//Dispatchers////////////////////////////////////////////////////////////////////////////////
    25. //If where should be additional dispatchers, copy the block and change values
    26. };
    27. bool OnLoad()
    28. {
    29. Start();
    30. Process::Kill();
    31. return true;
    32. }
    Alles anzeigen
    At this point we won't explain this very shorty script. Adding additional vehicles is more interesting for the further modding steps. At first we start with the three vehicles of the fire department: The ELW (battalion chief), DLK (aerial ladder vehicle) and HLF (rescue squad). We have assigned the number of seats and necessary commands in the editor. But we can't assign the vehicle speed in the editor - this part will be done by the start script.

    Vehicles in the start script

    Fire department

    Quellcode

    1. VehicleList vl("ELW_Haupt"); //Name of the ELW (battalion chief) of the fire department on the map
    2. if(vl.GetNumVehicles() == 0)
    3. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    4. else
    5. {
    6. Vehicle v(vl.GetVehicle(0));
    7. v.SetSpeed(12.0f); //Value of speed
    8. }
    9. VehicleList vl("DLK_Haupt"); //Name of the DLK (aerial ladder vehicle) of the fire department on the map
    10. if(vl.GetNumVehicles() == 0)
    11. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    12. else
    13. {
    14. Vehicle v(vl.GetVehicle(0));
    15. v.SetSpeed(9.0f); //Value of speed
    16. }
    17. VehicleList vl("HLF_Haupt"); //Name of the HLF (rescue squad) of the fire department on the map
    18. if(vl.GetNumVehicles() == 0)
    19. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    20. else
    21. {
    22. Vehicle v(vl.GetVehicle(0));
    23. v.SetSpeed(10.0f); //Value of speed
    24. }
    Alles anzeigen
    We've chosen the speeds 12 for the ELW, 9 for the DLK and 10 for the HLF. You can change this values at any time later. Double check the names in the script, otherwise it won't work!

    Volunteer fire department
    Here we use the same procedure as for the fire department.

    Quellcode

    1. VehicleList vl("MTW_FFW"); //Name of the MTW of the volunteer fire department
    2. if(vl.GetNumVehicles() == 0)
    3. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    4. else
    5. {
    6. Vehicle v(vl.GetVehicle(0));
    7. v.SetSpeed(12.0f); //Value of speed
    8. }
    9. VehicleList vl("LF16_FFW"); //Name of the LF of the volunteer fire department
    10. if(vl.GetNumVehicles() == 0)
    11. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    12. else
    13. {
    14. Vehicle v(vl.GetVehicle(0));
    15. v.SetSpeed(8.0f); //Value of speed
    16. }
    Alles anzeigen
    Rescue station
    We use the same procedure again.

    Quellcode

    1. VehicleList vl("RTW1"); //Name of the first ambulance
    2. if(vl.GetNumVehicles() == 0)
    3. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    4. else
    5. {
    6. Vehicle v(vl.GetVehicle(0));
    7. v.SetSpeed(11.0f); //Value of speed
    8. }
    9. VehicleList vl("RTW2"); //Name of the second ambulance
    10. if(vl.GetNumVehicles() == 0)
    11. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    12. else
    13. {
    14. Vehicle v(vl.GetVehicle(0));
    15. v.SetSpeed(11.0f); //Value of speed
    16. }
    17. VehicleList vl("NEF"); //Name of the emergency doctors vehicle
    18. if(vl.GetNumVehicles() == 0)
    19. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    20. else
    21. {
    22. Vehicle v(vl.GetVehicle(0));
    23. v.SetSpeed(13.0f); //Value of speed
    24. }
    Alles anzeigen

    Dispatcher in the start script

    The dispatcher will be availabe to dispatch units. For this reason, we will assign the dispatch commands to the dispatcher later. In this example we'll use three different dispatchers. The first one will handle all battalion alarms, the second one only fire department and the third one rescue units.

    Quellcode

    1. PersonList pl("Disponent_1"); //Name of the first dispatcher
    2. if(pl.GetNumPersons() == 0)
    3. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    4. else
    5. Game::AddToGroup(pl.GetPerson(0), 0); //Dispatcher on code block 1
    6. PersonList pl("Disponent_2"); //Name of the second dispatcher
    7. if(pl.GetNumPersons() == 0)
    8. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    9. else
    10. Game::AddToGroup(pl.GetPerson(0), 1); //Dispatcher on code block 2
    11. PersonList pl("Disponent_3"); //Name of the third dispatcher
    12. if(pl.GetNumPersons() == 0)
    13. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    14. else
    15. Game::AddToGroup(pl.GetPerson(0), 2); //Dispatcher on code block 3
    Alles anzeigen

    The names of the vehicles and persons have to be equal in the editor and the script. Otherwise there will be malfunctions!



    The finnished start script

    Quellcode

    1. void Start()
    2. {
    3. VehicleList vl("ELW_Haupt"); //Name of the ELW (battalion chief) of the fire department on the map
    4. if(vl.GetNumVehicles() == 0)
    5. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    6. else
    7. {
    8. Vehicle v(vl.GetVehicle(0));
    9. v.SetSpeed(12.0f); //Value of speed
    10. }
    11. VehicleList vl("DLK_Haupt"); //Name of the DLK (aerial ladder vehicle) of the fire department on the map
    12. if(vl.GetNumVehicles() == 0)
    13. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    14. else
    15. {
    16. Vehicle v(vl.GetVehicle(0));
    17. v.SetSpeed(9.0f); //Value of speed
    18. }
    19. VehicleList vl("HLF_Haupt"); //Name of the HLF (rescue squad) of the fire department on the map
    20. if(vl.GetNumVehicles() == 0)
    21. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    22. else
    23. {
    24. Vehicle v(vl.GetVehicle(0));
    25. v.SetSpeed(10.0f); //Value of speed
    26. }
    27. VehicleList vl("MTW_FFW"); //Name of the MTW of the volunteer fire department
    28. if(vl.GetNumVehicles() == 0)
    29. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    30. else
    31. {
    32. Vehicle v(vl.GetVehicle(0));
    33. v.SetSpeed(12.0f); //Value of speed
    34. }
    35. VehicleList vl("LF16_FFW"); //Name of the LF of the volunteer fire department
    36. if(vl.GetNumVehicles() == 0)
    37. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    38. else
    39. {
    40. Vehicle v(vl.GetVehicle(0));
    41. v.SetSpeed(8.0f); //Value of speed
    42. }
    43. VehicleList vl("RTW1"); //Name of the first ambulance
    44. if(vl.GetNumVehicles() == 0)
    45. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    46. else
    47. {
    48. Vehicle v(vl.GetVehicle(0));
    49. v.SetSpeed(11.0f); //Value of speed
    50. }
    51. VehicleList vl("RTW2"); //Name of the second ambulance
    52. if(vl.GetNumVehicles() == 0)
    53. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    54. else
    55. {
    56. Vehicle v(vl.GetVehicle(0));
    57. v.SetSpeed(11.0f); //Value of speed
    58. }
    59. VehicleList vl("NEF"); //Name of the emergency doctors vehicle
    60. if(vl.GetNumVehicles() == 0)
    61. System::Log("Vehicle not found: %s", vl.GetVehicle(0)->GetName());
    62. else
    63. {
    64. Vehicle v(vl.GetVehicle(0));
    65. v.SetSpeed(13.0f); //Value of speed
    66. }
    67. PersonList pl("Disponent_1"); //Name of the first dispatcher
    68. if(pl.GetNumPersons() == 0)
    69. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    70. else
    71. Game::AddToGroup(pl.GetPerson(0), 0); //Dispatcher on code block 1
    72. PersonList pl("Disponent_2"); //Name of the second dispatcher
    73. if(pl.GetNumPersons() == 0)
    74. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    75. else
    76. Game::AddToGroup(pl.GetPerson(0), 1); //Dispatcher on code block 2
    77. PersonList pl("Disponent_3"); //Name of the third dispatcher
    78. if(pl.GetNumPersons() == 0)
    79. System::Log("Dispatcher not found: %s", pl.GetPerson(0)->GetName());
    80. else
    81. Game::AddToGroup(pl.GetPerson(0), 2); //Dispatcher on code block 3
    82. };
    83. bool OnLoad()
    84. {
    85. Start();
    86. Process::Kill();
    87. return true;
    88. }
    Alles anzeigen

    If there are any failures and you aren't able to solve the problem by your own, you can open a new topic in the Scripting department of the EMERGENCY community message boards. You should attach your complete script and the logfile (can be found in the EMERGENCY 4 main folder) in order to get quick help.

    This information is important for all problems with scripts, not only for the start script.



    The dispatch scripts

    Create the first dispatcher

    As mentioned, we'll use three dispatchers. The first dispatcher can dispatch all battallions, the second one can dispatch all fire department vehicles (fire department and volunteer fire department). There's a different between the two departments, because at the regular fire department the fire fighters will stay on the station all day and the volunteer fire fighters have to get to the station first. The third dispatcher can dispatch all rescue service vehicles. Technically it's the same system as for the fire department. Below you can find the basic version of the script, we will make some changes later.

    Quellcode

    1. const char ICON[] = "";
    2. const char CURSOR[] = "";
    3. const char VEHICLE_NAME[] = "";
    4. const char VIRTUAL_OBJECT[] = "";
    5. const char SOUND[] = "";
    6. const char MESSAGE[] = "";
    7. const char NUM_PERSON[] = "";
    8. const char PROTO_PERSON[] = "";
    9. float WAIT_PERSON = 5;
    10. float WAIT_VEHICLE = 20;
    11. bool vehiclefull = false;
    12. object Alarm_ELW : CommandScript
    13. {
    14. Alarm_ELW()
    15. {
    16. SetIcon(ICON);
    17. SetCursor(CURSOR);
    18. SetValidTargets(TARGET_STREET);
    19. SetValidTargets(TARGET_FLOOR);
    20. }
    21. bool CheckTarget(GameObject *Caller, Actor *Target, int childID)
    22. {
    23. return true;
    24. }
    25. void PushActions(GameObject *Caller, Actor *Target, int childID)
    26. {
    27. Vector APos = Game::GetCommandPos();
    28. VehicleList vl(VEHICLE_NAME);
    29. if(gol.GetNumObjects()==0)
    30. {
    31. System::Log("%s not found", VEHICLE_NAME);
    32. return;
    33. }
    34. Vehicle v(vl.GetVehicle(0));
    35. ActorList al(VIRTUAL_OBJECT);
    36. if(al.GetNumActors()==0)
    37. {
    38. System::Log("%s not found", VIRTUAL_OBJECT);
    39. return;
    40. }
    41. Vector SPos = al.GetActor(0)->GetPosition();
    42. v.SetUserData(1);
    43. Audio::PlaySample(SOUND);
    44. Mission::PlayHint(MESSAGE);
    45. for(int i = 1; i <= NUM_PERSON; i++)
    46. {
    47. Person p = Game::CreatePerson(PROTO_PERSON, "Unnamed");
    48. p.SetRole(ROLE_SQUAD);
    49. p.SetUpgradeLevel(3);
    50. Game::FindFreePosition(&p, spawnp, 100.f);
    51. p.SetPosition(SPos);
    52. p.PushActionShowHide(ACTION_NEWLIST, true);
    53. p.PushActionWait(ACTION_APPEND, WAIT_PERSON);
    54. p.PushActionShowHide(ACTION_APPEND, false);
    55. p.PushActionMove(ACTION_APPEND, v.GetPosition());
    56. p.PushActionEnterCar(ACTION_APPEND, &v);
    57. if(i == NUM_PERSON)
    58. vehiclefull = true;
    59. }
    60. if(vehiclefull)
    61. {
    62. v.PushActionWait(ACTION_NEWLIST, WAIT_VEHICLE);
    63. v.PushActionMove(ACTION_APPEND, Apos);
    64. }
    65. }
    66. };
    Alles anzeigen

    This script won't be changed that much for usual. The only changse will be made in the first part. The script was programmed in a very clean way, so every variable, duration, prototype and name is defined in the first eight lines of the script.

    Add additional dispatcher

    The third dispatcher is responsible for the mecial services. We'll start with an ambulance.

    Quellcode

    1. const char ICON[] = "RTW1";
    2. const char CURSOR[] = "RTW1";
    3. const char VEHICLE_NAME[] = "";
    4. const char VIRTUAL_OBJECT[] = "";
    5. const char SOUND[] = "";
    6. const char MESSAGE[] = "";
    7. const char NUM_PERSON[] = "";
    8. const char PROTO_PERSON[] = "";
    The variables ICON and CURSOR are the names of the graphics, which will be displayed in the interface by clicking the vehicle or person. You'll only have to assign a name, the game will choose the corresponding graphic from the UI folder.

    const char VEHICLE_NAME[] = "RTW1";

    Equal to the start script, the name of the vehicle will be assigned with this step. The game knows then, to which vehicle the persons have to go.

    const char VIRTUAL_OBJECT[] = "Spawn_RTW1";

    The variable VIRTUAL_OBJECT is the name of the Virtual Object, where the persons will be generated.

    Quellcode

    1. const char SOUND[] = "mod:Audio/Alarm/Piepser.wav";
    2. const char MESSAGE[] = "Alarm for ambulance 1";
    At this point, you can assign a sound file - e.g. for the alarm sound - to the action. You'll have to insert the path to the .wav file. Usually sound files will be placed in the folder Audio. But you can place them where you want, if you assign the correct path. The variable Message defines a text which will be shown in the blue information window in the upper part of the screen.

    Quellcode

    1. const char NUM_PERSON[] = "2";
    2. const char PROTO_PERSON[] = "mod:Prototypes/Persons/Ambulance/paramedic.e4p";
    Here you can define the number of persons which will be generated by the script. An ambulance has two persons for common. Below you can define to path of the prototype file of the persons. Repeat this steps for the second ambulance, the emergency doctors vehicle, the battalion chiefs car, the aerial ladder vehicle and the engine.

    If you test the script and persons won't be generated, check the path at first!

    158 mal gelesen