IssueRaiser

Make the $ operator in GDScript cache node references (or add a $$ operator)

EDIT: ~~If #1048 is implemented, it may make this proposal obsolete.~~ EDIT 2: As @h0lley noted, #1048 doesn't actually make this proposal obsolete. It provides a clean alternative but it still results in boilerplate and a lack of green syntax highlighting unlike this proposal. **Describe the project you are working on:** https://github.com/godotengine/godot-demo-projects/ but this applies to any project made in GDScript. **Describe the problem or limitation you are having in your project:** Here is a code snippet from Dodge the Creeps: ![](https://user-images.githubusercontent.com/1646875/76044779-4e4a2000-5f29-11ea-96c3-161619caa301.png) The problem is that this code looks elegant, but it isn't very performant. Every time this method is called, it has to call `get_node()` 6 times, and 2 of those times are getting the same node (HUD). We can reduce this from 6 to 5 by caching the HUD reference like this: ![76044779-4e4a2000-5f29-11ea-96c3-161619caa301](https://user-images.githubusercontent.com/1646875/83560643-8ccd8780-a4e4-11ea-8af3-8bd40635e8bc.png) However, this method is still very inefficient, since it calls `get_node()` 5 times every time the method is ran. The best simple option that I can see is to use `onready var` like this: ![76044779-4e4a2000-5f29-11ea-96c3-161619caa301](https://user-images.githubusercontent.com/1646875/83561043-2b59e880-a4e5-11ea-8b89-fe884dec06af.png) This means that `get_node()` is only called 5 times once when this node is created,

Posted by aaronfranke on Jun 2, 2020, 7:35 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments

github (Software)

Make the $ operator in GDScript cache node references (or add a $$ operator)

aaronfrankeJun 2, 2020, 7:35 PM

EDIT: ~~If #1048 is implemented, it may make this proposal obsolete.~~ EDIT 2: As @h0lley noted, #1048 doesn't actually make this proposal obsolete. It provides a clean alternative but it still results in boilerplate and a lack of green syntax highlighting unlike this proposal. **Describe the project you are working on:** https://github.com/godotengine/godot-demo-projects/ but this applies to any project made in GDScript. **Describe the problem or limitation you are having in your project:** Here is a code snippet from Dodge the Creeps: ![](https://user-images.githubusercontent.com/1646875/76044779-4e4a2000-5f29-11ea-96c3-161619caa301.png) The problem is that this code looks elegant, but it isn't very performant. Every time this method is called, it has to call `get_node()` 6 times, and 2 of those times are getting the same node (HUD). We can reduce this from 6 to 5 by caching the HUD reference like this: ![76044779-4e4a2000-5f29-11ea-96c3-161619caa301](https://user-images.githubusercontent.com/1646875/83560643-8ccd8780-a4e4-11ea-8af3-8bd40635e8bc.png) However, this method is still very inefficient, since it calls `get_node()` 5 times every time the method is ran. The best simple option that I can see is to use `onready var` like this: ![76044779-4e4a2000-5f29-11ea-96c3-161619caa301](https://user-images.githubusercontent.com/1646875/83561043-2b59e880-a4e5-11ea-8b89-fe884dec06af.png) This means that `get_node()` is only called 5 times once when this node is created,

Comments (0)

No comments yet. Start the discussion.

Comment on this issue