A Beginner's Guide to Scripting Page 2

From The DarkMod Wiki
Jump to: navigation, search

"Teach Yourself Scripting In One Day!" by Fidcal

Page 1 | Page 2 | Page 3

written by Fidcal

On this page we shall begin writing scripts that can perform real tasks in your fan missions.

  • You should already understand the scripting essentials described on the previous page.
  • You should have created a simple reasonably large one room map named testScripts.map as described on the previous page at Create a Test Map
  • You should have created a script file named testScripts.script in the same folder as the map file as described on the previous page at Creating a Script File

How to Activate, Trigger, or Frob, an Entity

Activating an entity is to trigger it into action eg, from a targetting entity. 'Frob' really means the player activates it while it is highlighted so strictly speaking we are just activating it from script, eg, to trigger a door to open or close.

First create a door in the test map and name it TestDoor.

From script we use the script event (command or instruction) activate. As described in the Creating a Function section on the previous page, script events need to be used with an entity. Previously we used print with the sys entity, ie sys.print. Now we use the activate script event with our door entity - but we need to get the name of the door entity and tell the script compiler that the word is an entity name as described in the following section...

Using a Named Entity in a Map from Script

One easy way to refer to an entity in your map is to put the dollar sign $ at the front of the entity name. The script then knows it is an entity name in your map. So we named the door we made TestDoor so in script we can refer to it as $TestDoor. So, just as we joined sys with print to get sys.print we join $TestDoor with activate to get $TestDoor.activate.

You recall sys.print was then followed by the data in brackets - we put the text or values we wanted to print. With activate we have to put a triggering entity as described in the next section...

The Triggering Entity

The data in brackets that follows activate must be a triggering entity. Where there is no special triggering entity needed, use player1 which, preceded by $ to indicate it is an entity, becomes $player1. So...

The Complete activate System Event

Our complete system event is thus $TestDoor.activate($player1)

So now we are ready to include this in our script...

The script to Activate an Entity

We can use the same script function we wrote to send debug text to the console in the first page of this tutorial. Delete any extras about declaring and printing variables to leave just...

void SendToConsole()
 	sys.print("\n\nYour script is running");

Rename the function to something like TriggerDoor or ActivateDoor or even FrobDoor whatever you like. Then insert our activate line under the sys.print line. Finally, let's put a comment line at the top to get into the habit of documenting our scripts so we, and others, know what they do. So the script function should now look something like this....

// JoeBloggs: this script triggers a door to open and close in a test map

void FrobDoor()
	sys.print("\n\nYour script is running");

One last thing is needed to get this to work in our map. Change the function name in the call property of the target_tdm_callscriptfunction entity. Remember, this is the entity that actually starts the script working when triggered by the lever in our test map. So, the property should now be something like...

call FrobDoor.

All that remains is to map the map in Dark Mod and try it out. Each time you frob the lever the door should open and close remotely. If it fails to work:

  • Check you can see 'Your script is running' in the console.
  • Check every detail in the script is correct. A tiny typo and it might not work at all.
  • Check the 'call' property in your target_tdm_callscriptfunction entity has the value that matches the name of the script function in your script file.
  • Check the name of the door entity matches that in the $TestDoor.activate($player1); system event line in your script.

WORK IN PROGRESS...............

"Teach Yourself Scripting In One Day!" by Fidcal

Page 1 | Page 2 | Page 3