Stendhal NPC Coding: Difference between revisions

From Arianne
Jump to navigation Jump to search
Content deleted Content added
imported>Kymara
No edit summary
imported>Kymara
No edit summary
Line 3: Line 3:
{{Stendhal NPCs}}
{{Stendhal NPCs}}


== Adding NPCs ==
Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests. It is because of that reason, that is so important that you add NPC on the zone they are.
Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests. It is because of that reason, that is so important that you add NPC on the zone they are.



Revision as of 22:46, 4 November 2010


Stendhal NPCs

Usually we add NPCs (non-player characters) to make world more alive and to use them in Quests. It is because of that reason, that is so important that you add NPC on the zone they are.

This is how NPCs should be added to the world.

SpeakerNPC npc = new SpeakerNPC() { ... } creates a new NPC. When added to a zone, NPCs are added it to a global list so next time you need to get the NPC for participating in a quest, or if you want to teleportto it, you can call it by its name using NPCList.get().get(name). <source lang="java"> SpeakerNPC npc = new SpeakerNPC("name") {

   /...
   });

</source>

An example is:

<source lang="java">

   SpeakerNPC npc = new SpeakerNPC("Mr Healer") {
       protected void createPath() {
           List<Node> nodes=new LinkedList<Node>();
           nodes.add(new Path.Node(9,5));
           nodes.add(new Path.Node(14,5));
           setPath(nodes,true);
       }
       protected void createDialog() {
           // Lets the NPC reply with "Hallo" when a player greets him
           addGreeting();
           // Lets the NPC reply when a player says "job"
           addJob("I have healing abilities and I heal wounded players. I also sell potions and antidotes.");
           // Lets the NPC reply when a player asks for help
           addHelp("Ask me to #heal you and I will help you or ask me #offer and I will show my shop's stuff.");
           // Makes the NPC sell potions and antidote
           addSeller(new SellerBehaviour(shops.get("healing")));
           // Lets the NPC heal players for free
           addHealer(0);
           addGoodbye();
       }
   });
   zone.assignRPObjectID(npc);
   // This determines how the NPC will look like.
   npc.setEntityClass("welcomernpc");
   // Set the initial position to be the first node on the Path you defined above.
   npc.setPosition(9, 5);
   npc.initHP(100);
   zone.add(npc);    

</source>

The NPC is added to a list of NPC from where you can later obtain it for adding more dialogues for quests. So, it is very important to make sure the name you give to your NPC is unique.

It is a good idea to create a path that the NPC will follow on that area. Just used your tiled map or walk around in game and check coordinates to choose your nodes. You can also make the NPC stand still by using this instead:

<source lang="java">

   protected void createPath() {
       // NPC does not move
       setPath(null);
   }

</source>

Your NPC will stand still at whatever point you set as the initial position with npc.setPosition(x, y);.

And then create a dialog. Dialogs and such are explained in Quest sections, but you should find the exmaple above covers most options and the options like addHelp are quite self-explanatory.

We set its outfit either by:

  • setting its class to a PNG image that exists at data/sprites/npc
  • setting its outfit with setOutfit method.

See How to know player's outfits specifications

Finally set its initial position and its HP. Don't worry for your NPC. It can't be attacked nor killed.

Once that is done add the NPC to zone using zone.add() method.

We need to tell the zone to load this configuration class file. The NPC in the example lives in the ados area, so the information is stored in data/conf/zones/ados.xml. The map she's on is called int_ados_magician_house, so the information is inside the <zone name="int_ados_magician_house" file="interiors/ados/magician_house.tmx"> </zone> section. To load the java class file you created just add the line <source lang="xml">

 <configurator class-name="games.stendhal.server.maps.ados.magician_house.WizardNPC" />

</source> right after the <zone ... tmx"> Check the location of the file matches the path set in the class name!