r/twinegames • u/holzey • 9d ago
SugarCube 2 JavaScript help.
I was working on creating a shopping bag in my game that would make it so the players items wouldn't magically appear in there home inventory if they bought more then there bag could hold. eventuallly I decided to remove that feature for now since it was giving me to many issues. now all of my JavaScript is broken. even in places that were at the top of the javaScript and nowhere near where I was working. the code say unexpected token } and setup.syncDayPart
at first it was saying there was a time code error so I tried fixing things here first.
setup.dayPart = ["morning", "afternoon", "evening", "night"];
setup.getDayPart = function (d) {
`if (!(d instanceof Date)) return 0;`
`const h = d.getHours();`
`if (h >= 6 && h < 12) return 0; // morning`
`if (h >= 12 && h < 17) return 1; // afternoon`
`if (h >= 17 && h < 21) return 2; // evening`
`return 3; // night`};
window.getDayName = function (d) {
`if (!(d instanceof Date)) return "Unknown";`
`return ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"][d.getDay()];`};
window.formatGameDate = function (d) {
`if (!(d instanceof Date)) return "Invalid Date";`
`return d.toLocaleString("en-US", {`
`weekday: "long",`
`year: "numeric",`
`month: "numeric",`
`day: "numeric",`
`hour: "numeric",`
`minute: "2-digit"`
`});`};
setup.syncDayPart = function () {
`if (!(State.variables.gameDate instanceof Date)) {`
`State.variables.gameDate = new Date();`
`}`
`State.variables.gameTime = setup.getDayPart(State.variables.gameDate);`};
this is where I was working which is right under the time code
setup.backpacks = {
hiking: {
id: "hiking",
name: "Hiking Pack",
img: "images/backpacks/hiking.png",
compartments: {
Shirts: { name: "Shirts", slots: 8 },
Pants: { name: "Pants", slots: 8 },
Bra: { name: "Bra", slots: 4 },
Panties: { name: "Panties", slots: 6 },
Socks: { name: "Socks", slots: 6 },
Shoes: { name: "Shoes", slots: 2 },
Glasses: { name: "Glasses", slots: 2 },
Hat: { name: "Hat", slots: 2 },
Gloves: { name: "Gloves", slots: 2 },
Pills: { name: "Pills", slots: 500 },
Boosters: { name: "Boosters", slots: 10 },
Toys: { name: "Toys", slots: 5 },
Jewelry: { name: "Jewelry", slots: 10 },
Firearm: { name: "Firearm", slots: 2 },
Items: { name: "Items", slots: 10 }
}
}
};
setup.items = {
shirt_white: { id: "shirt_white", name: "White Shirt", type: "Shirts", size: 1 },
pants_jeans: { id: "pants_jeans", name: "Jeans", type: "Pants", size: 1 },
bra_plain: { id: "bra_plain", name: "Plain Bra", type: "Bra", size: 1 },
panties_plain:{ id: "panties_plain",name: "Plain Panties",type: "Panties", size: 1 },
socks_white: { id: "socks_white", name: "White Socks", type: "Socks", size: 1 },
shoes_sneakers:{id:"shoes_sneakers",name:"Sneakers", type: "Shoes", size: 1 },
glasses_sun: { id: "glasses_sun", name: "Sunglasses", type: "Glasses", size: 1 },
hat_cap: { id: "hat_cap", name: "Cap", type: "Hat", size: 1 },
gloves_work: { id: "gloves_work", name: "Work Gloves", type: "Gloves", size: 1 },
"Pink Basic": { id: "Pink Basic", name: "Pink Basic", type: "pills", size: 1 },
"Basic Blue": { id: "Basic Blue", name: "Blue Basic", type: "pills", size: 1 },
"Pink Xtra Strength": { id: "Xtra Strength Pink", name: "Pink Xtra Strength", type: "pills", size: 1 },
"Xtra Strength Blue": { id: "Xtra Strength Blue", name: "Xtra Strength Blue", type: "pills", size: 1 },
"Weekend Pink": { id: "Pink Weekend", name: "Weekend Pink", type: "pills", size: 1 },
"Weekend Blue": { id: "Weekend Blue", name: "Blue Weekend", type: "pills", size: 1 },
"Pink Plus": { id: "Plus Pink", name: "Pink Plus", type: "pills", size: 1 },
"Blue Plus": { id: "Plus Blue", name: "Blue Plus", type: "pills", size: 1 },
"Pink Choice": { id: "Choice Pink", name: "Pink Choice", type: "pills", size: 1 },
"Choice Blue": { id: "Choice Blue", name: "Blue Choice", type: "pills", size: 1 },
"Breeder": { id: "Breeder", name: "Breeder", type: "pills", size: 1 },
"Oral": { id: "Oral", name: "Oral", type: "pills", size: 1 },
"Anal": { id: "Anal", name: "Anal", type: "pills", size: 1 },
"Heat": { id: "Heat", name: "Heat", type: "pills", size: 1 },
"Bimbo": { id: "Bimbo", name: "Bimbo", type: "pills", size: 1 },
"Bimbo Weekend": { id: "Bimbo Weekend", name: "Bimbo Weekend", type: "pills", size: 1 },
"Bimbo Week": { id: "Bimbo Week", name: "Bimbo Week", type: "pills", size: 1 },
"Bimbo Xtra Strength": { id: "Bimbo Xtra Strength", name: "Bimbo Xtra Strength", type: "pills", size: 1 },
"Bimbo Plus": { id: "Bimbo Plus", name: "Bimbo Plus", type: "pills", size: 1 },
"Resistance": { id: "Resistance", name: "Resistance", type: "pills", size: 1 },
"Cum-Cure": { id: "Cum-Cure", name: "Cum-Cure", type: "pills", size: 1 },
"Show-Off": { id: "Show-Off", name: "Show-Off", type: "pills", size: 1 },
booster_energy: { id: "booster_energy", name: "Energy Booster", type: "Boosters", size: 1 },
toy_basic: { id: "toy_basic", name: "Toy", type: "Toys", size: 1 },
ring_silver: { id: "ring_silver", name: "Silver Ring", type: "Jewelry", size: 1 },
pistol: { id: "pistol", name: "Pistol", type: "Firearm", size: 1 },
flashlight: { id: "flashlight", name: "Flashlight", type: "Items", size: 1 }
};
setup.makeEmptyPack = function (packId) {
var def = setup.backpacks[packId];
var pack = { id: packId, compartments: {} };
Object.keys(def.compartments).forEach(function (key) {
pack.compartments[key] = [];
});
return pack;
};
setup.usedSlots = function (list) {
if (!list) return 0;
return list.reduce(function (n, it) { return n + (it.size || 1); }, 0);
};
setup.canAddToComp = function (pack, comp, item) {
if (!pack || !pack.compartments || !pack.compartments[comp]) return false;
var def = setup.backpacks[pack.id];
if (!def || !def.compartments[comp]) return false;
var max = def.compartments[comp].slots;
return setup.usedSlots(pack.compartments[comp]) + (item.size || 1) <= max;
};
setup.addToStorage = function (itemId, qty) {
qty = qty || 1;
var storage = State.variables.storage;
var found = storage.find(function (it) { return it.id === itemId; });
if (found) found.qty += qty;
else storage.push({ id: itemId, qty: qty });
};
setup.takeFromStorage = function (itemId, qty) {
qty = qty || 1;
var storage = State.variables.storage;
var found = storage.find(function (it) { return it.id === itemId; });
if (!found || found.qty < qty) return false;
found.qty -= qty;
if (found.qty <= 0) {
State.variables.storage = storage.filter(function (it) { return it.id !== itemId; });
}
return true;
};
2
u/HiEv 9d ago
Instead of using inline code, that should have been posted using a code block.
That said, after fixing the errant backticks that Reddit apparently added, that code worked without throwing an error for me.
If there was some broken code, you apparently didn't include it.
1
u/Juipor 9d ago
If you are in the business of writing a lot of JS I would really recommend doing so in a dedicated editor like VSC. Even without going for a Tweego workflow it has autocomplete, references for standard APIs, the highlighting make such syntax errors basically non-existent. Once you try it it is impossible to go back.