Static typing and child-parent casting

Godot Version

4.3.stable.official

Question

I have some simple Parent - Child class hierarchy. I store object of both classes in the dictionary. I often iterate over this dictionary. How can I efficiently cast objects that I get from here? Right now I’m doing something like this:

	for p in dict:
		var obj= dict[p] as ParentClass
		#do something specific to parent class
		if obj.get_type() == ChildClass.get_type():
			var child_obj = obj as ChildClass
                #do something specific for child class

get_type() is static and returns some predefined value, depending on class

I would like to also use strict typing, which will make things even harder.

could you use if obj is ChildClass:?

I think I was not precise enough. I wanted (ideally) to have cpp-like type of casting:

var obj : ParentClass = dict[p] as ParentClass

and then Godot would know this isn’t really ParentClass, but ChildClass. But now I know it is not possible.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.