ZingMS

Would you like to react to this message? Create an account in a few clicks or log in to continue.
ZingMS

ZingMS! Ultimate MapleStory gaming experience :)


    Basic tutorial in scripting NPCs

    avatar
    Rukia
    Zing's Slave


    Posts : 7
    Join date : 2010-08-02

    Basic tutorial in scripting NPCs Empty Basic tutorial in scripting NPCs

    Post  Rukia Tue Aug 03, 2010 6:48 pm

    Here is a guide to scripting NPC's. Its really very easy and there's so much you can do with it.

    All credits go to iamSTEVE from RageZone.


    I got tired of everyone releasing simple-ass NPCs that do the same thing, so I decided to write a guide because really, it isn't hard.
    Ok, starting off, all methods in your NPC scripts are defined by a source file called NPCConversationManager in your net.sf.odinms.scripting.npc package, or it might be in AbstractPlayerInteraction (the class is not an abstract class) which has methods that are universal or all scripting, meaning if you put a method in there, you can use it in ANY script.
    For exmaple, if I put a method like this
    public void setHp(int newhp) {
    c.getPlayer().setHp(newHp);
    }

    In abstractPlayerInteraction, you can use it in ANY script, for example you could do cm.setHp(5), pi.setHp(5), or anything, (pi is for portals)
    The thing in the paranthesis after the declaration of the method is called a parameter, something you have to provide when using the method. You may put a method with no parameter if you dont need any parameters.
    The basic NPC Script
    You will need to start the NPC script with variable, status, which is used in any NPC that is properly scripted to keep it from flooding everything at once.
    So at the top of your script, add
    var status = 0;
    The var status is set to 0 because that's just a better way to do it, you can set it to 5 if you want, or even 666, but that just makes your code weird and harder to read.
    All NPCs that are properly scripted have two functions start and action.
    Usually, in the start function, all that is set is the status and action.
    You should always set the status to -1 unless you're making a weird-ass NPC and action doesnt matter right now, most of the time it should be (1, 0, 0)
    So your start function should look like this
    function start() {
    status = -1;
    action(1, 0, 0);
    }

    And your action method, which should be the body of your NPC, aka what it does (say stuff, open a shop, warp, give an item, whatever). This part will mostly be coded with the methods in your NPCConversationManager and AbstractPlayerInteraction, with a cm. in front of it. For example, your NPCConversationManager has a openshop method inside of it, which is a void. (If you dont then you have a weird source) (A void means it has no return value, we'll go into more detail about that later)
    So to use this method, you would look at the parameter, which is an integer value, so you would have to fill in an integer value for the parameter and use the method like this: cm.openShop(INTVALUEHERE).
    Usually the name of the parameter should give you an idea what it is, like int shopid should give you a clue that the parameter is the shopId.
    If there is nothing in the paranthesis, that means that you dont need to add anything in the paranthesis, such as getJob, you do not need a parameter for that so you can just use it as cm.getJob();
    NOTE: Captilization matters, openSHOP isn't the same as openShop.

    When you use a method, you need to add a ; after all of them Ex: cm.sendOk("Hi");
    You miss the semicolon and it wont work.
    Some of the most commonly used methods are listed
    cm.sendOk(String text) : Sends a box with the text in the paranthesis (Note: All strings must have "Quotes" around them
    cm.sendNext(String text) : Like sendok except with a next button
    cm.sendNextPrev (String text): Like sendNext except with a next and a previous button
    cm.openShop(int shopid): Opens the shop with the specified ID
    cm.getPlayer() : This is a huge one, it lets you access most of the methods in the 2nd biggest file in your OdinMS folder (unless yours is really fucked up), MapleCharacter, but it simply gets the object of the player, and you can apply maplecharacter methods here such as saveToDb and other important ones, you will use this A LOT.
    cm.dispose(): disposes the NPC, ends the instance, whatever, use this when you want the NPC box to close, you should always add this method to the end of your NPC scripts, or the player will have to relog after using the NPC to talk to another NPC.
    Too lazy to add the rest, just read your NPCConversationManager

    Now time for the hard part (not really, still really easy Razz) the NPC body.
    First you have to start your action function which should be like this
    function action(mode, type, selection) { //close this bracket when the NPC ends

    Now to code this usually you start off with something like
    if (mode == -1) {
    cm.dispose();
    } else {
    if (mode == 0 && status == 0) {
    cm.dispose();
    return;
    }
    if (mode == 1)
    status++;
    else
    status--;

    To read this requires, *gasp*, COMMON SENSE
    if (mode = -1) happens if the mode is negative 1, which in that case the NPC does nothing.
    if (mode == 1) status++;, this increments status by 1 if the mode is 1, which allows you to use status correctly and code your NPC.
    But before we get started on coding your body, I have to explain if, else, and else if statements which are very basic.
    If statements are used like this
    if (BOOLEAN GOES HERE) {
    BLAHBLAHBLAH;
    } else {
    BLAHBLAH
    }
    Basically, a boolean returns true or false, it can be something like a cm method that returns a boolean (public boolean <methodname>) or something that returns true or false, such as (mode == 1) (NOTE: = is not the same as ==, = assigns a value to a variable, == is comparing to see if the two values are the same)

    If the boolean in paranthesis returns true, the BLAHBLAHBLAH gets execute (put whatever you want there, such as cm.sendOk("Whatever")Wink if not, it executes the else, simple right?
    Now how you work this with status is that everytime your NPC does something, the status is incremented, so simply code your NPC like this.
    if (status == 1) {
    whateveryouwanttodohere;
    } else if (status ==2) {
    blahblah;
    }else if (status == 3) {
    hhhhhh;
    }
    and so on

    You should try to limit it to 1 line per status if possible, so if you want an NPC to say hi to a player, you could do this (example)
    Note: anything with // in front of it is a comment, which is completely ignored when the NPC is executed
    //This only includes the action part of the action that makes the NPC do stuff
    if (status == 1) {
    cm.sendOk("Hi, would you like a brownie?");
    } else if (status == 2) {
    cm.sendOk("Well too bad, you're a fag and you're not getting one.");
    }

    So now put the whole script together
    var status = 0; //status

    function start() {
    status = -1;
    action(1, 0, 0);
    }

    function action(mode, type, selection) {
    if (mode == -1) {
    cm.dispose();
    } else {
    if (mode == 0 && status == 0) {
    cm.dispose();
    return;
    }
    if (mode == 1)
    status++;
    else
    status--;
    //This only includes the action part of the action that makes the NPC do stuff
    if (status == 1) {
    cm.sendOk("Hi, would you like a brownie?");
    } else if (status == 2) {
    cm.sendOk("Well too bad, you're a fag and you're not getting one.");
    cm.dispose(); //ends the NPC and exits window
    }
    } //closes the action function

    And you got yourself a NPC!
    What's different about coding NPCs is the body, there are many methods in your NPCConversationManager, and you can even add your own, I suggest you first start out reading NPCConversationManager and make sure you understand what all the methods in there do, this will help you when you want to code NPCs that do more than sit and talk, because that isn't very useful, this guide was made in hope of stopping the flood of useless NPC releases, I dont know if this was helpful, I hope it was atleast feel free to ask me any questions, but keep in mind that I dont even have a basic knownledge of javascript and all the statements I know in JS are usually just simple statements in java that are universal (like if elses are practically the same in every language). I'm not great at writing guides, so in case I didnt explain something clearly, let me know (post on thread dont PM) and I will try to help you if I can and Im not too lazy.

      Current date/time is Sat Apr 27, 2024 6:36 pm