
So I'm trying to feed something back in to set the bio field of a character (A shop) And it seems to work perfectly fine, I can get everything to log properly and ultimately end up with a string that I want. But, after everything is said and done when I do shop.set("bio", "This string"); I get an error; TypeError: Cannot read property 'length' of null
at evalmachine.<anonymous>:2207:39
at eval ( This is how I set up the bio field. Item Shop Inventory
--item|+3 Longsword --value|3300 --received|1500101 --material|0 --enchant|3 --sold|No
--item|+2 Armor --value|2150 --received|1500101 --material|0 --enchant|2 --sold|No But for everything I use .length on, has a value. It will only throw the error if I try and set the bio field. (Doesn't matter what the string is) If I comment the line out, everything goes back to working fine. I'm so close to getting a working shop for the party up and running. Here's the code. var Shop = Shop || {};
on("chat:message", function (msg) {
if (msg.type != "api") return;
var command = msg.content.split(" ", 1);
if (command == "!runShop") {
var shop = findObjs({
_type: "character",
name: "Shop"
})[0];
shopAttr = function(shopAttrName) {
return findObjs({
_type: "attribute",
_characterid: shop.get("_id"),
name: shopAttrName
})[0];
};
shop.get("bio", function(bio) {
var bioCopy = bio.replace(/ --/g, "--");
log(bioCopy);
var itemIndex = bioCopy.match(/--item.*?(?=<br>|$)/g);
log(itemIndex);
//Create a list of all the items in the shop
var shopItems = [];
for ( var i=0; i<itemIndex.length; i++) {
var n = itemIndex[i].split("--");
var a = 1;
var chatObj = {};
var mTags = "";
var mContent = "";
while(n[a]) {
mTag = n[a].substring(0,n[a].indexOf("|"));
mContent = n[a].substring(n[a].indexOf("|") + 1);
chatObj[mTag] = mContent;
a++;
};
shopItems.push(chatObj);
};
log(shopItems);
//Load calander and get date
var calander = findObjs ({_type: "character", name: "*Calander"})[0].get("_id");
var calanderAttrName = ["Year", "Month", "Weekday", "Day"];
var calanderValue = [];
var dateLine = "";
for (var i=0;i<calanderAttrName.length;i++){
var calanderAttrValue = findObjs({_type:"attribute", _characterid:calander, name:calanderAttrName[i]})[0].get("current") + "";
if (calanderAttrName[i] != "Weekday") {
if (parseInt(calanderAttrValue) < 10) {var calanderAttrValue = "0" + calanderAttrValue;};
dateLine = dateLine + calanderAttrValue;
};
calanderValue.push(parseInt(calanderAttrValue));
};
dateLine = parseInt(dateLine);
log(calanderValue);
log(dateLine);
var soldItemsValue = 0;
var shopSold = shopAttr("Sold");
var shopSold_Cur = parseInt(shopSold.get("current"));
var shopLevel = shopAttr("Level");
var shopLevel_Cur = parseInt(shopLevel.get("current"));
var updatedBio = "Item Shop Inventory<br>";
log(shopItems.length);
for (var i=0; i<shopItems.length; i++){
//Is item sold?
if (shopItems[i].sold != "No") continue;
//Otherwise, find sale date
var itemMaterial = parseInt(shopItems[i].material);
var itemEnchant = parseInt(shopItems[i].enchant);
var forsaleDays = (((5 + itemMaterial ) * itemEnchant ) / ((shopLevel_Cur) / 5 ));
log(forsaleDays);
var forsaleMonths = 0;
while(forsaleDays > 30) {
forsaleDays -= 30;
forsaleMonths += 100;
};
var forsaleTime = forsaleMonths + forsaleDays;
log(forsaleTime);
var received = parseInt(shopItems[i].received);
var bestBefore = received + forsaleTime;
log(bestBefore);
if (dateLine >= bestBefore) {
shopItems[i].sold = calanderValue[3] + "." + calanderValue[1] + "." + calanderValue[0];
soldItemsValue = parseInt(shopItems[i].value);
log(soldItemsValue);
shopSold_Cur += soldItemsValue;
};
log(shopItems[i]);
updatedBio += "--item|" + shopItems[i].item + " --value|" + shopItems[i].value + " --received|" + shopItems[i].received + " --material|" + shopItems[i].material + " --enchant|" + shopItems[i].enchant + " --sold|" + shopItems[i].sold + "<br>";
};
log(shopItems);
log(updatedBio);
log(shopSold_Cur);
//My problem child. Even if I set the string to "This is a string" it still gives the error.
shop.set("bio", updatedBio);
});
};
});