Kehom / GodotAddonPack

A collection of pure GDScript addons for Godot
http://kehomsforge.com
MIT License
183 stars 15 forks source link

[Inventory] A signal when a Item got switched with another Item. #41

Closed KometFox closed 2 years ago

KometFox commented 2 years ago

It seems your Inventory addon is missing facilities to check if a item got swapped by another item, I need this form of check since with my current implementation of the shop menu that disables certain bags so that the player cannot get free items gets bypassed when the player is dragging a item and swapping it with another item. Trying to use the signal "item_clicked" on my end only registers right clicks and not left clicks so I cannot really use that one for saving item datacode and making sure the bags stays disabled on the shop menu.

Kehom commented 2 years ago

I will work on it. Just to make sure the change will help in your use case, I must ask:

1 - Simply adding a signal to indicate item swap is enough to solve your use case? 2 - A way to block item swapping in a specific bag would solve the problem? 3 - Are you using the automatic internal drag & drop system or manually picking them based on the signals given by the bags?

KometFox commented 2 years ago

1 - Simply adding a signal to indicate item swap is enough to solve your use case?

Yes, having this Signal would allow me to trigger functions which either to save the datacode and/or adding a check where the swapped items comes from so that certain bags may or may not stay disabled till the drag&drop operation is done. Right now when the player for example picks a item from the "to buy" bag the 2 other bags gets disabled and when he/she swaps the items the disabled bags becomes enabled.

2 - A way to block item swapping in a specific bag would solve the problem?

That would cause for the player more annoyance then it would be of any help. Players would probably file a bug report why he/she cant swap the item on certain bags. The first solution could solve this case.

3 - Are you using the automatic internal drag & drop system or manually picking them based on the signals given by the bags?

I'm using the automatic internal drag & drop system. Thank you for your time.

Kehom commented 2 years ago

Ok. Whenever an item is dropped, regardless if it's placed on an empty slot, added to a pre-existing stack or swapped with an item, one item_dropped signal will be emitted. I have just pushed new code that allows one to check if an item is being dragged (as well as a function to retrieve its data if there is anything being dragged at all).

So, the idea here is that, at the "item_dropped" event handler, you could check if there is an item being dragged. If it returns true, then you can safely assume items were swapped, otherwise dropped at an empty slot.

To check, you can do something like this:

func _on_item_dropped_handler(evt: InventoryEventContainer) -> void:
   .... some code
   if (evt.container.is_dragging_item()):
      ... items were swapped
      print(evt.container.get_dragged_item_data())

Let me know if this is enough to solve your use case. If not, then I will see about adding a new type of signal specifically to tell about item swapping.

KometFox commented 2 years ago

Let me know if this is enough to solve your use case. If not, then I will see about adding a new type of signal specifically to tell about item swapping.

Thank you, this changed behavior solved my use case for the shop menu script, now the bags stay disabled when the player swaps the items from the same bag. I'll still have to investigate my weapon script part though so that it triggers the "SaveWeaponDatacode" function too when the item gets swapped.

Kehom commented 2 years ago

Excellent. Good to know the change was simple to implement.

Just to make it clear, the behavior itself was not changed. The item_dropped signal always worked like that. However, I failed to provide ("public") means to check if an item was being dragged or not, which resulted in a very difficult way to check if there was an item swap or not.