Happy Friday! I am trying to write this blog while also trying to not melt into a puddle – it's proving challenging, but I shall persevere!
This week I've been messing around with the mini crafting project that Kyatric sent me. I'm happy to say it now functions quite nicely as a little drag-drop crafting system. The project uses a JSON to store the item data and crafting recipes and an array to store the information about which items you're trying to craft with.
When you drop an item on top of another item, the item's IDs are saved to the array and a function is called that begins the check as to whether those two items can make something.
The function runs a couple of validity checks to see if the IDs of the two picked items can create something according to a recipe. If a validity check is passed, the corresponding global boolean will be set to true.
If the two item IDs in the array match the two ideas stored in a recipe, then the new item is generated and the two used to make it are destroyed. Because all of the crafting items are just instances of the same object, we need to add in an extra check to make sure that only the objects used for crafting are destroyed. A Boolean variable on the items object solves this.
Two global variables store the value of the item ID for the item that is picked up and the one it's dropped onto. When the picked items have been confirmed to be part of a recipe, the global variables are use to set the correct Boolean variables to true. Then when destroying the objects, only those which have the Boolean set to true are actually destroyed. So if you have multiple copies of one type of item, only the one you've used will disappear!
This may become a tutorial, but the file needs a fair bit of tidying/commenting before it's a tutorial-level example. Depends how useful people think it is!
Enjoy your weekend!