Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifies interaction input handling and refactors reload logic to be more performant #169

Merged

Conversation

aronand
Copy link
Contributor

@aronand aronand commented Apr 18, 2024

  • Simplifies PlayerInteractionComponent by merging input checks for interact and interact2 to a single block
  • Removes a RayCast3D.is_colliding() check and gets that information via RayCast3D.get_collider() (null if no collision)
  • Rewrites the reload logic (removes nesting, removes the while loop, makes the for loop faster by removing the use of InvetoryPD.remove_item_from_stack())

Reload time before:
reload_before_self

Reload time after:
reload_after_self

Simplifies PlayerInteractionComponent by merging input checks for interact and interact2 to a single block.
Removes RayCast3D.is_colliding() check and gets that information via RayCast3D.get_collider() (null if no collision)
if event.is_action_pressed("interact2"):
func _input(event: InputEvent) -> void:
if event.is_action_pressed("interact") or event.is_action_pressed("interact2"):
var action: String = "interact" if event.is_action_pressed("interact") else "interact2"
Copy link
Contributor Author

@aronand aronand Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input and InputEvent don't seem to have a way to return the action name, thus a bit hacky way of getting the action name. Needs a different solution if more interaction keys are introduced, or moving the interaction check to Node._process() instead, and/or checking action via:

if Input.is_action_pressed(node.input_map_action):
	node.interact(self)

This change alone seems to partially work, as the UI updates to reflect the new key, but the interaction itself doesn't seem to work with this change alone (EDIT: Probably because it's within a conditional checking for the two interaction keys 😄).

Simplifies attempt_reload() by decreasing the level of nesting and by rewriting the reload logic:
The reload now loops through the item slots, looking for suitable ammo. When ammo is found, the entire
quantity is used instead of one by one.
break
for slot in inventory.inventory_slots:
if slot != null and slot.inventory_item.name == equipped_wieldable_item.ammo_item_name and ammo_needed > 0:
inventory.remove_item_from_stack(slot)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InventoryPD.remove_item_from_stack() consumes one item at a time, making this loop take longer than is necessary. A possible refactor to it would be to allow the user to define the amount to remove as well.

Copy link
Owner

@Phazorknight Phazorknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this does some nice cleanups.

There's an issue though with the changes to the reload: It seems like the reload_amount properties of AmmoItemPD is not considered, thus a battery is just adding 1 to the charge of the flashlight. Or the clip of the Laser Rifle is just adding 1 to the Laser Rifle charge.

Would you be able to fix this?

@aronand
Copy link
Contributor Author

aronand commented Apr 19, 2024

Good catch! I'll have a look during the weekend. Mainly focused on testing the pistol and didn't realize the mechanics are different for different wieldables.

@aronand
Copy link
Contributor Author

aronand commented Apr 20, 2024

Fix is in now. Tested both the flashlight and the laser rifle, which now seem to function as intended.

@aronand aronand changed the title Simplifies PlayerInteractionComponent Simplifies interaction input handling and refactors reload logic to be more performant Apr 20, 2024
Copy link
Owner

@Phazorknight Phazorknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@Phazorknight Phazorknight merged commit 45e67fd into Phazorknight:main Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants