Scripting Tutorial

From PMDOWiki

This tutorial goes into the specifics of adding NPCs to an existing map, and giving them interactivity.

Start the game in Dev Mode and select “Edit Ground” in the Dev Controls.

Adding Entity

Start by opening an existing ground map. We will open Data\Ground\cliff_camp.rsground

Switch to the “Entities” tab, and create a new Character object with these settings:

Don’t forget to switch to “Add New” Mode!

Setting the trigger type to “Action” is what allows the character to be spoken to; the two callbacks listed below are the ones available when choosing that trigger type. They will be referred to later.

Char display allows you to change everything about the character’s sprite.

You can specify a nickname for the character; if left blank, the species name is used.

Click on the position in the map to place the character.

Save the map.

Setting Strings

Switch to the Strings tab, and add dialogue.

Save the map as you would normally, to save the changes to the strings.

Scripting

Switch to the “Script” tab and open the script directory for this map:

Alternatively, you can find the directory manually by going to Data\Script\ground\cliff_camp

Open init.lua in order to edit the logic behind the ground map.

There’s plenty of existing code here, you will need to add the following code:

function cliff_camp.Meditite_Action(chara, activator)
  DEBUG.EnableDbgCoro() --Enable debugging this coroutine
  GROUND:CharTurnToChar(chara,CH('PLAYER'))--make the chara turn to the player
  UI:SetSpeaker(chara)--set the dialogue box's speaker to the character
  UI:WaitShowDialogue(STRINGS:Format(MapStrings['Meditite_Line_001']))
  UI:WaitShowDialogue(STRINGS:Format(MapStrings['Meditite_Line_002']))
end

Whenever you speak to an object, the game looks for a method named <Object name>_Action and calls it. This is why our function is named Meditite_Action. The first argument, chara, is the character being spoken to (Meditite). The second argument, activator, is the player object.

Save the file with your edits, go back to the ground editor and reload the scripts.

Exit the ground editor, and you will be returned to the title screen. Fast-travel to the cliff camp, and the Meditite should be added.

If you experience issues, check the logs for the most recent errors in your current session. They are found in the LOGS folder.

Further reading

Scripting Cheat Sheet