I have the below code snipped that loops over all nodes in group tileFrames. Per design, all my nodes in that group are instances of the same scene "TileFrame" and I have also declared that as class_name in the linked gdscript.

In the Editor it is now not auto-completing custom variables like mouse_hover (I guess because at that time the compiler does not know what classes to expect here which is fine). Can I somehow tell it what class this is or assign a class to it?

var frameTiles = get_tree().get_nodes_in_group("tileFrames")
			if frameTiles.size() > 0:
				for tile in frameTiles:
					if tile.mouse_hover:
						global_position = tile.global_position
						state = State.DROPPED

Max

  • xyz replied to this.
  • max_godot No, as far as I know. You can't have a typed for iterator if a container you iterate over is not typed. Nothing in your code says to the interpreter which type is contained in frameTiles. Each entry can be of any type so how would the interpreter know which class it is?

    However, the assist should work if you iterate over a typed array:

    var array: Array[MyClass] = [MyClass.new(), MyClass.new()]
    for a in array:
    	a.

    If you can guarantee that array contains only the specific type you can cast the whole array:

    for tile in frameTiles as Array[MyClass]:
    	tile.

    max_godot You can cast it using as keyword:

    var t = tile as MyClass

      xyz but that means I would have to create a new variable for it - is there no way to do it right away in the FOR call?

      • xyz replied to this.

        max_godot No, as far as I know. You can't have a typed for iterator if a container you iterate over is not typed. Nothing in your code says to the interpreter which type is contained in frameTiles. Each entry can be of any type so how would the interpreter know which class it is?

        However, the assist should work if you iterate over a typed array:

        var array: Array[MyClass] = [MyClass.new(), MyClass.new()]
        for a in array:
        	a.

        If you can guarantee that array contains only the specific type you can cast the whole array:

        for tile in frameTiles as Array[MyClass]:
        	tile.

          xyz Thanks, the Array casting was exactly what I was looking for! 🙂