#RESERVED
I plan on expanding on things as I go... this one post may look short but expand things and you'll realize it doesn't have much room left. Simplest way is just me reserving another post right now to help where I can.
If you have any questions, please post them here and I'll add them to the faq as soon as possible including any possible examples and code snippets.
I still don't understand npcs!!!
A:
This will serve as a full guide to coding npcs in EXTERNAL NPC SCRIPTS!!
Step 1: Add the npc to the server!
You can do this from the database itself but there's a simpler way! COMMANDS!
For those of you who actually bothered to read the commands in the source you would have noticed there is an /addnpc command. You MUST be a gm to use these commands (open the accounts table and set permission to like 5 or something.)
Reading through this command we see we have 3 values to enter...
The Npc ID (script number)
The Npc Mesh (the look of the npc)
The Npc Name (the name of it! *duh*)
So we can do /addnpc ID Mesh Name
But what if we don't know mesh numbers?
Well. There's a command for that too!
/npc Mesh
Will spawn a npc of mesh of our choice where we are located.
NOTE: for those who don't know.. the last digit = direction (IE: 10-19 are same npc... they just change direction)
For this example we'll do... /addnpc 1337 2697 ElitePVPers
So now that we have our npc placed via our cmd we need to create a script! (Notice it shows instantly and is instantly usable but has no script. There is no need to ever restart server in this example!)
(yes... I enabled mobs on mine, deal with it :P)
So... lets go to our debug/npcs folder!
You should see lots of different external scripts in here (notepad works fine for editing, they are plain text). Try opening one and look around if you want.
Lets create our own 1337.npc file using notepad or by copy/pasting an existing npc and renaming the ID.
Lets now paste this text into it for our first simple test.
Code:
def npc(Client, Option):
if(Option == 0):
Text("This is a blank npc which we can now code any way we like!")
Link("Cool", 255)
Finish()
return 0
Save the file and click the npc again
OOH MY GOD IT TALKS!
There's no reason to need to re-compile the server or anything like that. They work instantly which lets you keep your server running while you fix any bugs in the npc and you still have full access to your character attributes.
So lets go over some main differences in python...
Instead of brackets we will be using tabs to indicate what goes with what (keep sections of code together essentially)
Instead of && / || or w/e we will be using actual words so like...
if (Client.Level > 120 and Client.Level < 130):
Further note that... if statements are different. After the if you MUST place a colon (':')
The statements instead of flowing as
if
else if
else
are now
if
elif
else
It's simple changes but could save you some problems with debugging.
Now, lets write our npc to be a bit more advanced... how about we have it tell us our level and then if over level 120 ask us if we want a free dragon ball?
Here's the full code I'd use personally... Try to break it down and learn from it
Code:
def npc(Client, Option):
if(Option == 0):
Text("Hey there " + Client.Name + " you are level " + str(Client.Level)+" ")
if(Client.Level > 120):
Text("This makes you able to get free db's! Would you like one?")
Link("Yes Please", 1)
Link("No thanks", 255)
else:
Text("Come back when over 120 to get free dragonballs!")
Link("I'll be back", 255)
Finish()
if(Option == 1):
if(Client.Inventory.Count < 40):
Client.AddItem(1088000)
Text("Check your inventory! You have your free db!")
else:
Text("You do not have enough inventory space!")
Link("Thanks", 255)
Finish()
return 0
[GIANT WARNING]
These python scripts are picky with a few different things you may not be used to!
YOU MUST DO ALL SPACING USING ACTUAL SPACES! 5 SPACES PER INDENTATION. IF YOU DO NOT LIKE THIS THEN USE A PYTHON EDITOR SUCH AS IDLE WHICH DOES IT FOR YOU
YOU MUST CAST ALL NON STRINGS TO STRINGS WHEN PRINTING OUT IN TEXT!
Eg: if you tried to use...
Text("you are level " + Client.Level)
you will get an error.
You must use
Text("you are level " + str(Client.Level))
This is because python, unlike C# does not immediately consider it a string so in this case it's trying to do a mathematical addition vs a string addition. This will obviously cause a problem (think about it... if I said "add 'yes' and 1 and give me the answer", chances are you'd ask me what yes was... is it a number? am I just saying 'yes1', etc.
Yes, python scripts will take a bit of getting used to but they are really cool once you get the hang of them.
*cough* IDLE can be downloaded from the python website... there is also some AWESOME documentation/guides on there.
Mini guide to using Idle: