Hi all,
Right now, I've got a very makeshift inventory system going. Basically, I've created a variable for the player's weapon and armor ($player.weapon and $player.armor respectively), and I'm changing the value depending on what they've got in the slot.
However, it occurs to me that this isn't the most flexible approach I could be using, and doesn't leave a lot of growth room for different items or scenarios. E.g. Having multiple weapons in your inventory, dropping weapons, selling weapons, using one-use items, etc. Ideally, the inventory would be accessible from the side-menu - I already know how to create links in it and have already added a few.
I'm sure I could
probably come up with some sort of system to do this. But I'm pretty sure given the prevalence of SugarCube games, I'd probably be reinventing the wheel.
In short, does anyone have any inventory system code that I could take and throw into my game? I've used Google to scour the site, but I haven't run across any inventory system code that seems explicitly safe to use for Sugarcube 2.0.
A link to an existing explanation is fine if I'm just doubling up on a common request, which I'm fairly sure I am.
Comments
You may wish to start looking at the Making sidebar Inventory items droppable thread, specifically at my first post about an object-based version of the inventory system detailed within.
EDIT: Though after entering the code from this thread in a test story, it seems to be breaking quite a bit. I've entered in a few different variants and it's still happening. No idea what I'm doing wrong.
Now it's just figuring out a way to check for and use <<if>> functions with inventory items to make things happen. I believe in the original it was a custom macro called <<isInInv>>.
This basically means I've got an inventory system which shows items that I can pick up and drop, but I can't create any meaningful interactions with said items.
After playing around with this for the last 48 hours, I'm going to shelve this code for now and continue working on the meat of my game, unless anyone's got any suggestions. I'm starting to feel like I need a bit more knowledge of Javascript.
Inventory System Javascript:
You could use a slightly modified version of that code combinded with an <<if>> macro within a passage.
To make the above <<if>> macro a little less complex you could add a findByID helper method to the $inventory array during it's initialisation by changing the initInv / emptyInv macro(s) code.
note: A more experienced Javascript coder may be able to supply a better implementation of the following method.
The follow example uses the above findByID helper method.
Thank you very much! I've added this into my code and it's working very well. This should be enough to keep me moving forward with the inventory system.
Just in case :
The object in the inventory passage will be a link (don't forget to name it like the passage you create for the object). Can be usefull in some context.
The findByID is inside too.
Thx for all the person who create this wonderfull inventory system.
Sorry, I don't understand. わかりません o_o
Trying to figure out a way to combine the two to get a listInv that has items as passage links while retaining the number of items next to it. E.g. Iron sword x2. I'm going to poke around at this for a while, see how many error messages I can take before I move on to the next thing.
Currently kind of cheating by using the current code to reference items to pull things up.
Thank you so much!
* Currently, equipment is 'equipped' by checking if it exists in the inventory, then duplicating the item values to an 'equipped item' variable. This way, I can easily use <<if>> statements with the 'equipped item' variable, and to equip something else, you just overwrite the details with the item you want to equip, or clear them to unequip.
* If someone dropped an equipped item, you'd still have all its properties in the equipped item variable. E.g. You'd still have a sword equipped even though you dropped it.
Cutting out the ability to drop something and limiting discarding to selling them at a store seemed like the best way to deal with this without inventing some new macro or significantly altering the existing ones, something my current skillset doesn't allow for.
I assume I need something like the following:
and then check if the item has an image property when adding it to the list and then append it to the wiki at the end, like this?
Well, this worked for me:
First, you're pointless escaping the double quotes there. Beyond that, assuming there were multiple items—e.g. a screwdriver and a hammer—that would make the markup look like following: Which is probably not what you want.
I'd probably suggest adding a break, at least, after the image:
Additionally. You're pointless using the <<print>> macro in several places.
For example, this: Could, and probably should, be the following:
And this: Where you also have a forward slash inside the <<link>> which is doing nothing absolutely nothing. Could, and probably should, be the following:
I'm trying to make a sense out of the code you out here. So, I decided to write a function that returns the count of items that hold the same "name" property. Now, after some experimentation I understood that "id" property is unique across all items, even the ones that carry the same name, bare different "id" property. Which makes sense, since this property can be used to uniquely identify items inside the inventory.
Now, all the above could be as false as it is. After I tried implementing a counter function, i did not get the results that would back my understanding. Here are two of my experiments that have all failed:
Normally the functions should return 2 but they either return nothing or 1. No, matter how many items with the same name are placed in the inventory. Your functions counts them correctly though.
Could you please explain where I've failed?
which then would yield the number of items have the same name and are inside the player's inventory.
I guess I might need to run with my own implementation then. This one for some reason doesn't operate as I imagined.
Update: Scratch that, I think, I've suceeded with this one:
Results:
Your problem is that you have fundamentally misunderstood how the <Array>.forEach() method works. Its callback is not a predicate, it does not allow early termination, and it has no return value—other than undefined.
Try the following for your countItemById() function, which uses the <Array>.find() method: NOTE: The predicate function assumes that all members of the $playerInventory array will be objects that conform to the "item object" format shown in this thread—i.e. they must, at least, contain the properties: id and count.
I think I need a function that takes the name of the item I wanna transfer, the container from which I'm transferring the item and the container to which I transfer my item.
window.transferItem("item_name", $source_container, $target_container);
Is there a way to move objects from one place to the other? let's say by passing a reference but then removing that same association in the container from which the item was taken from? Do you just make deep copies of the objects (if possible) and remove the ones you don't need in this case?
So far, i'm thinking about doing the following:
look up the item by it's name inside the source array. Inspect if the source inventory and see if it has more than one item inside (look up by name then look at the count property). If so decrement the count property of that item and place a reference of that item inside the destination array (if it doesn't exist already, if so, just look it up by name and increment, it's count value).
Anyways I'll be experimenting with this one. Any ideas are welcome!
The removeFromInv macro shows you how to remove an item from an Array (in that case a variable named $inventory), in your new case you would be passing the array in as the second parameter of transferItem.
The above can also be said about the addToInv macro except it is showing you how to add an item to an Array, and in it's new case you would be passing the array in as the third parameter of transferItem.
So, what I did is simply, I tried to convert the container into a parameter that needs to be passed to the macro that handles the container and the item. The last two functions are working flawlessly as JS gurantees to make it work properly. Now, the other functions simply fail. For example when I call "addToContainer" it tells me that the passed container doesn't have a "findIndexByID" property. Which, tells me that some garbage is being passed instead of the $player.inventory.
I sometimes wish I could debug and observe what is happening here. Anyone any ideas how to manage multiple containers?