Drag and Drop 'Crafting' System

16

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

alchemystylecrafting.c3p

Download now 187.8 KB

Stats

5,557 visits, 10,395 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 5 Nov, 2020.

Startup

On the start of the project, the first thing that fires is an AJAX request to call the JSON project file. This is then parsed into the JSON object and its contents written into the two text objects so they're visible at runtime. As mentioned earlier, this isn't essential for the game to function, but acts as more of a debug – it helps having the recipes visible at least so you can see if they're working!

How Crafting/Combining Works

In order to craft something, the player must drag one object onto another. If the IDs of the object match the IDs in a recipe, then the new object is created.

When the player begins to drag an item, its UID is stored in the global variable DragItemUID. When the item is dropped, the game sets the size of the array, aCollidingItems, in preparation for a recipe check. If when the item is dropped, it's overlapping another item instance, the second item's UID is stored in OverlapItemUID and both the IDs (from the instance variable stored on the item) are added to the array.

Once the array has been populated, the recipe check is done by calling a function. The function handles all of the checks, item creation and destruction for the combination process.

This is quite a large function, so forgive me if the explanation gets a little complicated. It must loop through both the JSON file and the array to compare the dragged/overlapped items with the recipe list.

Starting with a For Each loop in the JSON file, we start the recipe check by resetting the ID validity check Booleans, to make sure the check starts from a clean slate.

The function then loops through the aCollidingItems array. If either of the IDs stored in the array match one of the IDs for ingredients in a recipe, then the corresponding ID validity check Boolean is set to true. So for example, if the array value matches the first ID in the recipe, then ID1Valid is set to true.

If both validity check Booleans are true, then a recipe has been matched and the combination process can begin. This starts by storing the corresponding recipe result into the ResultID global variable. To help with making sure only the objects used in the combination are destroyed, we then pick the two objects which have UIDs matching DragItemUID and OverlapItenUID and set their instance variable Used to true.

The function then loops through the items array within the JSON file to retrieve the appropriate information to spawn the correct result from the recipe. A new Item instance is spawned and its ID instance variable, animation and animation frame are set accordingly.

The final thing to do is to destroy the two ingredients used in the combination process. To do this, we pick all of the Item instances that have their Used and if their IDs match ID1 or ID2 then those instances are destroyed.

Item Spawning

This project also includes a way to spawn new items into the game layout, which again is mainly done for debugging purposes in this example. But essentially, there are ten items in the JSON file, and handily, ten number keys on a keyboard. So, when one of the keys is pressed, the item with the ID matching the key is spawned. Its ID instance variable is set after spawning, as is the animation frame to ensure the correct item is created in the layout.

This is a very basic implementation of item spawning (I added it more for testing, but figured I'd leave it in), but relatively easy to adapt – you could try having an invisible layer that contains an instance of each item that when clicked, spawns that item in the game screen, for example.

  • 3 Comments

  • Order by
Want to leave a comment? Login or Register an account!