There are some methods which you could take a look at instead of having each item having it's own weapon script.
- DBNPC (functions as a cache) -> Script
- Files (slow-cache) -> DBNPC (Loaded into a DBNPC to function as a "faster-cache") -> Script
- Files -> Script (DEPRECATED, do not use this method. File I/O alone is slow as hell compared to caching the items in a DBNPC)
Method 1 and 2 are the most used ones. DO NOT USE METHOD 3
Using method 2 you only need filebrowser to edit, create and remove items.
This could be good for staff who don't need NC or doesn't know how to script to make adjustments to items. Should only be handles by trusted persons or a System Administrator or something of that kind.
1)
You have a database with all the data for each item. Within this DB you create an API to include retrieving the wanted information. Something like this could work (Basic idea though)
PHP Code:
//DBNPC: ItemDatabase
function onInitialized() onCreated(); // redirect to onCreated() when server restarts etc
function onCreated() {
ItemDB = this; /* This will function as a global pointer serverside.
Scripts serverside can access this DB by doing
ItemDB.function() or ItemDB.variable etc */
this.init = true; // Scripts can check ItemDB.init to see if items are initialized
createItemData();
}
function createItemData() {
/* Here you can either give each item an unique numerical ID or
a unique string ID. Possible to both as well though! */
// Example of variable setup
this.vars = {"itemname", "type", "subtype", "slot", "customdata"};
// Numerical ID examples
this.item_1 = {"Big Sword", "weapon", "twohanded", "primary", {"slow"}};
this.item_2 = {"Small Sword", "weapon", "onehanded", "primary", {"fast"}};
// String ID examples
this.item_bigsword = {"Big Sword", "weapon", "twohanded", "primary", {"slow"}};
this.item_smallsword = {"Small Sword", "weapon", "onehanded", "primary", {"fast"}};
/* Important Notice!
If you want something more flexible you can also store
each item as an TStaticVar object */
}
// This is a simple way to get itemdata
/* Possible to do:
temp.item = ItemDB.getitem("bigsword"); OR
temp.item = ItemDB.getitem(1);
You can now use temp.item to read info about said item. */
public function getitem(id) {
return makevar("this.item_" @ id);
}
/* Just need a variable from an item?
temp.itemname = getitemvar(1, "itemname"); -> "Big Sword"
This function will function faster (Not really noticed unless you have hundreds of variables) when using TStaticVar objects
then you can just do:
return makevar("this.item_" @ id @ "." @ var);
*/
public function getitemvar(id, var) {
temp.index = this.vars.index(var);
if (temp.index > -1) {
return makevar("this.item_" @ id)[temp.index];
}else {
return 0;
}
By using this you can either create an WNPC or a create a class which you join to the player to handle all the item actions.
I would suggest joining a class to the player. Then you
access the player object by using
this.var,
this.function() etc
player.join("itemcontrol"); <- Do this on login in Control-NPC
PHP Code:
// WNPC: ItemControl OR class: ItemControl
// Make sure you have access to the player object when working
// with a WNPC serverside!
public function equipitem(id) {
temp.item = ItemDB.getitem(id);
// Use the information gathered from the ItemDatabase
}
public function useitem(id) {
temp.item = ItemDB.getitem(id);
// Act based on the information retrieved, should the item be equipped?
// Summon something? Make you go super fast? Do that here
}
// ETC
This is just an example of how it could be done using a DBNPC -> Script. Use it as a base
