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

Fix Simple node setup not happening #91

Merged
merged 1 commit into from
Jun 27, 2024
Merged

Fix Simple node setup not happening #91

merged 1 commit into from
Jun 27, 2024

Conversation

wnbaum
Copy link
Contributor

@wnbaum wnbaum commented Jun 27, 2024

Any Simple node like SimpleCharacter will have its _ready overwritten by the block script if a ready block exists. This PR enables setup for the Simple node to still happen regardless. (e.g. Pong paddle texture initialization)

This was the best way I could find to do this. Calling super() in the child _ready when _ready is not overridden in the parent causes an error (even though it's a virtual method...). I don't know of any way to check if the method is overridden in the parent to call it or not.

Any Simple node like SimpleCharacter will have its _ready overwritten by the block script if a ready block exists. This enables setup for the Simple node to still happen regardless.
Copy link
Contributor

@manuq manuq left a comment

Choose a reason for hiding this comment

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

I think it's a good workaround for now. Great!

@manuq manuq merged commit d60c3bd into main Jun 27, 2024
2 checks passed
@manuq manuq deleted the fix-simple-ready branch June 27, 2024 18:52
@dbnicholson
Copy link
Member

This is an OK workaround, but I feel like we can do better. Would it be possible to extend the generated _ready so that it does:

diff --git a/addons/block_code/ui/picker/categories/category_factory.gd b/addons/block_code/ui/picker/categories/category_factory.gd
index 1ebb8b1..71659df 100644
--- a/addons/block_code/ui/picker/categories/category_factory.gd
+++ b/addons/block_code/ui/picker/categories/category_factory.gd
@@ -159,7 +159,14 @@ static func get_general_blocks() -> Array[Block]:
        b = BLOCKS["entry_block"].instantiate()
        b.block_name = "ready_block"
        b.block_format = "On Ready"
-       b.statement = "func _ready():"
+       b.statement = (
+               """
+               func _ready():
+                       if get_parent().has_method("_ready"):
+                               super()
+               """
+               . dedent()
+       )
        b.category = "Lifecycle"
        block_list.append(b)
 

@wnbaum
Copy link
Contributor Author

wnbaum commented Jun 27, 2024

Yeah, I would have preferred something like that but from my looking I don't think it's possible. For one, you would want to use super.has_method("_ready") instead of parent.has_method("_ready") since we are attaching our generated block script to the BlockCode node's parent. But when I do super.has_method("_ready") it returns true (I'm assuming because it has the method even though it's not implemented). So the issue is there is no way that I could find to determine if the _ready method is overridden in the superclass or not. Hence the workaround.

@dbnicholson
Copy link
Member

Apparently not. If you have super() anywhere and it isn't defined in the parent you get a parse error.

This pull request was closed.
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.

4 participants