Overriding parent class variable

Godot Version

4.2.1

Question

I have a parent and child classes. The parent class defines a variable and uses it in some methods. I want to set the variable by the child so that the parent will act on that value.

First attempt:

class_name BaseTile extends Area2D
var tile_type : String = "Unasssinged"

func _input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event.is_action_pressed("mouse_left_click"):
		print("clicked " + tile_type)
		
class_name ForestTile extends BaseTile
func _ready():
	tile_type="Forest"

Result when clicking on the node:

clicked Unasssinged

Second attempt with extra debug messages for good measure:

class_name BaseTile extends Area2D
var tile_type : String = "Unasssinged"

func set_tile_type(_type: String):
	print("setting type in parent")
	tile_type = _type
	print("Parent was changed: " + tile_type)

func _ready():
	print("Parent is ready: " + tile_type)

func _input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
	if event.is_action_pressed("mouse_left_click"):
		print("clicked " + tile_type)

class_name ForestTile extends BaseTile

func _ready():
	set_tile_type("Forest")
	print("Child is ready: " + tile_type)

func set_tile_type(_type: String):
	print("setting type in child")
	super.set_tile_type(_type)

Result:

Parent is ready: Unasssinged
setting type in child
setting type in parent
Parent was changed: Forest
Child is ready: Forest
clicked Unasssinged

Is there a way to solve this?

Works for me on 4.2.2, is this really the code you are using?

Mostly, I removed some extra code that wasn’t relevant. I’ll explain the scene structure:
The parent class is attached to an Area2D scene BaseTile:

image

The CollisionShape2D triggers the _input_event(…) func

The child class is attached to another Area2D scene ForestTile which also includes the BaseTile:

image

This is the project file:

Forest tile is not inheriting the base tile script; maybe you want to make a new inherited scene of the base tile by right clicking BaseTile.tscn in the filesystem. Then right clicking the root node and extending the script to ForestTile.

BaseTile is only a child Node in your set up, not a parent/base Class

I didn’t know it was possible to create a parent/child scene. I was relying on the relation via the scripts. What’s the difference between the two?

The main difference is that having a child node does not mean the parent extends it. Extending scripts is not related to the scene tree.

A new Inherited scene is like soft-copying the scene, you can still update the old version and it’s changes will propigate to the inherited scenes, similar, but not the same as nor related to extending scripts.


Chances are your script would work if you removed BaseTile and gave the ForestTile a collision shape, I only recommend a inherited scene because there is more to the BaseTile than just a collision shape and type string.

I need to change BaseTile.tile_type_name from ForestTile. What’s the best way? Inherit the BaseTile or access it some other way?

Final update. I inherited the scene as suggested, and also inherited the class. Work as expected now. Thanks a lot @gertkeno

1 Like