Autoload, Singletons, .gd, .tscn, .tres and their practical usage

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nikola-Milovic

I don’t know if the title is too vague or too abstract/ big, but the general idea is. What are those things used for when combined with Autoload and in general. I understand that you can use Autoload in project settings with .gd’s to have some logic that can be reused everywhere which seems super useful. Also .tscn can be used to have an ObjectRegistry for units/projectiles and so on. But can .tres be used for something as well? Also some other usages. I cannot get my mind around the idea of structuring godot projects, how do you store persistent data? I head about scriptable objects and making resource files but don’t understand what exactly those .tres files do and what’s their usage. If someone could showcase some functionalities using simple examples it would be great. Thank you very much. Basically I am asking about Singletons and .tres (scriptable objects). Those two confuse me the most.

:bust_in_silhouette: Reply From: Magso

In terms of Unity since you mention scriptable objects, a singleton is like using [RuntimeInitializeOnLoadMethod] not to be confused as using DontDestroyOnLoad(); (I’ll come back to this) and .tres files are assets/resources such as materials, physicsmaterials, environments, etc. To make a new resource type or scriptable object, use the key word class_name followed by it’s name.

extends Resource
class_name my_new_resource

Right click in the file system tab and select new resource and the script will show up amongst all the other inbuilt resources.

A .tscn file can double up as either a scene or prefab depending on the top node of the scene. Using any node with a transform as the scene’s root means it can be loaded into any other scene like a prefab; usually for maps I personally use a plain Node as the root. Because of this scenes in Godot don’t use a scene manager, they essentially behave like DontDestroyOnLoad(); until either queue_free() (Destroy()) is called or the scene tree, which is like the game’s instance itself, is told to load a specific scene using get_tree().change_scene("path/to/scene").

For some reason I didn’t receive email notification. Could you expand upon this just a tad bit. What about extending Resource instead of Node? Does that make a difference, also I don’t quite understand the class_name and what it does. And what should I do with the .tres files? I saw that mostly it’s used for storing data while saving or for like character. But how do you access that data, or how can you even save data to it?

Nikola-Milovic | 2020-03-22 21:10

It can extend both Node and Resource (the example should’ve been Resource not Node, I should change that). Extending as a Node will allow it to show up in the list when creating a new node, this would only be used for specialised scripts that add a certain functionality/feature to the scene. Here’s the explanation of class_name.

The .tres files hold data that nodes will use for example a Camera node uses an Environment resource which can be saved as a .tres file and it can be reused. A .tres file is saved by either creating a new resource from the file system tab or creating new resource data for a node in the inspector like, for another example, a new physicsmaterial and once created click the dropdown arrow and a save option should be there. The data can be accessed again once the resource is selected in the file system or has been added to a node.

Magso | 2020-03-23 01:08