Is it possible to set type hints in code ?
Asked Answered
E

5

0

When i am writing something (poorly thought out) like :

class_name DataSet
extends Resource

@export var resource_type: String = "Resource"
@export var resource_set: Array[resource_type]

The engine doesn't seems to appreciate it and tells me :
Parse Error: "resource_type" is a variable but does not contain a type.

I get that it's not the smartest way to go but I have no idea of workaround for that. The resource_type can't be typed hint as a 'Type' itself.

My wish would be to create multiple DataSet resources that would be type hinted according to the custom Resources they store for safer access and the ease of "quick load"-ing in the inspector.

Ezara answered 3/3 at 21:16 Comment(0)
P
0

Ezara The whole point of static typing is to specify the type before the compilation (hence: static) to minimize bugs caused by (mis)casting at assignments. Using a variable for this defies the entire purpose of static typing. If the contained type can vary, just use a non-typed array.

Postpone answered 3/3 at 22:3 Comment(0)
E
0

Postpone
I get what you are saying but if the sole purpose of that variable is to be set in editor to assert that the resources added to the data set are of the correct custom type, as each data set should be bounded to one resource type ? Doesn't it add to the safety ? If it's a @tool script, the resource_type @setter could be constrained to the editor.

Obviously having one custom script for each data set with a clearly defined type hint would be better but for dozen set the whole thing might get redundant.
(Inheriting from the base data_set and just overriding the @export variable with the desired type hint might be the best option tho)

On the initial question, the property_list gives some clue on how it would be simple to change that whenever the resource_type String value is set by overriding the "hint_string" of the resource_set property

{ "name": "custom_resource_set", "class_name": &"", "type": 28, "hint": 23, "hint_string": "24/17:CustomResource", "usage": 4102 }
{ "name": "resource_set", "class_name": &"", "type": 28, "hint": 23, "hint_string": "24/17:Resource", "usage": 4102 }

Ezara answered 3/3 at 22:15 Comment(0)
P
0

Ezara Why do you need a typed array to hold the resources? Just use a plain array.

Postpone answered 3/3 at 22:36 Comment(0)
E
0

Postpone
I am the "dev" on the game we are working on but we would like that non-dev team members could have straightforward tools in Godot to edit Resources (which will store most of the data of the game), mostly in the inspector. I am trying to find ways to expose things in the inspector in a manner that doesn't leads to wrong typed error and that doesn't imply tons of different classes for that.

Right now it looks like this, I don't how much value it has in the long run tho

Ezara answered 3/3 at 23:0 Comment(0)
P
0

Ezara Strictly speaking this has nothing to do with static typing, it's just a gui thing. Yeah, you can do it via property hints but what's the difference between typing a class name into gui vs typing it into script declaration? If you expect non programmers to choose the type then it'd be better to provide a selectable enum instead.

Postpone answered 4/3 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.