How do I make crafting that piggybacks my inventory system?

Get help using Construct 2

Post » Fri Jan 08, 2016 6:02 am

So, I created a text-based inventory based on https://www.scirra.com/tutorials/583/easy-inventory-using-an-array this tutorial. That went down smoothly. After mulling over the tutorial, I had a couple of array breakthroughs. My interest was very peaked. So I thought I might try a crafting system. Holy crap, I have had no luck. I've tried making one from scratch a couple times, but I'm stuck, and have made no decent progress. This https://www.dropbox.com/s/2yavzzsx7u8hoif/CraftTest.capx?dl=0capx is the best I can come up with. From what I can tell the code should work? I'm just hoping one of you can help. Thank you very much in advance. Have an awesome evening. :)

Edit: :shock: Jeez I totally forgot to tell you what I'm trying to accomplish. Somtimes I'm just braindead. Anyway... I want the player to collect wood and steel. once they have 5 wood and 5 steel, they can craft a sword. When the sword is crafted, I want it to remove the wood and steel from the inventory. My code has failed to remove the wood and steel. Again, thank you :lol:
B
8
S
2
Posts: 39
Reputation: 604

Post » Fri Jan 08, 2016 4:08 pm

Do all of your objects have ids? You need to correlate an ephemeral id number (e.g. "steel" has an id of 1 -- or whatever)

You give us no idea of how your inventory works behind the scenes, so I am going to tell you how I would do this.

First, I would have ephemeral ids for all of my in-game objects (steel, wood, etc) and correlate those to the Sprite UID of the corresponding sprite in the inventory. That way you can match up the UIDs of the actual steel sprite to the steel entry in the id slot of the array. In other words, steel (being id #1) is stored in the 1 slot of the array (the 2nd slot in the actual data array, since it starts indexing at 0). So in the 1 slot in the array you store a number of how much of that particular item that you have.

Like:

Inventory Array: [ 0 , 12, 0, 0, 0, 9, 0, 0, 0]
ItemID to UID Array: [123, 509, 501, 629, 2011, 4094, 23, 95, 59]
ItemID to ItemName Array: ["Gems", "Steel", "Cloth", "Hides", "Wood", etc, etc, etc, etc]

Then when you craft, you simply subtract from the value in each of the corresponding array slots, for example: You want to craft a spear (or w/e)

It takes 5 steel (id 1) and 5 wood (id 5), in your inventory array at index 1 and index 5 you have values: 12 and 9, so you subtract 5 from 12 in the #1 index and 5 from 9 in the #5 index. Then if you have a stackable inventory, you simply repopulate the inventory by looping through your array and skipping any item that has a value 0 and redisplay the inventory (probably using a function).

All of this stuff is just how you track id numbers and correlate them to sprite UIDs.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Fri Jan 08, 2016 4:56 pm

Okay, thank you very much for the explenation. Though the tutorial I linked should explain what I did well. Sorry I didn't explain well enough. Basically, for the crafting, every object that is going to be crafted has four instance variables: The name of the first resource it requires, the amount of the first recource it requires, the name of the second recource, and the amount of the second recource it requires. So I created a function called "Craft" and have it pass the four instance variables as parameters. So I check if the player has equal or greater wood than the crafted obects wood requirement, and do the same thing for steel.Then if the player has enough of both it crafts it. Althoug, it doesn't actually craft anything right now, it just deletes the placeholder object mini_Sword. Please tell me if this explenation was satisfactory. Thank you again.
B
8
S
2
Posts: 39
Reputation: 604

Post » Fri Jan 08, 2016 11:38 pm

Yea, I looked through the tutorial. Let me actually look at your capx.

First observation, it doesn't look like you actually filled your array with any data. So you are working with a blank array, like this:

[]

or actually this:

{"c2array":true,"size":[10,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]]]}:
indices: -------------------------------------------0-----1-----2-----3-----4----5-----6-----7-----8-----9

This is what a new Array in Construct2 defaults to. You will see this if you output the array as JSON, like Array.AsJSON. You can always use Array.AsJSON to output the contents of any of your arrays to a TextBox in order to help you debug data-driven logical errors.

So, when you say:
Image

You are looping through the above array, which just contains a bunch of zeroes for all 10 indices of the array (the indices being 0 to 9).

If you want actual data in there, you need to loop through however many resources you have an actually initialize the data, otherwise you will get continued logical errors.

You need something like this:
Image
in order to initialize the numbers of each resource in your inventory (assuming you are using the array index as a corollary for the resource id).
Note: I used random(100), but I assume you know already how many of each resource your player has.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests