Guide to the Godot game engine/Making nodes

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Guide to the Godot game engine


Making new nodes can be easy or hard, depending on the planned node. Some will just require this guide, some will benefit from being introduced with an entire plugin. This guide will assume the former.

Write this code:

tool
extends Node
class_name TestNode

export var message = "Hello world!" setget set_message

func _ready():
  print(message)

func set_message(value):
  if value and value is String:
    message = value

The setget tells the engine to run set_message whenever you try to change message. In this particular example, you cannot empty it. Because of the tool, this runs even if you change the value in the inspector.

The class_name tells the engine to add the script to the add node popup, add it to the auto-correction list and to allow you to instance it with TestNode.new().

GDScript