Skip to content

Commit 6860dce

Browse files
committed
Added FAQ Info about adding custom types to the editor via a plugin.
Included changes requested by PR #35 review Previous round of fixes. Squashing previous corrections into a single commit. Fixing merge error with summary.md
1 parent d3b1163 commit 6860dce

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [Rust Panic Hook](./recipes/rust_panic_handler.md)
2626
- [Logging](./recipes/logging.md)
2727
- [Loading external resources](./recipes/loading_external_resources.md)
28+
- [Custom nodes Plugin](./recipes/custom-nodes-plugin.md)
2829
- [Exporting](./exporting.md)
2930
- [Android](./exporting/android.md)
3031
- [(TODO) iOS](./exporting/ios.md)

src/faq/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ library = ExtResource( 1 )
5151
5252
```
5353

54+
## Why aren't my scripts showing up in the Add Node portion of the editor even though they inherit from `Node`, `Node2D`, `Spatial` or `Control`?
55+
56+
Due to limitations with Godot 3.x's version of GDNative, NativeScript types do not get registered in the class database. As such, these classes are not included in the "Create New Node" dialog.
57+
58+
A workaround to this issue has been included in the recipe [Custom Nodes Plugin](../recipes/custom-nodes-plugin.md).
59+
5460
## Can I use Rust for a [toolscript](https://docs.godotengine.org/en/stable/tutorials/misc/running_code_in_the_editor.html)?
5561

5662
Yes, any Rust struct that inherits from `NativeClass` can be also used as a tool class by using the `InitHandle::add_tool_class` during native script initialization instead of `InitHandle::add_class`.

src/recipes/custom-nodes-plugin.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Custom Nodes Plugin
2+
3+
While GDNative in Godot 3.x has some limitations, creating an `EditorPlugin` that registers custom nodes can be used to help integrate your types more tightly in the engine. Using a Custom Nodes plugin, you may register as many `NativeClass` scripts as you need in a single plugin.
4+
5+
1. Create the directory "res://addons/my_custom_nodes/".
6+
2. In the directory "res://addons/my_custom_nodes/", create `plugin.cfg` and `add_my_custom_nodes.gd` and include the following code for each.
7+
8+
## plugin.cfg
9+
```ini
10+
[plugin]
11+
name="My Custom Nodes"
12+
description="Adds my custom nodes for my native classes."
13+
author="your-name"
14+
version="0.1.0"
15+
script="add_my_custom_nodes.gd"
16+
```
17+
18+
## add_my_custom_nodes.gd
19+
```gdscript
20+
tool
21+
extends EditorPlugin
22+
23+
func _enter_tree():
24+
# Get the base icon from the current editor interface/theme
25+
var gui = get_editor_interface().get_base_control()
26+
var node_icon = gui.get_icon("Node", "EditorIcons")
27+
28+
# Alternatively, preload to a custom icon at the following
29+
# var node_icon preload("res://icon_ferris.png")
30+
31+
add_custom_type(
32+
"MyNode",
33+
"Control",
34+
preload("res://gdnative/MyNode.gdns"),
35+
node_icon
36+
)
37+
38+
# Add any additional custom nodes here here.
39+
40+
func _exit_tree():
41+
remove_custom_type("MyNode")
42+
# Add a remove for each registered custom type to clean up
43+
```
44+
45+
From the editor's main menu, find "Project > Project Settings > Plugins". Find "My Custom Nodes" in the list of plugins and activate this. From here, you can create these directly from the "Create New Node" dialog. In addition, any nodes created this way will disallow the script from being changed or removed.
46+
47+
For more information about this kind of plugin, please refer to the [Godot Documentation](https://docs.godotengine.org/en/stable/tutorials/plugins/editor/making_plugins.html#a-custom-node).
48+
49+
**Note**: This method only works for types that inherit from [`Script`](https://docs.godotengine.org/en/stable/classes/class_script.html). In addition, changing the path to the ".gdns" file will cause the custom `Node` to no longer register correctly.
50+
51+
**Note 2**: This does not register your custom classes in the class database. In order to instantiate the script from GDScript, it will still be necessary to use `var ScriptType = preload("res://path/to/my_node.gdns")` before attempting to instantiate it.

0 commit comments

Comments
 (0)