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

🐛 [bug] Ability Container fails to initialize if preloaded with multiple Abilities #23

Closed
danielbernard opened this issue May 29, 2023 · 3 comments · Fixed by #28
Closed

Comments

@danielbernard
Copy link
Contributor

danielbernard commented May 29, 2023

Description

AbilityContainer removes entries during initialization while granting, causing it to lose abilities.

To Reproduce

Create an AbilityContainer, add multiple Ability entries in the editor, run the scene and observe what abilities remain in the abilities Array.

Details

I added a few unique abilities to an Ability Container manually in the editor, and upon running, I was receiving index errors.
Out of bounds get index '2' (on base: 'Array[Ability]')
I observed two possible issues in the code:

First, this section of the _ready function

for i in abilities.size():
var ability = abilities[i - 1]
grant(ability)

the for i in abilities.size() will return [0 ... n] rather than [1 ... n], so there's no need to subtract 1. This leads to accessing the last element of the array first.
Removing the -1 or replacing this with the following should let it process in order.:

 for ability in abilities:
 	grant(ability) 

Note that replacing the index iteration with objects allows the code to execute without errors, but the ability array is still missing entries even if the array changes shape (as occurs in the grant function).

Digging into the indexing error, I found that you get odd behavior when the grant(ability) call is performed; since it will remove the first instance of an ability in the AbilityContainer, it removes the entry currently being granted. This causes the original array shape to change, and the loop in the _ready function to get confused and skip entries.

# Removes from abilities array if it's there. This avoids duplication which could lead to bugs.
var ability_index = abilities.find(ability)
if ability_index >= 0:
abilities.remove_at(ability_index)

@danielbernard danielbernard changed the title Ability Container fails to initialize if preloaded with multiple Abilities 🐛 [bug] Ability Container fails to initialize if preloaded with multiple Abilities May 29, 2023
@danielbernard
Copy link
Contributor Author

Oh; I just saw the commentary in #10.
The current design doesn't iterate in reverse, it hits the last element, then iterates back from 0.

To traverse in reverse, we could:

	for i in abilities.size():
		var ability = abilities[-i - 1]
		grant(ability) 

but iterating in reverse still gets mixed up. After the first grant/removal at -1, the end of the array is shortened, so -2 accesses the second to last element of the shortened array.

Switching to always access the last element works, if we're to assume that each entry will always be removed.

	for i in abilities.size():
		var ability = abilities[-1]
		grant(ability) 

I'm not sure if the removal (movement to granted_abilities, rather than simply adding the entry to the new array and only removing from abilities if there's a duplicate) is the expected behavior; I may be missing the way this is supposed to work during initialization.

@OctoD
Copy link
Owner

OctoD commented May 30, 2023

I agree with you, that system should be retouched a little bit, it's too clumsy to me now.

The main logic behind the use of these two arrays is that you could add abilities without granting them (a skill tree is a good example) and grant them after when some criteria are matched.

@danielbernard
Copy link
Contributor Author

Makes sense! Good enough for now, and should be usable.
I'll get a little more familiar with using it and see if I think of anything.

OctoD added a commit that referenced this issue Jun 12, 2023
@OctoD OctoD mentioned this issue Jun 12, 2023
@OctoD OctoD closed this as completed in #28 Jun 12, 2023
OctoD added a commit that referenced this issue Jun 12, 2023
migalvalm added a commit to migalvalm/godot-gameplay-systems that referenced this issue Oct 17, 2023
* fix inventory in diablo_like example (OctoD#21)

Co-authored-by: sergwest <ksendzov.sd@gmail.com>

* chore: updates gitattributes

* chore: adds import file

* feat: adds hooks methods in Interaction (OctoD#22)

feat: adds on_before_interaction_end
feat: adds on_before_interaction_start
chore: updates sot-like example with draggable object

* chore: adds a line at the end so godot stop complaining

* Fix icon paths for attribute/ability resources/nodes (OctoD#24)

* fix icon paths missing "attributes_and_abilities"
* add available icons to ability nodes/resources

* fix abilitycontainer _ready missing preloaded abilities (OctoD#25)

* fix: fixes OctoD#26

* fix: adds condition

* chore(demo): adds example on how to use tags_to_display

* fix: closes OctoD#23 (OctoD#28)

* feat: adds _on_interaction_started and _on_interaction_ended calls (OctoD#29)

this occurs when an interactable_area is interacted and if the methods are implemented

(look at sot-like example)

* Feat/2d-and-3d-point-and-click-controllers (OctoD#30)

* feat: adds point_and_click_2d

* feat: adds PointAndClick3D node

* fix: fixes typo in tags_updated signal parameter name

* chore(docs): adds docs

* feat: adds radial menu (OctoD#31)

* chore: removes commented duped line

* feat: adds RadialMenuContainer icon

* chore: updates readme

* feat: adds initial camera shake implementation (OctoD#33)

* fix: closes OctoD#34. Thank you @Hairic95

* feat: adds SlideShow node for your game intros (OctoD#35)

* chore: migrates from 4.0 to 4.1

* fix: fixes test

* fix: fixes esc menu button

* chore: updates IDEAS.md

* feat: adds get_attributes_dict method

* Feat/turn-based-nodes (OctoD#36)

* feat: adds turn based nodes

* Update README.md (OctoD#42)

* fix: closes OctoD#39 (OctoD#41)

* chore: updates docs

* chore: adds requirement for reproj

---------

Co-authored-by: Sergwest <39670193+AFK1@users.noreply.github.com>
Co-authored-by: sergwest <ksendzov.sd@gmail.com>
Co-authored-by: octod <iamoctod@gmail.com>
Co-authored-by: OctoD <OctoD@users.noreply.github.com>
Co-authored-by: Daniel Bernard <daniel.john.bernard@gmail.com>
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 a pull request may close this issue.

2 participants