The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

My main file is getting quite big with lots of functions. I've started to create new .gd files that extends Resource and load them into the main file. I don't know if this is the "right" way or if it is bad for speed or something. Do you know a better way?

Here’s an example:

# character-functions.gd
extends Resource

func do_something():
    pass

# main.gd
extends Node2D
const CHARACTER_FUNCTIONS = preload("res://scripts/character-functions.gd")

@onready var character_functions = CHARACTER_FUNCTIONS.new()

func something_else():
    character_functions.do_something()
Godot version 4.0.3.stable
in Engine by (23 points)

1 Answer

0 votes
Best answer

Godot is really supposed to be used with nodes, maybe you could separate functionality from the main script to multiple sub nodes. But without knowing more about what your main and character-functions actually do, you could extend nothing instead of extending Resource, and make the functions static:

# character-functions.gd
static func do_something():

# main.gd
const CHARACTER_FUNCTIONS = preload("res://scripts/character-functions.gd")
func something_else():
    CHARACTER_FUNCTIONS.do_something()

This way you won't be instantiating the character-functions.gd class at all, you are just calling the static functions from it.

If the script functions cannot be static, you should extend RefCounted instead, since it's the simplest class that does memory management for you (gets freed from memory when no longer in used / in scope). Resource is the base data-container class, so it's otherwise identical to RefCounted, except it includes functionality to save itself to the disk. See the When and how to avoid using nodes for everything documentation page for more info about these classes.

by (509 points)
selected by

Thank you very much. I'll use the static function then and not instantiate the script. That's mainly for functions that return something or change a value.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.