Not sure how to add items to inventory from tutorial

You could do this if you like, but honestly I also don’t want you to get discouraged! The logic is a little noodley, but honestly everyone encounters noodley code eventually…

You’ve already got logic in here for finding the top left while hovering; that already works. And you’ve already got logic in here for finding which slots your automatically placed item will occupy. You just need to steal a little bit of “inspiration” from the top-left logic and put it into your place_item_pickup area. You’re already iterating through the item grids there, and there’s no reason that you have to call pickup_item._snap_to at the top of this method. You could call it at the end, say after the for loop, right before item_held = null.

Then you wouldn’t need to use icon_anchor at all, because all the data you need is already right here in this one method, which would be much cleaner.

Even if you do decide to simplify this, I’d say you don’t need to throw this all out and follow a different tutorial. You’ve got the bones of what you’re looking for basically done here! You could just simplify this, once you’ve figured out what all the bits are for.

But, of course, I also won’t stop you. If you’re not feeling like you’re learning anything or making progress with this approach, or if you feel like it’s just not for you, you can of course follow a different tutorial to see what their approach looks like. Whatever helps you move fowards, either in your game, or in your skillz :stuck_out_tongue:

Thanks for the encouragement! I’ll have another go at it tomorrow, and get back to you with the results!

No problem! One other thing I’d encourage is getting a bit playful with the code. I know it can be a bit daunting at first when things seem more mysterious, but remember that no one knows what you’re doing minute-to-minute, it’s just you. So comment some crap out! Huh, okay, this line is busted, but I’m not really sure what it’s for… what happens if I just don’t run it? Oh, okay, now it kinda works, but it’s not in the right place. It looks like it’s taking some kind of global position. What if I just give it the global position of the first slot? Oh okay, now all the items are all snapping to the top left. Oh, okay… What if I give it the position of a_Slot? Oh I see… okay…

Something that separates watching a tutorial (or even taking my help), versus writing the code yourself, is the process. Writing code for yourself is like sculpting. You start out with a shapeless blob that does nothing, and little by little you add the bits that give it form. You shouldn’t expect to be able to just start at the top of the file and dump out perfect code top-to-bottom. It’s about being 25% of the way there, and then adding a little bit more and now being at 30%. And then just a bit of this, to get us to 40%, and eventually we get to 80%, call it good enough and ship it! :wink:

Watching this tutorial, it had a little bit of that energy, but it very much felt to me like he already had done this in practising and knew where he was going before he started. He made some variables long before he needed them, etc. You won’t be like that, though, when you’re figuring stuff out for the first time, and that’s fine! Push some clay around!

The debugger can be very helpful in being like “what the heck is this even doing…”, and comments are also great for getting some logic out of your way, without actually getting rid of it. (If you didn’t know, Godot’s editor allows you to highlight several lines and hit Ctrl-K to comment them all out, and also to uncomment them). There are some times where I comment out the real logic and add a line that just makes some manual data. I know this line will never make it to production, because it’s hard-coding something that’s meant to be dynamic. But that doesn’t matter, because it’s really easy to delete lines later. What I’m doing now is experimenting. So I’m feeding stuff in, to see how the code responds. And once I’ve figured out how this thing works, then I’ll figure out how to get the real data. Or sometimes it’s about isolating where the problem is. If I give this piece known good data, does it work? If so the problem is in my logic that’s getting this data. And if it still doesn’t work, then the problem is the way this data is being used. That just cut my bug search-space in half!

I guess what I’m saying is that the process is about making a game, but that doesn’t mean that every single line written needs to ship in the finished product. Sometimes what’s really valuable is trying stuff out until you figure out what you don’t know, then you can focus on using that new knowledge and intuition to make the game.

Good luck!

I took your suggestion moved the line “pickup_item._snap_to(grid_array[calculated_grid_id].global_position)” to the end of the “place_item_pickup” function, and it still makes the first item spawn snap to the wrong place. I did some testing though and learned that the information is being set correctly, it’s just the item object is snapping to the wrong place. Also, I’m still getting the "Invalid get index ‘180004’ ('on base: ‘Array’). error when I try to “pickup” an item if there’s already a normally placed item anywhere in the grid.

Sorry, yeah, I was being perhaps slightly coy. :sweat_smile:

Since we’re not setting icon_anchor anywhere in between those lines, it’s still going to be set to the really high value. What I was implying, was that from below the for loop you’ll have iterated over all of the item_grids, and you could build up an icon_anchor equivalent in that loop and then not have to use icon_anchor anymore.

I thought I already was doing that in the function, here’s the code

func place_item_pickup(a_Slot):
	var calculated_grid_id = a_Slot.slot_ID + icon_anchor.x * col_count + icon_anchor.y
	grid_container.add_child(pickup_item)
grid_array[calculated_grid_id].global_position
	print(calculated_grid_id)
	pickup_item.grid_anchor = a_Slot
	for grid in pickup_item.item_grids:
		var grid_to_check = a_Slot.slot_ID + grid[0] + grid[1] * col_count
		grid_array[grid_to_check].state = grid_array[grid_to_check].States.TAKEN 
		grid_array[grid_to_check].item_stored = item_held
		if grid[1] < icon_anchor.x: icon_anchor.x = grid[1]
		if grid[0] < icon_anchor.y: icon_anchor.y = grid[0]
	
	pickup_item._snap_to(grid_array[calculated_grid_id].global_position)
	item_held = null
	clear_grid()

aren’t the if grid[1] and if grid[0] statements in the for loop setting icon_anchor?

Oh yeah! I forgot you added that :stuck_out_tongue:

In that case you’re, like, 98% of the way there! At this point it’s just a bug, rather than a missing feature.

You’ve moved the snap_to to the bottom… but you’re calculating calculated_grid_id at the top still :wink:



It works perfectly now, thanks so much for all your help! There’s still a few things to touch up, like making a case where the inventory is full, and going through the check_slot_availability_pickup function with the item at each level of rotation, but those are things I think I’ll be able to figure out. Then I’ll be cleaning it up for when I import it into my actual project. Thanks again!

Glad you got there :tada:

Good luck with the rest of your project!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.