0 votes
by (2k points)

So I'm following a guide that was attached to the zip, and there are many terms that I don't understand, but for now I will ask just a few I couldn't quite find in motorslave or just couldn't understand even there. So this what part of a passage code says:

''List of items in "Test":'' <<set _Items = UInv.GetItemsArray("Test")>><<if _Items.length == 0>>(empty)<<else>><<for _i = 0; _i < _Items.length; _i++>>_Items[_i] (<<print UInv.BagHasItem("Test", _Items[_i])>>)<<if _i < _Items.length - 2>>, <<elseif _i < _Items.length - 1>><<if _Items.length > 2>>,<</if>> & <</if>><</for>><</if>>

So far, what I understand is that _ variables are temporary, set is for seting a variable to something. == is evaluated to true if both sides are the same. else and if I understand them so if there are no items I guess the message is empty. <<for>> is something I don't quite get yet, but I know it repeats the execution of its contents, which is harder for me to understand than you would think, but I believe it can be used to make lists acording to motorslave. _i is a temporary variable, but I don't know it's purpose. what's the point of the ; between 0 and _i is another thing I don't understand. For now those are my questions. Sorry for being a nuisance.

1 Answer

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

Just to make it a bit easier to read:

''List of items in "Test":''
<<set _Items = UInv.GetItemsArray("Test")>>
<<if _Items.length == 0>>
	(empty)
<<else>>
	<<for _i = 0; _i < _Items.length; _i++>>
		_Items[_i] (<<print UInv.BagHasItem("Test", _Items[_i])>>)
		<<if _i < _Items.length - 2>>, <<elseif _i < _Items.length - 1>>
			<<if _Items.length > 2>>,<</if>> & <</if>>
	<</for>>
<</if>>

Now, the <<for>> macro is explained in the SugarCube documentation (see link).

The "_i = 0" sets _i to 0 initially, the "_i < _Items.length" is how the loop determines when to stop (when that is no longer true), and the "_i++" causes the value of _i to increase by 1 after each time through the loop.  Looking at the SugarCube documentation should help explain SugarCube macros, functions, and methods like these.

As for the rest, _Items is set to an array by the UInv.GetItemsArray() function.  Arrays have a .length property, which tells how many elements are in it, and those elements will be numbered from 0 to .length - 1.  So if the array's .length property is 5, then the items in that array will be at array indexes 0 to 4.  The UInv.BagHasItem() function returns the number of items the bag has with that name, which can also act as a kind of true/false value, since 0 is a falsy value, and any other number is a truthy value.

So, what this <<for>> loop does is go through each item in the array and display each item's name ("_Items[_i]") and how many of that item there are ("<<print UInv.BagHasItem("Test", _Items[_i])>>").  If there are no items, then it will display "(empty)" instead.

If you still find <<for>> loops confusing, you could probably just use the UInv.DisplayItemList() function instead, since it handles doing the loop for you.  Examples of its use exist in the sample code in passages 4-7, plus the simple "<<print UInv.DisplayItemList("Test")>>" example in the "Let's begin" passage.

Hope that helps!  :-)

by (2k points)
Thank you. Yeah, I try to read the motorslave page before asking something, but it is still difficult for me to understand what most things do. I do understand the english, but then I'm like: "ok, what are loops for in programing?" I google it and read a few sites, and then I'm like "ok, and how does that work with this?". I guess it will take longer than just a year to be able to call myself decent at using Twine. Hey, 2 questions. First: I found some instructions you gave another guy some time ago here: http://twinery.org/questions/37545/editing-f2andys-inventory-system-for-multiple-items-eat-wear is this still relevant? and secon question: Do you have Patreon?
by (41.9k points)
Regarding your first question, yeah, that's all still valid if you're using UInv.  It should all still be valid when I release the next version of UInv as well, as I slowly get closer to the full release.  I try to note anything which might break backwards compatibility in the changelog.

Regarding your second question, I've been considering it.  Considering how broke I am, I probably should set up a Patreon page.  :-P
by (2k points)
You should get one, your work is too good to not get some contribution. That way you'd also be able to share your games and codes and also taking small requests for a fair price. You could coach morons like me on the usage of Twine for 30 dollars per half an hor and 50 dollars an hour, or something like that. Idk, man, just throwing in some ideas. I wouldn't mind donating you to motivate you to keep going with your codes. Same goes for other seniors of this site, like greyelf. Don't get too greedy tho, remember that the community is not that big. You could get extra money with the donations for your codes, but I doubt it would be enough to make a living, even a cheap living.
by (41.9k points)
edited by

FYI, I took your advice, so if you're still interested, I have Patreon and PayPal donation links now.

Thanks, and have a good New Year!

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...