Hi,
I have list of surnames and names in two separate files from which I try to create group of people by randomizing it.
Firstly I just sticked names to labels, added these labels to the group and later pulled one random name from it as the player name. That worked.
Later as the number of properties of that people will inevitably rise I created the class Human and created objects from this class and also added them to the group. Now I get the error when I try to get that "Player" name. Obviuosly I lack knowledge how to work with groups and objects so heeelp lol
Here is the code for creating people

func populate_village():
	for i in range(40):
		var Labela =labela.instance()
		#Labela.add_to_group("Heads_of_Families")
		Labela.bbcode_enabled = true 
		var human = Human.new()
		human.Name = get_me_name("male")
		human.Surname = get_me_name("surname")
		human.Age = randi() % 45 + 15
		human.add_to_group("Heads_of_Families")

Here is the code for choosing one of them

func choose_one():
			Heads = get_tree().get_nodes_in_group("Heads_of_Families")
			Child = Heads[randi() % Heads.size()]
			childName = Child.Name
			Child.set("custom_colors/default_color",Color(1,0,0)) 
			Child.text = childName

The error comes at 4th line childName = Child.Name
Invalid get index Name on baseNode2D

  • DaveTheCoder replied to this.
  • ZedMcJack
    There's nothing wrong with your variable assignment. My guess is that Child doesn't have the type that you think it does. Try this to see what the function is actually doing:

    func choose_one():
    		Heads = get_tree().get_nodes_in_group("Heads_of_Families")
                    print(Heads)
    		Child = Heads[randi() % Heads.size()]
                    assert(Child is Human)
    		print(Child.Name)
    		print(Child.Surname)
    		print(Child.Age)

    It might be that you're forgetting to add the generated humans to SceneTree. From the docs:

    Nodes can be assigned a group at any time, but will not be added until they are inside the scene tree (see is_inside_tree). See notes in the description, and the group methods in SceneTree.

    Put this in populate_vilage():

    func populate_village():
            ...
            add_child(human)
            human.set_owner(self) # optional. See links below
            human.add_to_group(...)

    See PackedScene and Node for an explanation of owner

    DaveTheCoder Name should be name (lower case).

    Well, changing it to lower case brings that its name is Node2D and the error is line 6: Invalid set index 'text' (on base: 'Node2D (Main.gd)') with value of type 'String'.
    Is it possible to add objects to Group? It seems it is as there is no error when I did that, but how do I read/reference now those objects and its properties in Group?

    A Node2D does not have a property "text". What are you trying to do with line 6?

    It looks like you're confusing Label's (or RichTextLabel's) and Node2D's. They have different properties.

      DaveTheCoder A Node2D does not have a property "text". What are you trying to do with line 6?

      It looks like you're confusing Label's (or RichTextLabel's) and Node2D's. They have different properties.

      There is a class Human

      extends Node
      
      class_name Human
      
      var Name
      var Surname
      var Age
      var Health 
      
      # Called when the node enters the scene tree for the first time.
      func _ready():
      	pass # Replace with function body.

      In function Populate_village() I create objects for each person and add it to the group Heads of Families.
      In function Choose_one() I am trying to pull one random name from the group Head of Families but it doesnt work. The variable Child is misleading it should be Man or Player.

      What do you mean by "it doesn't work"? Please provide more details.

      I explained above why this line produces an error:
      Child.text = childName

        DaveTheCoder What do you mean by "it doesn't work"? Please provide more details.

        I explained above why this line produces an error:
        Child.text = childName

        You were right, variable Child doesnt have that property. So I changed a bit that function to be more specific what I need. It now looks like this:

        func choose_one():
        		Heads = get_tree().get_nodes_in_group("Heads_of_Families")
        		Child = Heads[randi() % Heads.size()]
        		print(Child.Name)
        		print(Child.Surname)
        		print(Child.Age)

        And it trhrows this error Invalid get index 'Name' (on base: 'Node2D (Main.gd)').
        I am certainly doing something wrong but I do not see what it is.
        In VFP (where I am coming from) you can assign an object to variable like this:

        x=CREATEOBJECT("custom")
        y=x
        ?y.Name
        #it shows Custom

        So I thought if I have objects in a group Heads of Families and choose one of it by assigning a variable to it, Ill get the objects properies and it doesnt work. At least not in the way I am expecting it.

          ZedMcJack
          There's nothing wrong with your variable assignment. My guess is that Child doesn't have the type that you think it does. Try this to see what the function is actually doing:

          func choose_one():
          		Heads = get_tree().get_nodes_in_group("Heads_of_Families")
                          print(Heads)
          		Child = Heads[randi() % Heads.size()]
                          assert(Child is Human)
          		print(Child.Name)
          		print(Child.Surname)
          		print(Child.Age)

          It might be that you're forgetting to add the generated humans to SceneTree. From the docs:

          Nodes can be assigned a group at any time, but will not be added until they are inside the scene tree (see is_inside_tree). See notes in the description, and the group methods in SceneTree.

          Put this in populate_vilage():

          func populate_village():
                  ...
                  add_child(human)
                  human.set_owner(self) # optional. See links below
                  human.add_to_group(...)

          See PackedScene and Node for an explanation of owner

            catnc
            Thanks catnc I just added those 2 lines in the populate_village() function and it worked. 🙂

              ZedMcJack Glad I could help. In case you haven't, I do urge you to read up on set_owner(), or you could end up with obscure bugs later down the line, e.g. if you're building a village from another node, say Region, then set_owner(self) would add humans to Region, not Village.