|Attention||Topic was automatically imported from the old Question2Answer platform.|
Hi, I am currently working on a Menu, that is divided into sections.
I am trying to code the UI so that it can be fully compatible and navigable with a joystick.
Right now, each section can be accessed by using r1 and l1 to navigate between them.
Everything works fine and the code is still acceptable. I can also open and close the menu with the “start” button.
The problem is that, as I was coding the logic for the inventory, which should allow the user to interact with the items, the code is getting more and more messy.
There is probably no unique way to organize the code to handle different kinds of inputs across stacked popup windows, but I would like to know if any of you could be so nice to give me some guidance or show me an example of how this could be done in a better and cleaner way.
One example of my code so far is this:
This code is attached to the Inventory node itself.
Inside the inventory slot I handle part of the input that is used to navigate across the different possible actions on the items, such as “use item” and “throw/drop item”
func _input(event): match InventoryManager.interaction_stage: InventoryManager.interaction_stages.NONE: handle_normal_input(event) InventoryManager.interaction_stages.USE_ITEM: handle_use_item_input(event) InventoryManager.interaction_stages.THROW_ITEM: handle_throw_item_input() InventoryManager.interaction_stages.CONFIRM_ACTION: handle_confirm_input() func handle_normal_input(event): if Input.is_action_pressed("ui_up"): change_active_slot(-1) elif Input.is_action_pressed("ui_down"): change_active_slot(1) elif event.is_action_pressed("ui_left"): change_active_item_category(-1) elif event.is_action_pressed("ui_right"): change_active_item_category(1) elif event.is_action_pressed("menu_interact"): if !items_list.get_child(active_slot).is_empty(): items_list.get_child(active_slot).show_actions_popup() set_process_input(false) func handle_use_item_input(event): if event.is_action_pressed("menu_interact"): use_item() print("here") elif event.is_action_pressed("menu_go_back"): perform_action_dialog.hide() items_list.get_child(active_slot).can_interact = true InventoryManager.interaction_stage = InventoryManager.interaction_stages.SELECT_ACTION elif Input.is_action_pressed("ui_left"): perform_action_dialog_quantity.value -= 1 elif Input.is_action_pressed("ui_right"): perform_action_dialog_quantity.value += 1 func handle_throw_item_input(): if Input.is_action_pressed("ui_up"): perform_action_dialog_quantity.value -= 1 elif Input.is_action_pressed("ui_down"): perform_action_dialog_quantity.value += 1 func handle_confirm_input(): pass func change_active_interaction_action(direction): pass ...
Thanks in advance to anyone that will try to help me
I rewrote a part of the code and decided to divide each dialog into its own scene, in order to have a different _unhandled_input method for each of them.
Now the code is not all compressed into a single script file, and I can now handle different inputs, without the risk of them overlapping and thus firing two or more at once, resulting in an unpredictable input action flow.
The main drawback of this method is that I have to disable the process input of the dialogs every time a new dialog pops up on top of another to allow each dialog to handle its own input.
So the flow is as follows:
Inventory Input → select and click on an item → diable inventory input → enable item actions popup input → choose an action → open a new action dialog and enable its input → disable previous popup input → perform action → close dialog, disable its input and re-enable the inventory input.
Codewise it is not as messy as before but has a lot of in-between steps that need to be handled to achieve the desired result.
I will probably iterate again through this solution, but the overall concept will most likely remain the same
paoloforti96 | 2020-11-25 08:59