How do I combine items using an array?

0 favourites
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • Hi,

    I'm trying to implement a feature where the player selects two (or possibly more) items from an inventory and combines them to make a new item, depending on which items were combined. I want to use arrays (unless there's an easier method), but can't work out what events I need to do this.

  • A lot depends on your existing inventory system (do you have one yet?) and the method you want the player to interface with crafting. Are they picking recipes from a list? Do they drag and drop items on top of each other to automatically combine? Are they throwing things in a pot and seeing what comes out? ect.

  • Thanks, oosyrag. The game's still very early in development but I was thinking along the lines of dragging and dropping items to combine them.

  • If you're looking for something drag/drop, I had a crack at it a couple of years ago and wrote a tutorial for it. It's pretty basic, but you might be able to build upon it, or at least take some ideas from it.

    DRAG AND DROP 'CRAFTING' SYSTEM

  • Many thanks Laura_D, this looks really helpful. :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Could anyone give a brief explanation as to how modify this to make a line of text appear with "You made a [insert item name here]" when the two items are combined?

    I've tried adding a text object and an "itemDescription" line in the json file, then using "set text to JSON.Get(".itemDescription") but it always displays "0" instead of the text. I'm still very new to JSON so have probably missed out a lot of steps.

  • Assuming your json is already parsed and loaded into memory (are you able to get other values from it?), maybe you have a path or syntax problem. Can you post the json file?

  • Hi oosyrag,

    I reworked the file earlier today and the text is displaying now. I'm still not sure what I changed. I wanted to ask if there's a way to add a third item into the combining "recipes", for example, combine three items instead of two? I've tried adding an extra global variable and matching events but it prevents all items being combined. The code's currently nearly identical to Laura_D's original: https://drive.google.com/file/d/1ybqEwpb8Bc9oyXQI6gTE43jzFVp43tXw/view?usp=sharing

  • Did you have a version with the third item set up? It sounds like it should work. I could take a look and check what you did if you don't mind uploading that version.

    The tricky part might be in event 7. Add a for each (family) items, to add all 3 (or more) items to the colliding items list.

    However (IMO), drag and drop isn't the greatest system for 3+ item combinations. With drag and drop, mechanically a user is interacting with 2 things, the dragged item and the target.

    I think crafting slots would be more suitable if you want bigger combinations, or click /select to add to a list, then combine/cancel. It gets more complicated when having more than 2 items.

    Edit: An alternative option is to have a the third item combine with the result of the first two, which could keep things much simpler, but may or may not work depending on your design goals.

  • Did you have an idea of how you wanted the interface to be or how you would want the process to look? If you could describe what you want I wouldn't mind trying to put together a system for you, I haven't done a crafting system myself yet.

  • Hi oosyrag,

    Many thanks for all your help. The project's still too early in development for me to have a really clear idea about the interface, but once I've given it more thought I'll post a sample image and description when I have time.

    If I decide to stick with drag and drop I doubt I'll use more than three items as I could see it was starting to get overly complex.

    In the meantime, here's my attempt at adding a third item. I've added the "For each item family" but still can't get them to combine.

    According to debug mode the "ID2Valid" and "ID3Valid" variables don't always switch to "True" when overlapping the other items.

    drive.google.com/file/d/12kvwVJnoTaVl5K5gLJEllirUFHkhvmPy/view

  • It works, just your orange has an ID set to "3" in the layout editor. If you follow the JSON, it should be "1", and the yellow one should be "2" (the yellow one is 1 right now).

    In event 6, the array size should be set to (0,1,1) so that it resets, or empties the array. Then the new values get pushed in. If it is (2,1,1) the way you have now, the first two entries will be 0 and then you'll add entries after that (basically you'll always have a red thing mixed into your stack).

    Right now only two of the items out of the three used instances get destroyed, because only two instances get set as "Used" in event 16 and 17. To fix this is a bit trickier. You'll want to set the "Used" instance variable in the family "Items" instead of the sprite object "Item".

    First delete the "Used" instance variable from "Item". Then add a new "Used" instance variable in the "Items" family, set to boolean. You can delete events 16 and 17 completely. In event 7, add the actions for "Items: Set Used to True" and "Item: Set Used to True", you'll need both. Then in event 8, after the function is called, set Items and Item Used to False. Finally in event 18 (formerly event 20, if you didn't delete 16 and 17 yet), add the compare boolean condition "Item is Used".

  • Hi oosyrag,

    Many thanks for taking the time to explain this, it's been a big help. Just one question - in event 7, why do I need to set the Used variable to True for both the Item family and the Item object? I accidentally deleted one of the "Set Used to True" actions afterwards and found it still worked.

  • I thought the instance being dragged (item) wouldn't get set to used without the additional action, as opposed to the overlapping instances (items). But if it works without it that's fine too!

  • I made an attempt to refactor and simplify the example, as well as allowing it to be more flexible in terms of number of ingredients, and ended up redoing it in a way that made more sense to me. Maybe this might be of use to you.

    dropbox.com/s/r0tnsmpimtrr7ik/dragdropcraftingexample.c3p

    Instead of using global variables, there is now a second array to keep track of matching ingredients, which allows for recipes of any size. This array is also used to delete the used ingredients by UID, so the instance variable that kept track of "used" is no longer necessary either.

    The overlap array now keeps track of both the item id and the UID of every overlapping item, so that each individual instance is kept track of.

    The JSON format for the recipes changed a little to make it more flexible as well. The ingredients list is now stored in an array inside an "ingredients" object, which can include as many ingredients as you want, without having to define id1, id2, ect.

    So it looks like this now.

    "recipes": [
    	{	
    		"ingredients": [0,1,2],
    		"result": 3
    	}
    ]
    

    Edit: Updated the example so it shows how to load json from multiple files, since I would personally keep them separate in an "items.json" file and a "recipes.json" file.

    Note that recipe matches are currently prioritized by first valid match. If I were to improve on it I would probably design the crafting system so that the order of the recipes in recipes.json doesn't matter. Basically account for conflicts to prevent situations where there may be two valid recipes ahead of time (such as defining what happens if you have a and c overlapping already, and drop a b onto it when you can have the recipes a+b=c, and a+b+c=d).

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)