I want to modify the standard inventory output to add the item’s weight, but I cannot seem to guess the phrase needed to list the contents of a container carried by the player. Here is my attempted code:
Instead of taking inventory:
let W be a real number;
say "You are wearing:[line break]";
repeat with Item running through the list of Equipment worn by player:
increase W by weight of item;
say "[item] ([weight of item] lb.) [line break]";
say "[line break]You are carrying [line break]";
repeat with Item running through the list of Equipment carried by player:
if item is a container:
list contents of item;
repeat with subitem running through open container carried by player:
say "[subitem]: ([weight of subitem] lb.) [line break]";
otherwise:
say "[item] ([weight of item] lb.) [line break]";
increase W by weight of item;
say "Total weight: [W] lb.";
Stephane: I think that worked, but some other lines of code are confusing the matter. Why is Inform’s syntax so complicated for these kinds of things (no pun intended). I now have the following code, but the container itself is not being printed, but I may be able to fix that.
Instead of taking inventory:
let W be a real number;
say "You are wearing:[line break]";
repeat with Item running through the list of Equipment worn by player:
increase W by weight of item;
say "[item] ([weight of item] lb.) [line break]";
say "[line break]You are carrying [line break]";
repeat with Item running through the list of Equipment carried by player:
if item is a container:
list subcontents of item;
otherwise:
say "[item] ([weight of item] lb.) [line break]";
increase W by weight of item;
say "Total weight: [W] lb.";
To list subcontents of (C - a container):
repeat with LocalItem running through things which are in C:
say "[LocalItem]: ([weight of LocalItem] lb.) [line break]";
I think I see it. If the item is a container you call for listing its subcontents but nowhere do you list the container itself.
Try something like this (Inform not handy so this is pseudocode)
To list subcontents of (C - a container):
say "[C]: ([weight of C] lb.). Inside of [C] you find:";
repeat with LocalItem running through things which are in C:
say "[LocalItem]: ([weight of LocalItem] lb.) [line break]";
Edit: You’ll also need a tweak for if the container is empty. And I’m not sure if the container actually has its own weight separate from its contents.
This is because you’re only printing “[item]” if it’s not a container; tweaking the carried loop like this should work:
repeat with Item running through the list of Equipment carried by player:
say "[item] ([weight of item] lb.) [line break]";
if item is a container:
list subcontents of item;
increase W by weight of item;
(NB I copied and pasted this so the tabs/spacing are probably messed up).
For inventory management, I admit to relying entirely on the code proposed by Emily Short in example 177 of the WWI manual (Equipment List - divided tall type). On the one hand, because Emily Short’s examples are very educational and help me progress a lot, and on the other hand because her code is understandable (and written using many approaches throughout the manual), easily reusable and adaptable. I bet I’m not the only one!
After printing the name of a thing while taking inventory:
Say " (weight: [weight] lbs)"
Without tweaking anything else, that gets you an inventory listing like this:
You are carrying:
a sword (weight: 10 lbs)
a leather armour (weight: 20 lbs) (being worn)
a money pouch (weight: 1 lbs)
a backpack (weight: 5 lbs) (being worn)
a tinderbox (weight: 2 lbs)
I’ll give that a try. In my case, it was not opening containers and skipping contents of closed containers. I see that in your example, it displayed tinderbox, os I’ll try again. Is your backpack open in your example?
The backpack in the example is just a wearable container (not openable). I didn’t realise that you also wanted to change the default behaviour of only listing the player’s visible inventory. If the player picks up a locked strongbox that they do not yet have the key for, do you want the INVENTORY command to say what’s inside it?
You make a good point. I guess I am trying to make “Take Inventory” do too much. I want to list all the items’ weights for a total weight the player is carrying. If I overload the Inventory command, it will pre-empt these other details, like what is inside a locked box. I will make a new command like “Find Weight”.
You know, if it can help, you can create actions that are not available to the player but remain available to the author and the game. You “just” need not to associate an Understand command with them. You can even use these actions during other actions available to the player, to make them more complete, possibly under certain conditions.
Thanks. Good tip. I think I have the code I want (almost). For closed containers, it will not display the contents, but the combined weight. Even if you can’t see what’s in a locked box, you can tell if it is extra heavy or not. I’ll show the code when it’s complete for the benefit of others.
I have it working. Here is a list of inventory with weights of each. If the container is closed, it does not display the contents, but tallies the weight of the filled container anyway.
Instead of taking inventory:
let totWt be 0.0;
say "You are wearing: [line break]";
repeat with Item running through the list of Equipment worn by player:
increase totWt by weight of item;
say "[item] ([weight of item] lb.) [line break]";
say "[line break]You are carrying [line break]";
repeat with Item running through the list of Equipment carried by player:
if item is container:
let FW be 0.0;
now FW is weight of item;
if Item is skein:
increase FW by fluidWt in skein;
otherwise:
increase FW by weight of contents of item;
increase totWt by FW;
say "[item] ([FW] lb.) [line break]";
if item is open:
list subcontents of Item;
otherwise:
increase totWt by weight of item;
say "[item] ([weight of item] lb.) [line break]";
say "Total weight: [totWt] lb.";
There are a few To decide phrases, but they are obvious.
Thanks for sharing this code. Your game project seems to lean towards a rather simulationist approach (even though I understand the value of indicating the presence of a more or less heavy mass in a container that can’t be opened). Is it like this in all aspects of the game? Are you perhaps creating a sort of roguelike, with a well-defined universe?