2)
This method includes having a textfile for each item
These textfiles you want to store under the
levels/ somewhere. I'll state why later. Let's choose the folder:
levels/itemdata
You'll probably also need to add levels/itemdata/*.txt into folder config.
It's also possible to use the file prefix .arc, but this is personal preference
as they are in the end, both text files.
It's also possible to use .ini files, then I suggest you lookup some of Inverness' scripts in the code gallery. I will not cover that though
In each textfile for each item you want to use a setup like this:
NPC Code:
itemname=Big Sword
type=weapon
subtype=twohanded
slot=primary
customdata=slow,common
This is because you can load all of the data needed by using obj.loadvars()
If you saved the above file as
bigsword.txt you could do:
PHP Code:
temp.itemdata.loadvars("levels/itemdata/bigsword.txt");
echo(temp.itemdata.itemname); // would echo Big Sword
This we can use in a DBNPC to cache the items.
Here it would be better to use TStaticVar's to store the item data
Not using TStaticVar would mean extra work and opens for errors when going
file -> array
PHP Code:
// DBNPC: ItemDatabase
function onInitialized() onCreated();
function onCreated() {
this.itemDB = this;
cacheItems();
}
function cacheItems() {
// Load the levels/itemdata/ folder
temp.folder.loadfolder("levels/itemdata/*.txt", 0);
for(temp.file : temp.folder) {
cacheItem(temp.file);
}
// A simple failsafe, if one of the cacheItem() functions below
// add anything into the this.errors array, display that here!
if (this.errors.size() < 1) {
this.cached = true;
}
else {
echo("[ItemDatabase]: There was an error when caching!");
echo("-- Errors: "@ this.errors);
}
}
function cacheItem(file) {
temp.data.loadvars("levels/itemdata/"@ file);
temp.dynamic_varnames = temp.data.getdynamicvarnames(); // store it for reuse
// Check if we actually got any data
if (temp.dynamic_varnames.size() > 0) {
temp.variable = "this.item_" @ temp.id; // Store the wanted variable name
// Get string id by stripping the ending .txt from the file
temp.id = file.substring(0, file.length() - 4);
/* Go through each of the dynamic variables
(This is excluding the default variables like timeout, scriptlogerrors etc) */
for(temp.var : temp.dynamic_varnames) {
makevar(temp.variable @ "." @ temp.var) = makevar("temp.data." @ temp.var);
}
/* It is possible to use obj1.copyfrom(obj2), but I'm not using it
since it does more than just copying the variables, and you don't
need more than that. Personal Preference though.
I've also experienced some oddities when using this while using
custom object names */
}
// Found no dynamic variables, add an error into this.errors!
else {
this.errors.add("No data found for \""@ file @"\"");
}
}
// Ways to retrieve item data
public function getitem(id) {
return makevar("this.item_"@ id);
}
public function getitemvar(id, var) {
return makevar("this.item_" @ id @ "." @ var);
}
We now have a cache for our textfiles. We can retrieve the item data by using
PHP Code:
temp.itemdata = ItemDB.getitem("bigsword");
echo(temp.itemdata.slot); // primary
You can now use the ItemDatabase DBNPC to retrieve information about items you need info about.
Now, the reason you want all of your text files into
levels/ is because of this one event which allows you to create automatic updating of itemcache when editing files!
PHP Code:
function onLevelFileUpdated(filename) {
// replace "itemdata/" with the folder you use
if (!filename.starts("itemdata/") || !filename.ends(".txt")) return;
// This expects to find something on the first line.
// No reason the leave the top line empty (Not sure if it reads linebreaks.. hm)
// Edit the if statement below if you want another behavior chech for
// empty files
temp.testfile.loadlines("levels/"@filename);
if (temp.testfile[0] == NULL) {
// The file has been deleted, do something about that here!
}
else {
// The files has been updated, do something about that here!
}
// Lets retrieve the updated file
temp.tokens = filename.tokenize("/");
temp.file = temp.tokens[temp.tokenst.size()-1]; // example: bigsword.txt
temp.item = temp.file.substring(0, temp.file.length()-4); // example: bigsword
// Use the temp. files above to do something when an item's textfile has been
// deleted or updated/edited! :)
// CODE HERE
}
If the examples are a bit tough to follow, just give me a PM and I'll look into simplifying them
This is just an idea of how you can do it using files and a DBNPC!

(It's also possible to use a WNPC to cache items, just saying!)
All the scripts I have written from the top of my head, so look at it as "gs2 psuedocode" :v I have not tested the scripts and they are not finished ones. The Database scripts could probably work if pasted, but these two methods are just to give you an idea of how to do it.