I've read what I could about the subject of exporting, but I either don't understand, probably the reason, or haven't found the solution.

My player has five slots that should link to Ability class objects. I'd like to export those "slot" variables so I can add/remove abilities on the fly for testing.

Thank you! ?

From what I know, I believe exporting custom classes in Godot can be tricky. I know you can export classes that extend Resource, but to what I extend I do not know. There is a proposal to improve exported custom resources, but right now it is just a proposal.

If you can, you might be able to have your ability class objects extend Resource, and then they may be usable with something like export (Ability) var ability_slot_one, but I have not used it myself so I don't know how usable it is. It might be worth trying though.

In my projects, I have worked around issues like this in two different ways.


The first way I dealt with this was through a exported integer and a list. Something like this for example (untested):

export (int, "none", "water", "fire", "earth", "air") var ability_one
var ability_slot_one = null

const ABILITIES = [
	null,
	preload("res://abilities/water.gd"),
	preload("res://abilities/fire.gd"),
	preload("res://abilities/earth.gd"),
	preload("res://abilities/air.gd"),
]
func _ready():
	var ability_slot_one_class = ABILITIES[ability_one]
	if (ability_slot_one_class != null):
		ability_slot_one = ability_slot_one_class.new()

The other way is using class_name and doing a comparison through a series of if - elif - else conditionals (untested):

water.gd

class_name Ability_Water
extends Node
# stuff here. This pattern is repeated for Fire, Earth, and Air
# abilities, with "class_name Ability_Water" changed based
# on the ability

player script:

export (int, "none", "water", "fire", "earth", "air") var ability_one
var ability_slot_one = null

func _ready():
	if (ability_one > 0):
		if (ability_one == 1):
			ability_slot_one = Ability_Water.new()
		elif (ability_one == 2):
			ability_slot_one = Ability_Fire.new()
		elif (ability_one == 3):
			ability_slot_one = Ability_Earth.new()
		elif (ability_one == 4):
			ability_slot_one = Ability_Air.new()
		else:
			print("Unknown ability set to ability_one!")
			return

That is just how I have gotten around it in the past. Anymore I find I just have multiple nodes and use NodePaths to link them together, but it can be a tad unwieldy depending on how linked things need to be.

That said, there are probably better ways to handle exported custom classes and/or work around them, but I'm not aware of them and/or forgot. Regardless, hopefully this helps a little bit :smile: (Side note: Welcome to the forums)

7 days later

Thanks so much! This is a very good solution, love it.

3 years later