How i can create custom data type?

Godot Version

4.1.2

Question

I try create custom data type for my dialoge system. How i can do it? i try make inner class.But in inspector when i export it. it is null.

I haven’t done it, but I think I’ve seen it in a tutorial and as far as I can remember:

TextPart should 1) extend Resource, and 2) have all variables you want to be exposed also be marked as @export

ps: and it may help to indicate type, such as @export var text_parts: Array[TextPart], but probably isn’t needed.

1 Like

You will need to create a seperate file for TextPart

TextPart.gd

extends Resource # This can be any Node type, but for your purposes you only need a resource
class_name TextPart # This line makes the class available to other scripts

var text_content = "text"
# Rest of your variables...

func _init(_text_content:= "", ): #Default parameters as per @mrcdk's reply
# Definition 

With this script saved (not attached to anything), you can use it as a custom type.

@export my_text_part: TextPart

Edit:The docs have a tutorial on this exact topic, which is worth a look here.

1 Like

Inner classes can’t be exported. Classes with no extends <class> automatically extend RefCounted.

Create a script that extends Resource and export the variables you want to have in the inspector. Resource with an _init() function with parameters will fail to load if the parameters don’t have a default value.

1 Like

that is good, but what if i need create an array with it in inspector?

Sorry, didn’t notice you needed an array. You can create an exported array of type TextPart like this:

#main.gd

extends Node2D

@export var text_parts: Array[TextPart] = []

func _ready():
	text_parts.append(TextPart.new("Foo", "Bar", 42))

Note, I don’t instantiate any TextPart objects until _ready(). Not sure if there’s a way around this, but I couldn’t get it working otherwise.

Also, here’s an updated TextPart script to fix an issue with _init() not running properly due to weak typing of the optional parameters:

# text_part.gd

extends Resource
class_name TextPart

@export var text_content = ""
@export var text_name = ""
@export var text_speed = 1

func _init(_text_content:= "", _text_name:= "", _text_speed:= 1):
	text_content = _text_content
	text_name = _text_name
	text_speed = _text_speed

Hope that helps; feel free to ask any questions :slight_smile:

1 Like

also, what if i create text_part.gd like this?
изображение
by the way, it still works

1 Like

Perfectly fine; static typing is great practice, and improves autocompletion in the editor.

2 Likes

So I discovered that if I write a new data type on top of the TextPart, it won’t work fine:


how i can fix it?

Everything in your screenshot look okay, so I couldn’t say for sure where the issue is.

Here’s a working version attempting to recreate your setup: (Apologies for the image format)

Note, it looks like the default parameter’s aren’t nessesary as previously thought - although they weren’t giving me any trouble either.

If you can’t get it working, you’re welcome to share project files and I can take a look. :slight_smile:

I see everything is ok, how can I share the project with you? github or something like that? (thank you for helping)

No problem. GitHub is fine; alteratively, you can zip the project folder and share via Google Drive, OneDrive, etc.

here:

I’ve never seen this problem before, so am not 100% certain why this is the case, but the problem is that ActionTextPart, which is a Resource-derived class cannot export an Action, because it is a Node-derived class.

If you change action.gd to extend from Resource instead of Node, everything works for me. If you needed node functionality here, you may need to rethink your system.

Fixed action.gd:

extends Resource
class_name Action

func do_action():
	pass

not sure, but I change action.gd to Resource. Nothing changed. oh i reload godot and it work! thanks!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.