Data Structure for Saving Crafting Recipes

E

Easien Games

Guest
I am currently working on a platformer where the player can also pick up items and craft tools.

The inventory system is already set up and working properly. The player should be able to craft tools by dragging two items into a 1x2 crafting grid.

Now I'm wondering, how can I save the crafting recipes so the game knows which combinations should lead to which results.
My only idea was creating a ds list like this:
Code:
ds_crafting = ds_grid_create(4, 1);
ds_crafting[#0, 0] = ITEM.LOG; ds_crafting[#1, 0] = ITEM.LOG; ds_crafting[#2, 0] = ITEM.WOODEN_PLANKS; ds_crafting[#3, 0] = 4;
The first two entries would contain the crafting materials, the third one the product and the fourth one the amount.
However for this method to work there must be an way to check if "item 1" and "item 2" are part of the same row of the ds list plus get the other two entries of that specific row.

If there is a way to make this work, please let me know! If not, maybe you could suggest another data structure I could use!

Thanks!!
 

Reign

Member
Code:
//Store recipes like this
ds_grid_set(ds_crafting, ITEM.Log,ITEM.Log, ITEM.Plank);

//Get the crafted item like this
resultingItem = ds_grid_get(ds_crafting, ITEM.Log,ITEM.Log);
 
Last edited:
E

Easien Games

Guest
Thanks for the help!
I just thought way too compicated!

With a script and a few if statements for all the recipes it's pretty easy to do!
 

Drepple

Member
There's nothing wrong with if statements, but a ds_grid would be much easier to use and faster in the long run. When you create a ds_list you define a width and a height. (since it's a grid) Then you can access every value just like with any other variable. Say you want to make a grid named "recipes", you would create it using:
Code:
recipes = ds_grid_create(4,10);
This would create a grid that is 4 wide and 10 high (in case you have 10 recipes)

Then lets say you want to set the first item of the 3rd recipe to a stick. You would have to change the value on the first column and the third row, like this:
Code:
recipes[# 0,2] = ITEM.STICK
(Since the first value of a grid is 0 and not 1, it will be '0,2' instead of '1,3')

Then say, you wanted to access that value to check if the first crafting slot is equal to it, do the same thing:
Code:
if (crafting_slot_1 == recipes[# 0,2]) {

}
For example, if two logs create four wooden planks, you could do this to set the recipe:
Code:
recipes[# 0,2] = ITEM.LOG
recipes[# 1,2] = ITEM.LOG
recipes[# 2,2] = ITEM.WOODEN_PLANKS
recipes[# 3,2] = 4
and then access them the same way again later, for which you should create a script. Just have the two items in the crafting slots be the arguments and in the script use something like for-loops to go through the grid and find which item you will get.
Code:
var item1 = argument[0];
var item2 = argument[1];
for (i = 0; i < ds_grid_height(recipes); i ++) {
    if (recipes[# 1, i] == item1 && recipes[# 2, i] == item1) {
        //display the item you will get
        break; //break after finding the correct recipe so the game stops searching
    }
}
Lastly, learn more about ds_grids and how to use them here. I hope I could help out, good luck with your project!
 
  • Like
Reactions: Yal
E

Easien Games

Guest
Thanks alot for this detailed answer!
ds_grid definitely come handy when it comes to creating things like that!
I managed to set up a simple crafting system and it works quite fine!
 
Top