I'm new to Godot and GDscript, in the game I've just started to develop, I will have different types of tiles for the ground (grass, sand, rock...), which share some functionalities, for example, their deletion upon _exit_tree(), since it will be possible to remove and substitute some of them during the game. I've thought to have a parent class where to put these shared functionalities and then extend it by the tiles. The parent class is linked to nothing, it is a script staying on its own. When I save this script, Godot tells me "This script is not linked to any node, but an external resource has been saved anyway". Will this give me any trouble or is it something not to care about and everything will be fine?

Yes, you can have loose scripts. It's easier if you give them a class name.

extends Node
class_name Grass

Then in another script, you can create a new Grass like this;

var grass = Grass.new()

I use a lot of naked scripts, but they generally extend node or node2d. I thought that scripts had to at least explicitly extend resource. Is that not the case?

Yes, you have to extend something. The most basic class is Object, but it is basically useless and just a base for other classes. So you almost always want to use Node or something more specific.

My script extends Spatial, that should be fine, correct?

Hi,

If script extends Node or any Node derived class then you need to remember to manually free it, right? (At least if it is not added to scene tree)

For scripts not attached to any nodes I use mostly Resource (if that actually is resource) or Reference (if script only provides some logic) as base class.

I'll try to better explain my idea. The world ground can have different types of tiles: grass, rock, sand, water, and swamp.

At the moment I've created a ground_tile.gd script containing:

extends Spatial

class_name GroundTile

func _exit_tree():
    self.queue_free()

# other things shared by each kind of tile

This script is not attached to any node.

Then I have the tiles scripts which look like this:

extends GroundTile

class_name GrassTile

const walkable : bool = true
const navigable : bool = false
const speedModifier : float = 1.0

# Some functions below

This grass tile is a scene that has a Spacial as root (the script is attached here), then a mesh, a static body, and a collision shape.

Do you think I should change what the scripts extend?

@Alhazred said: Do you think I should change what the scripts extend?

Ah, in this case it's fine in my opinion, because finally script is attached to some node.

In my project I have some script called GenericObject which extends Spatial and is base for any game object. It's not assigned to any node itself but its derived classes are and your usage seems very similar here.

One thing I see a bit unnecessary is:

func _exit_tree():
         self.queue_free()

If scene tree is destroyed then it will automatically free all children and in case you want to remove tile manually you can just call queue_free directly on that node. I mean that code is not bad, you assure that it will be freed even when you just call remove_child but I think that remove_child is for cases when you actually want to keep node and if you doesn't need it any more you can just call queue_free directly, then what it does is remove child and free.

If your GrassTile extends GroundTile and node with assigned GrassTile script will be freed then GroundTile script will be also freed as it's base of GrassTile.

9 months later