[Add-On] Learning NPC scripts (beginner's guide)

Status
Not open for further replies.
Re: [Guide] Learning NPC scripts (beginner's guide)

@nCloud

the cm.haveitem part didn't have ( , ) , or { which you needed
Forgot a semi colon (;) at the end of the sendSimple
Forgot to use \r\n#L0#text#l to show the selection. (I just changed it to YesNo)

\r\n = skip one line
#L0# = selection 0 (also #L1# would be selection 1, #L2# would be selection 2, etc.
#l = ends selection

PHP:
var status = 0;   

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

function action(mode, type, selection) {   
        
    if (mode == -1) {   
        cm.dispose();   
    }   
    else {    
        if (status >= 2 && mode == 0) {    
            cm.sendOk("Goodbye");    
            cm.dispose();    
            return;    
        }    
           
        if (mode == 1) {   
            status++;   
        }       
        else {   
            status--;   
        }   
           
        if (status == 0) {
		    cm.sendNext("Why, hello there my fellow ninja. It seems you may be in need of some throwing stars...");
	    }
	    else if (status == 1) {
            if (cm.haveItem(ILBI ID HERE,1)) {
			    cm.sendYesNo("Ah, it seems you already have some. Shame. Do you want some more?");
		    }
		    else {
			    cm.sendOk("Ummm, seems you could use some. Here, take these.");
			    cm.gainItem(ILBI ID HERE, 5);
			    cm.dispose();
			}
		}
		else if (status == 2) {
            cm.sendOk("These should help you with your hunting");
            cm.gainItem(ILBI ID HERE, 5);
            cm.dispose();
		}	
	}
}
 
Re: [Guide] Learning NPC scripts (beginner's guide)

Some questions quick questions:) I'm noob sorry


Please explain what mode 1, 0, -1 does for me/

PHP:
        else {   
            status--;
What is the exact mode for this to occur ?

PHP:
var status = 0;   

function start() {   
    status = -1;

Why did you define variable status with the value of 0 and then instantly set it's value to -1 after.
Why is it -1 btw.

PHP:
    action(1, 0, 0);
Why is it 1, 0, 0

PHP:
        if (status >= 2 && mode == 0) {    
            cm.sendOk("Goodbye");    
            cm.dispose();    
            return;    
        }
Explain to me why is it >= 2 for status and why you need a return;
 
Re: [Guide] Learning NPC scripts (beginner's guide)

Hey everyone great guide but i'm having a bit of a problem...
I have this script and when I use the NPC it give me an error is the START command prompt thingy

Here's the script
Code:
/** Nanami of RageZone **/
/** Pleased if I helped ((: **/
importPackage(net.sf.odinms.client);
var status = 0;



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


function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
    } else {
        if (status >= 2 && mode == 0) {
            cm.sendOk("Friggin nib.");
            cm.dispose();
            return;
        }
        if (mode == 1)
            status++;
        else
            status--;
        if (status == 0) {
                cm.sendYesNo("You will need the following items to rebirth \r\n#ePyogo Mushroom\r\nBlood Dagger#n\r\nand #e300mil#n\r\nDo you wish to be reborn?");
            }
        else if (status == 1) {
            if ((cm.haveItem(1382016, 1)) && (cm.haveItem(1332052, 1)) && (cm.haveMeso(300000000))) {
            cm.sendYesNo("You sure you want to reborn?");
            }
            else if (!cm.haveItem(1382016, 1)) {
            cm.sendOk("Friggin nib, you are missing one or more of the items. Check your inventory then talk to me again.");
            cm.dispose();
            }
            else if (!cm.haveItem(1332052, 1)) {
            cm.sendOk("Friggin nib, you are missing one or more of the items. Check your inventory then talk to me again.");
            cm.dispose();
            }
            else if (!cm.haveMeso(300000000)) {
            cm.sendOk("Friggin nib, you are missing one or more of the items. Check your inventory then talk to me again.");
            cm.dispose();

            }
            
        }
        else if (status == 2) {
var rand = Math.floor(Math.random() * 27 + 1);
var scrolls = new Array(2040603, 2044503, 2041024, 2041025, 2044703, 2044603, 2043303, 2040807, 2040806, 2040006, 2040007, 2043103, 2043203, 2043003, 2040506, 2044403, 2040903, 2040709, 2040710, 2040711, 2044303, 2043803, 2040403, 2044103, 2044203, 2044003, 2043703);
if (rand == 1) {
            cm.gainItem(1382016, -1);
            cm.gainItem(1332052, -1);
            cm.gainItem(scrolls[rand-1], 1);
            cm.gainMeso(-300000000);
            cm.getChar().levelUp();
        cm.unequipEverything()
        cm.changeJob(net.sf.odinms.client.MapleJob.BEGINNER);
        cm.sendNext("Go level up you level 1 noob =D");
        cm.getChar().setLevel(2);
        cm.dispose();
        }
}
}
}



Here's the error
Code:
Dec 23, 2008 10:05:34 AM net.sf.odinms.scripting.npc.NPCScriptManager action
SEVERE: Error executing NPC script.
java.lang.reflect.UndeclaredThrowableException
at $Proxy3.action(Unknown Source)
at net.sf.odinms.scripting.npc.NPCScriptManager.action(NPCScriptManager.
java:50)
at net.sf.odinms.net.channel.handler.NPCMoreTalkHandler.handlePacket(NPC
MoreTalkHandler.java:2Cool
at net.sf.odinms.net.MapleServerHandler.messageReceived(MapleServerHandl
er.java:135)
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messa
geReceived(AbstractIoFilterChain.java:570)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageR
eceived(AbstractIoFilterChain.java:299)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(Abst
ractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.mess
ageReceived(AbstractIoFilterChain.java:648)
at org.apache.mina.filter.codec.support.SimpleProtocolDecoderOutput.flus
h(SimpleProtocolDecoderOutput.java:5Cool
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(Prot
ocolCodecFilter.java:180)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageR
eceived(AbstractIoFilterChain.java:299)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(Abst
ractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.mess
ageReceived(AbstractIoFilterChain.java:648)
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorF
ilter.java:220)
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.
run(ExecutorFilter.java:264)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source
)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnabl
e.java:51)
at java.lang.Thread.run(Unknown Source)
Caused by: java.security.PrivilegedActionException: javax.script.ScriptException
: sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method net.
sf.odinms.client.MapleCharacter.getPet(). (<Unknown>#40) in <Unknown> at line number 40
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.script.util.InterfaceImplementor$InterfaceImplementorInvocati
onHandler.invoke(Unknown Source)
... 19 more
Caused by: javax.script.ScriptException: sun.org.mozilla.javascript.internal.Eva
luatorException: Can't find method net.sf.odinms.client.MapleCharacter.getPet().
(<Unknown>#40) in <Unknown> at line number 40
at com.sun.script.javascript.RhinoScriptEngine.invoke(Unknown Source)
at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(Unknown So
urce)
at com.sun.script.util.InterfaceImplementor$InterfaceImplementorInvocati
onHandler$1.run(Unknown Source)
... 21 more


Any help on this would be VERY much appreciated <333
 
Re: [Guide] Learning NPC scripts (beginner's guide)

Not quite guys. Here you go:

@Xoti you forgot a { at the end of the cm.haveItem

PHP:
var status = 0;     

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

function action(mode, type, selection) {     
          
    if (mode == -1) {     
        cm.dispose();     
    }     
    else {      
        if (status >= 2 && mode == 0) {      
            cm.sendOk("Goodbye");      
            cm.dispose();      
            return;      
        }      
             
        if (mode == 1) {     
            status++;     
        }         
        else {     
            status--;     
        }     
             
        if (status == 0) {    
            cm.sendNext("Hello!");   
        } 
        else if (status == 1) {   
            if (cm.haveItem(2000005, 1)) {
                cm.sendSimple("You have power elixirs.\r\n#L0#Can I have some anyway?#l\r\n#L1#I'll see you later#l");  
            } 
            else { 
                cm.sendOk("You don't have a power elixir? Here I'll give you 10!");   
                cm.gainItem(2000005, 10);  
                cm.dispose(); 
            }
        } 
        else if (status == 2) {  
            if (selection == 0) {   
                cm.sendOk("Okay here you go");   
                cm.gainItem(2000005);   
                cm.dispose();   
            } 
            else if (selection == 1) {   
                    cm.sendOk("See you later");   
                    cm.dispose();   
            }   
        } 
    }  
}

Nope, it works perfectly (:
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@nCloud

the cm.haveitem part didn't have ( , ) , or { which you needed
Forgot a semi colon (;) at the end of the sendSimple
Forgot to use \r\n#L0#text#l to show the selection. (I just changed it to YesNo)

\r\n = skip one line
#L0# = selection 0 (also #L1# would be selection 1, #L2# would be selection 2, etc.
#l = ends selection

I forget the ; in C, I forget the ; in java :)

Thanks for the help. I'll be sure to post some more conplex scripts at some point.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

I have a question.
I'm searching a command to warp all players in a pq.
If u talk to the npc and with a party of 4/5/6 ppl that they can get warped to the pq map.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@Mal
Try this, you added way to many brackets.

Code:
/** Nanami of RageZone **/
/** Pleased if I helped ((: **/
importPackage(net.sf.odinms.client);
var status = 0;



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


function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
    } else {
        if (status >= 2 && mode == 0) {
            cm.sendOk("Friggin nib.");
            cm.dispose();
            return;
        }
        if (mode == 1)
            status++;
        else
            status--;
        if (status == 0) {
                cm.sendYesNo("You will need the following items to rebirth \r\n#ePyogo Mushroom\r\nBlood Dagger#n\r\nand #e300mil#n\r\nDo you wish to be reborn?");
            }
        else if (status == 1) {
            if (cm.haveItem(1382016, 1) && (cm.haveItem(1332052, 1)) && (cm.haveMeso(300000000)) {
            cm.sendYesNo("You sure you want to reborn?");
            }
            else if (!cm.haveItem(1382016, 1)) {
            cm.sendOk("Friggin nib, you are missing one or more of the items. Check your inventory then talk to me again.");
            cm.dispose();
            }
            else if (!cm.haveItem(1332052, 1)) {
            cm.sendOk("Friggin nib, you are missing one or more of the items. Check your inventory then talk to me again.");
            cm.dispose();
            }
            else if (!cm.haveMeso(300000000)) {
            cm.sendOk("Friggin nib, you are missing one or more of the items. Check your inventory then talk to me again.");
            cm.dispose();

            }
            
        }
        else if (status == 2) {
var rand = Math.floor(Math.random() * 27 + 1);
var scrolls = new Array(2040603, 2044503, 2041024, 2041025, 2044703, 2044603, 2043303, 2040807, 2040806, 2040006, 2040007, 2043103, 2043203, 2043003, 2040506, 2044403, 2040903, 2040709, 2040710, 2040711, 2044303, 2043803, 2040403, 2044103, 2044203, 2044003, 2043703);
if (rand == 1) {
            cm.gainItem(1382016, -1);
            cm.gainItem(1332052, -1);
            cm.gainItem(scrolls[rand-1], 1);
            cm.gainMeso(-300000000);
            cm.getChar().levelUp();
        cm.unequipEverything()
        cm.changeJob(net.sf.odinms.client.MapleJob.BEGINNER);
        cm.sendNext("Go level up you level 1 noob =D");
        cm.getChar().setLevel(2);
        cm.dispose();
        }
}
}
}
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@ MrMysterious

You're looking for explanations to understanding the beginning of the script. Look for Clysse's guide on analyzing NPC scripts. There's a link of it in the release section

Why do I use this top?

When I first started learning java scripting I always began the beginning of my script the same way. It never failed me so I'm attached to using it :):
 
Re: [Guide] Learning NPC scripts (beginner's guide)

You can also add the "canHold" command which I found useful when you use the "gainItem" command.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@ MrMysterious

You're looking for explanations to understanding the beginning of the script. Look for Clysse's guide on analyzing NPC scripts. There's a link of it in the release section

Why do I use this top?

When I first started learning java scripting I always began the beginning of my script the same way. It never failed me so I'm attached to using it :):

Nah I want your explanation =) clysse's is correct to some extent but not all of it is correct.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

I was wondering if there was a way that I could have an NPC script alter/add to a MySQL database depending on something such as cm.haveItem?

Thanks,
Cloud
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@ MrMysterious

You're looking for explanations to understanding the beginning of the script. Look for Clysse's guide on analyzing NPC scripts. There's a link of it in the release section

Why do I use this top?

When I first started learning java scripting I always began the beginning of my script the same way. It never failed me so I'm attached to using it :):

Still waiting for my answer.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

Hi.great guide btw.now i have a prob wif dis..
Code:
var status = 0;

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

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (status >= 2 && mode == 0) {
			cm.sendOk("Alright, see you next time.");
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			cm.sendNext("Hi, I'm the Items Exchanger! ");
		} else if (status == 1) {
			cm.sendNextPrev("Click next.")
		} else if (status == 2) {
			if (cm.haveItem(2000005)) {
				cm.sendNext("Great, you've clicked next");
			}
		} else if (status == 3) {
			cm.sendSimple("What would you like to exchange?\r\n#L0##bWizet Invincible Hat#l");
		} else if(status == 4) {
			if (selection == 0) { 
				if (cm.haveItem(2000005 >= 2000)){
					cm.sendOk("There you have it!");
					cm.gainItem(1002140, 1);
					cm.gainItem(2000005, -2000);
					cm.dispose();
				}
				else cm.sendOk("You do not have enough elixirs");	
				cm.dispose();
			}	
		}
	}
}
and when i tried clicking on the npc,and i exchanged,nothing came out..(when i clicked"Wizet Invincible Hat").......and i bugged myself.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

Hi.great guide btw.now i have a prob wif dis..
Code:
var status = 0;

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

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (status >= 2 && mode == 0) {
			cm.sendOk("Alright, see you next time.");
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			cm.sendNext("Hi, I'm the Items Exchanger! ");
		} else if (status == 1) {
			cm.sendNextPrev("Click next.")
		} else if (status == 2) {
			if (cm.haveItem(2000005)) {
				cm.sendNext("Great, you've clicked next");
			}
		} else if (status == 3) {
			cm.sendSimple("What would you like to exchange?\r\n#L0##bWizet Invincible Hat#l");
		} else if(status == 4) {
			if (selection == 0) { 
				if (cm.haveItem(2000005 >= 2000)){
					cm.sendOk("There you have it!");
					cm.gainItem(1002140, 1);
					cm.gainItem(2000005, -2000);
					cm.dispose();
				}
				else cm.sendOk("You do not have enough elixirs");	
				cm.dispose();
			}	
		}
	}
}
and when i tried clicking on the npc,and i exchanged,nothing came out..(when i clicked"Wizet Invincible Hat").......and i bugged myself.
Wrong usage of cm.haveItem .
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@priest - As Xoti said, you have used cm.haveItem wrong.

This would work fine.

PHP:
var status = 0;

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

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (status >= 2 && mode == 0) {
			cm.sendOk("Alright, see you next time.");
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			cm.sendNext("Hi, I'm the Items Exchanger! ");
		} else if (status == 1) {
			cm.sendNextPrev("Click next.")
		} else if (status == 2) {
			if (cm.haveItem(2000005) {
				cm.sendNext("Great, you've clicked next");
			}
		} else if (status == 3) {
			cm.sendSimple("What would you like to exchange?\r\n#L0##bWizet Invincible Hat#l");
		} else if(status == 4) {
			if (selection == 0) { 
				if (cm.haveItem(2000005, 2000) {
					cm.sendOk("There you have it!");
					cm.gainItem(1002140, 1);
					cm.gainItem(2000005, -2000);
					cm.dispose();
				}
				else cm.sendOk("You do not have enough elixirs");	
				cm.dispose();
			}	
		}
	}
}

You only need to have an exact amount in you must have 2000 of 2000005 to obtain it. Even if you have more, your script is only going to take 2000 so this would work fine.


EDIT - Oh, I also noticed you double bracketed on the cm.haveItem commands , yours looked like this:
(cm.haveItem 2000005, 2000)) <---- You only need 1 )
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@priest - As Xoti said, you have used cm.haveItem wrong.

This would work fine.

PHP:
var status = 0;

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

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (status >= 2 && mode == 0) {
			cm.sendOk("Alright, see you next time.");
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			cm.sendNext("Hi, I'm the Items Exchanger! ");
		} else if (status == 1) {
			cm.sendNextPrev("Click next.")
		} else if (status == 2) {
			if (cm.haveItem(2000005) {
				cm.sendNext("Great, you've clicked next");
			}
		} else if (status == 3) {
			cm.sendSimple("What would you like to exchange?\r\n#L0##bWizet Invincible Hat#l");
		} else if(status == 4) {
			if (selection == 0) { 
				if (cm.haveItem(2000005, 2000) {
					cm.sendOk("There you have it!");
					cm.gainItem(1002140, 1);
					cm.gainItem(2000005, -2000);
					cm.dispose();
				}
				else cm.sendOk("You do not have enough elixirs");	
				cm.dispose();
			}	
		}
	}
}

You only need to have an exact amount in you must have 2000 of 2000005 to obtain it. Even if you have more, your script is only going to take 2000 so this would work fine.


EDIT - Oh, I also noticed you double bracketed on the cm.haveItem commands , yours looked like this:
(cm.haveItem 2000005, 2000)) <---- You only need 1 )
wow thanks!but when it comes to this line:
Code:
                if (cm.haveItem(2000005, 2000) {
you need another ')' to close the code,is it?or it will just only close the '(' beside 2000005
 
Re: [Guide] Learning NPC scripts (beginner's guide)

wow thanks!but when it comes to this line:
Code:
                if (cm.haveItem(2000005, 2000) {
you need another ')' to close the code,is it?or it will just only close the '(' beside 2000005

Ah, I ment you only need the 1 ')' to close the '(' beside 2000005

i.e (2000005, 2000)

Glad I could help.
 
Re: [Guide] Learning NPC scripts (beginner's guide)

@priest - As Xoti said, you have used cm.haveItem wrong.

This would work fine.

PHP:
var status = 0;

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

function action(mode, type, selection) {
	if (mode == -1) {
		cm.dispose();
	} else {
		if (status >= 2 && mode == 0) {
			cm.sendOk("Alright, see you next time.");
			cm.dispose();
			return;
		}
		if (mode == 1)
			status++;
		else
			status--;
		if (status == 0) {
			cm.sendNext("Hi, I'm the Items Exchanger! ");
		} else if (status == 1) {
			cm.sendNextPrev("Click next.")
		} else if (status == 2) {
			if (cm.haveItem(2000005) {
				cm.sendNext("Great, you've clicked next");
			}
		} else if (status == 3) {
			cm.sendSimple("What would you like to exchange?\r\n#L0##bWizet Invincible Hat#l");
		} else if(status == 4) {
			if (selection == 0) { 
				if (cm.haveItem(2000005, 2000) {
					cm.sendOk("There you have it!");
					cm.gainItem(1002140, 1);
					cm.gainItem(2000005, -2000);
					cm.dispose();
				}
				else cm.sendOk("You do not have enough elixirs");	
				cm.dispose();
			}	
		}
	}
}

You only need to have an exact amount in you must have 2000 of 2000005 to obtain it. Even if you have more, your script is only going to take 2000 so this would work fine.


EDIT - Oh, I also noticed you double bracketed on the cm.haveItem commands , yours looked like this:
(cm.haveItem 2000005, 2000)) <---- You only need 1 )

Missing ')' .
 
Status
Not open for further replies.
Back