How do I move an object to a specific instance of another object? [SOLVED]

I’m trying to make an inventory for my game, I added the ability to put objects in your inventory, but when I stop dragging my object with my mouse, it obviously just stops where it is. I can make it center on one of the inventory slots, but it always goes to the same slot. How can I make it center on the nearest slot instead?

I used the position action change its location to being centered on an inventory slot.

screenshot (the code is at the bottom of the events, and also there are so little events because I just started on my game):

There are at least 2 methods. Either use the collision between the object being dragged and the slots to pick a slot or the condition pick nearest to pick the nearest slot from the cursor x,y. Either way you’d need to check if the slot was empty first. You could use an object boolean like IsEmpty with the slot and set it’s default value to true

Button released
Cursor on object (or object group)
Dragging object collision with slot
Boolean IsEmpty of slot is true
action: put object around slot and set boolean to false

Or

Button released
Cursor on object (or object group)
Boolean IsEmpty of slot is true
Pick nearest slot from CursorX(), CursorY()
Action: put object around slot and set boolean to false

If you use the nearest method then you could limit it to a certain distance away and/or use a tween to move the dragging object slowly to the slot so it doesn’t jump into a slot.

I used the top method and it works great. There’s only one problem, when the boolean is set to false, you can’t put the object on any of the inventory slots. Do you know how I can make it only set one inventory slot to false?

Can you post your events?

yeah

ok. The Boolean should be an object variable of inventroyslot not a scene variable. You want to pick only the inventroyslot objects where the object variable IsEmpty = true.

You might want to add the same variable check to the cursor is on the slot, so it only changes the animation if the slot is empty. I’m assuming it’s adding a border or something. BTW, it looks like the event is a subevent of the e key is press I don’t think it should be. As it, it only triggers if the e key is pressed.

sry if I’m being annoying, but I changed the boolean to an object variable and it still does the same thing, I think that the reason why the it only works once is because is it sets every single slot to being occupied every time the item goes in one slot. How can this be fixed?

BTW thanks for helping me with the inventory slot animation, now it works exactly how I thought it should.

I enjoy helping. Don’t worry. If the default value for the objects variable inventortyslot is true and you changed the variable from scene to the inventortyslot object in both the condition and the action then it should be working.

I figured out the problem, the object variable is on the object that I’m putting on the slot instead of the slot. Now it works, except my events to set the IsEmpty variable to true when there is not an object on it don’t work. I know that it is because I set it to be true if the object is not colliding with inventory slot, but that means every single inventory slot. This should be the last thing, here is my events:

Collisions and instances can be weird. I believe if you put the cursor/touch in on inventorytems into a not (or inverted) and you change the condition to normal (not inverted) It will do what you’re trying to do. See my screenshot.

Conditions work as filters. They look for matching objects or instances. As long as 1 object or instance is true then it triggers the actions.

I’m going to try to not confuse you but this can be tricky and it took me awhile to learn. Apologies if I’m saying something that you already know.

Say there are 9 slot instances. If the cursor wasn’t on any objects, then an inverted cursor/touch in on inventorytems would pick 9 objects. (by inverted, I mean the way you invert the condition not the not action) If the cursor was on 1 object, then it would return 8 and trigger. Now, since there’s a trigger once it will only trigger once. A trigger once needs to go back to false and then true again before it can trigger again.

Now, the not action which displays as invert the logical result inverts the result and not the condition. I know, it sounds the same. It’s subtle but different.

Now, if there are 9 slots and the cursor is on 1 it would normally pick 1 and return true but the NOT makes it false. If the cursor is not on a slot, it would normally return 0 objects or false but the NOT inverts it and makes it true.

I think I understand, but what actions am I supposed to use?

This works for me.

Add the NOT condition under other and drag the cursor is on condition into it like in the previous picture. Make sure the conditions are in the same sequence and the **cursor is on ** condition isn’t inverted.

Oh, sorry, I have the conditions set up, but I don’t know which actions I should have on the event. I need to set the IsEmpty boolean to true if there is no object on the inventory slot.

The same actions that you had. It was just the condition rhat needed changing.

alright, I’m not sure what I did wrong, but it doesn’t work.
Screenshot:

On your last event, it’s checking if IsEmpty is true and setting it to true. I think the option should be false. if IsEmpty false and all of the other condtons then make true

I tried it, but it doesn’t do anything.

I think you need to add a pick all inventoyitem see below because of the cursor is on inventoryitem condition. I’m not sure if you even need the cursor is on condition. That seems to cause a delay in updating until the cursor isn’t on the object. IDK. You can either remove the cursor line or add the pick all. Your choice.

or

Finally! Everything works the way I intended, thanks for helping

1 Like