Exported class, and visibility in editor

Godot Version

4.3

Question

Hi,
I wrote class Bouncing which export some variables. I add this class to one node, and also export it, creating new instance in same line. When i create int variable, it’s possible to change it in editor as intedned, but in “Bouncing” section, there is only possible to assign new value.

Node which use and export Bouncing class part

@export var testInt: int = 12
@export var bouncing: Bouncing = Bouncing.new()

Boucing class part

@export var bouncing_object: Node3D 
@export var bouncing_speed = 5.0
@export var bouncing_height = 0.5;

And the result is…

image

I wan’t for example change bouncing_speed from this place.

What i am doing wrong?

I’d rather instantiate the bouncing using the instantiate method (with a PackedScene being a template/empty Bouncing node) rather than the new constructor, maybe that could explain why Godot doesn’t truly instantiate your Bouncing node, since your class does not have a static method called ‘new’ overriding the constructor

This would become:

@export var bouncing: Bouncing

const BouncingScene = preload(...)

func _ready() -> void:
    bouncing = BouncingScene.instantiate()
    (...).add_child(bouncing)

I see two ways of doing this.
The simplest is to add the exports to the node that you are editing:

@export var testInt: int = 12
@export var bouncing_object: Node3D 
@export var bouncing_speed = 5.0
@export var bouncing_height = 0.5   
var bouncing: Bouncing = Bouncing.new()
func _ready()->void: 
   bouncing.bouncing_object = bouncing_object
   bouncing.bouncing_speed = bouncing_speed
   bouncing.bouncing_height = bouncing_height

Unfortunately for this method if you have more than one node using bouncing, you will be repeating code.

The other method is to use composition architecture.
Create a BouncingComponent node and add a script to it:

#BouncingComponent
@export var bouncing_object: Node3D 
@export var bouncing_speed = 5.0
@export var bouncing_height = 0.5

Then just add that node to any scene/object that can bounce and you will have your exports available in the editor.

I think i’m do it wrong.

This is not a Node, RefCounted or Resource. It’s just a plain class, which i use as “trait”, so add necessary code to user node to bounce. Should I avoid that approach. Should i always use one of Node/RefCounted/Resource?

Don’t use Nodes for a new’d @export, sounds like it’s getting deleted or lost because it’s not added to a scene tree. For reference @export nodes are saved as node paths, so it can’t store unpathable Nodes.

If it has to be a Node then you have to add it to the scene, otherwise switch to a Resource for it to be editable in inspector.

If you don’t specify an inheritance using the extends keyword, your class will inherit from RefCounted.
See here.

Traits are components of game objects. Composition in Godot is explained well here.

There is a dedicated Traits architecture coming for Godot but it is not here yet.