0 votes
by (270 points)
edited by

Thank you again! Your help lead me on a google journey of JS discovery and I am certainly the better for it.  The biggest question (that I asked in the comment https://twinery.org/questions/4638/help-needed-multislot-clothing-object-based-inventory-system?show=5590#c5590) relates the secondary part of the orignal question.  In the comment I made you can see what I ended up doing to get close to the effect I was looking for, but I still have a small issue.  In short I now have a passage that just runs a description macro for the item passed to it, but my equip/unequip links error out.  One of the relevant lines being:

temp.wiki(' ' +'<<link "Equip">> <<unequipClothingJS ' + item + '>> <</link>>');

I think it has to do with trying to put the object in the text string, earlier I had gotten around what i think is the same problem by adding a new property (lname) that is a string of the object name which produced this:

.wiki('[[ ' + item.name + '\u00A0\u00D7' + item.count + '|itemExamine][$currentitem = $' + item.lname + ']] <br>');

I am not real pleased with this work around and I feel like in one of your examples you did something like this without needing a string name property, but I can't seem to find it now.

 

 

1 Answer

0 votes
by (68.6k points)
selected by
 
Best answer

Your example was something of a mess, so it's not entirely clear what you're actually doing.  Being able to look at the project would help.

That said, some things in your example stand out:

  1. You declare $currentitem as an array in StoryInit, but elsewhere use it to hold a single item object.
  2. The first argument to the <<ItemDescrip>> macro seems that it should be an object, however, you attempt to use it in a few places as though it were a string—notably, within the temp.wiki() calls.  You should probably be doing something like the following there:
    temp.wiki('<<link "Unequip">><<unequipClothingJS $player $' + item.lname + '>><</link>>');
  3. When creating the markup to invoke the <<unequipClothingJS>> macro within <<ItemDescrip>>, you only attempt to pass it a single argument, the item, when it expects two, the character and the item.

 

As to needing the "$currentitem = $' + item.lname + '" bit.  Since you're creating markup strings and rendering them, you're going to have to do something inelegant there to pass objects around, so one bad choice is as good as another.  As an example of something else inelegant you could do, which would not require the use of the item's lname property:

State.temporary.item = item;
temp.wiki('<<capture _item>><<link "Unequip">><<unequipClothingJS $player _item>><</link>><</capture>>');

 

As to your slot conflict checking code.  What you have should work, though you're doing it backwards.  I'm not sure I'd recommend using ES2015 arrow functions, however, unless you're okay with your project not working in older browsers.

by (270 points)

Sorry for the disorganization, you are extremely helpful in spite of myself.

1.) Started typing before i had thought that one through, thanks for pointing it out.

2.) I had tried to leave that as an error to point out the issue I was running into, thanks for providing a cleaner version though.

3.) DOH! that was silly

Thank you for pointing me to the capture methodology and pointing out that either way it is a bit cumbersome.  I have to decide which I want to go with now, but at least you provided me with the solution I was hoping to find, but couldn't.  I really appreciate this paragraph and snippet.

Hmm, more to think about.  I need to reread how the arrow works I guess.  I was just looking for a solution that did not require an explicit for loop, and did  not realizing how the arrow worked.

 

		if (conflicts.some(function(r) {
			return Slots.includes(r)})) 

would be the expanded version correct?  

 

by (68.6k points)

I need to reread how the arrow works I guess.

It's not a matter of how arrow functions work, it's a matter of the syntax simply doesn't exist in older browsers.  My usual advice is to write code that works in all browsers supported by the story format you're using—and SugarCube supports browsers that don't understand any ES2015 syntax features.

Whether or not your player pool will include anyone who uses such a browser, I couldn't say.

 

		if (conflicts.some(function(r) {
			return Slots.includes(r)})) 

would be the expanded version correct? 

Yes.  Though, I'm not a fan of omitting the semi-colon at the end of the return statement.

Additionally.  As I look back at the code again, you only use conflicts and Slots for that one test, so you really don't need to store either.  Just store the results of the test.  For example:

		var $list = $(document.createDocumentFragment());

		var hasConflict = Object.keys(character.clothing)
			.filter(function (key) {
				return character.clothing[key];
			})
			.some(function (slot) {
				return clothing.clothingType.includes(slot);
			});

		if (hasConflict) {

 

Finally.  As I look back at the code again, I've noticed something that I hadn't before.  Your calls to this.error() really should terminate/exit the handler, however, as far as I can see, you're not doing that anywhere.  For example, calls like the following:

this.error("…");

Should instead be something like:

return this.error("…");

 

by (270 points)
edited by
The rereading comment was about me needing to figure out how the arrow notation really worked so I could expand it properly, I had just altered an example.  Thank you for the cleaned up section of code.  I had created the intermediate variables as steps in figuring out how I was going to resolve the comparison, but was confused enough by the arrow notation that i was having trouble condensing it.  Your method saves the variables and is honestly more understandable, though tbh reading it one go like that still twists my brain.  Thanks for pointing out the this.error errors, I think i propagated an error from one example .  

Not sure what the policy is on sharing files, but here is my example project if anyone ends up wanting to see what i did (remove spaces).  I think maybe I am comfortable enough with it now to add it to my real project (not to say it's perfect, but hopefully it's enough to work from now).  Thank you for all of your help TheMadExile.

https://drive.google.com      /open?id=1a4LVtiSXb9psbVl_CUbYAWAhTI64ddFT
...