Is there a way to keep a function stored somewhere other than in another script?

Godot Version



Hello! I’m reluctant to save each function in separate scripts because, as I understand, in order for your function in this separated script to work they have to have onready vars, use get_node, or something like that, otherwise the script doesn’t recognize the $sprites, $buttons, etc. The problem is that I have many many sprites and many many buttons and applying this for each individual node would be abysmal.

All I want is a way to store a function, within the same script, as something like: the function only costs 1 line in your editor, but if you open it up the code is all there. But not like folding because the editor still processes those lines, consuming CPU resources. I want something like: the folded functions to be set aside and not be checked all the time.

Why all of this? For 1 simple reason only: I researched on google and apparently after many many lines, like 10k, the editor starts to lag. My code is approaching 3k and is starting to not feel as responsive. I estimate the end code will have something like 9-10k lines. I don’t want to move the code to other scripts, I just want to keep the folded function crystallized so the editor doesn’t lag.

Is it possible? I just want the editor to not lag, I’m fine with the scene having 10k lines of code (I know the reasons programmers hate to do that but for my project it’s working fine).

Thanks for the help! :innocent:

Do you have only one script file with 3k lines? Estimated to have 9-10k lines? What? I hope I misunderstood something.

@onready variables are not the only way for a script to know about nodes. You can gain access to nodes at runtime. You can pass references to them as function and signal parameters. You can also use @export variables to easily add nodes in editor by dragging and dropping them to inspector.

But isn’t your problem that it is not working fine?

Resources for learning:
Best practices
Using signals

The reason it’s lagging is because the editor needs to do parsing and syntax checking.

And yes, for many reasons large single file scripts or classes like that are frowned upon, I would do my utmost to organize it now before it gets worse. But that’s just me.

Pay now or pay later with interest.

1 Like