• Godot HelpProgramming
  • How can I set different properties in the inspector window for different nodes with the same script

Ok, maybe sound much complicated. I will try to explain. I have two nodes with a same script attached. In the script I have to variables with "export" property. I want to a first node shows only first variable in inspector also a second node shows the second var. Is it possible?

I'm not sure there is a good way to dynamically make it where what values are exported varies based on the node itself. You could maybe get something working with _get_property_list, though then you'd need to implement the setter and getter functions using _set and _get, which would have a decent amount of boiler plate code. What I might suggest doing is implementing a base class that contains everything both nodes will need, and then extend the class for each node and export the variables there. Something like this, for example:

# base class
# ============
class_name base_class
extends Node

var animal_sound = "bark"
var animal_name = "dog"

func make_animal_noise():
	print (animated_name + " makes sound: " + animal_sound)
# ============

# first class
# ============
extends base_class
export (String) var new_animal_sound = "moo"

func _ready():
	animal_sound = new_animal_sound
	make_animal_noise()
# ============

# second class
# ============
extends base_class
export (String) var new_animal_name = "duck"

func _ready():
	animal_name = new_animal_name
	make_animal_noise()
# ============

Then you can share the majority of the functionality by implementing it in the base class and only overriding/setting the values you need by extending and exporting variables. That is what I would recommend doing if you need to have exported variables that differ but share the same base functionality between multiple scripts/nodes.

Well they are the same script so will naturally have the same variables. If they had different variables, then they would not be the same script by definition. What you probably want is inheritance.

a year later