0
\$\begingroup\$

For my quick test to see if I can create multiple combatants for my battles, I would like to be able to continuous create party members/enemies in order to get to the correct amount of combatants expected (the total amount).

The debugger is showing the root scene children to be:

Running Applications... Party Member<ID:Area2D> ... @Area2D@6#ID

where the expected output should be:

Running Applications... Party Member<ID:Area2D> ... Party Member<ID:Area2D>

and so on, including the enemy types as well

So it looks like it is reverting back to the Area2D, rather than staying as the Party Member class custom object.

I have followed many different ways of creating the new instances but none are working as expected. I was able to test quite a lot up until now where I need to be able to access the object properly in order to determine what type it is and call the expected function, etc.


func createPartyMemberObject(root, job):
    
    partyPath = load("res://Resources/Party Members/Party Member.tscn")
    
    var party = partyPath.instantiate()
    party.resource = job

    print_debug(party)

    root.add_child(party)

    print_debug(party)

    return party

In fact it seems to keep the Party Member class up until it is ready to be added to the root path. So it keeps the class name until after adding the child into the scene root (add_child). - I'm not sure if there is something that I am missing here.

For the first instance it continues as expected, but from the second instance on it isn't working properly.

I have tried creating multiple different variables in order to track the different party members but that didn't work either.

Possibly it has something to do with the partyPath, but I'm not sure what it could be. maybe the instantiate only works for the one object rather than multiple. Commenting out the load var and keeping at a higher level, etc. didn't work.

I couldn't find any bugs in godot github, so I'm not sure if it is a bug or something that I am missing.

Thank you in advance for any help.

\$\endgroup\$
2
  • \$\begingroup\$ Your class_name is "Party Member"? I'm not sure that's a valid class_name. Could you try "PartyMember" or "Party_Member"? \$\endgroup\$
    – Alex CB
    Jul 25, 2023 at 10:42
  • \$\begingroup\$ If I understand correctly, this is about the name of the Nodes. That is an String that can change, so do not rely on it for checking the type. If you need the name to be something specific, set it. The issue is that Godot will keep the name of the Node if there is no collision, for the second instance there is already a Node with that name, so Godot generates a name... And it doesn't use the class_name or the original name to do it. The process has performance implications for games that make lots of Nodes dynamically, so I'll reiterate that if you need the name, set it. \$\endgroup\$
    – Theraot
    Jul 27, 2023 at 15:00

1 Answer 1

1
\$\begingroup\$

Try statically typing your vars (and func return value), like this:

var party_scene := preload("res://Resources/Party Members/Party Member.tscn")

func createPartyMemberObject(root, job) -> PartyMember:
  var party: PartyMember = party_scene.instantiate()
  ...

Source.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks. I did try this to figure out if there is some debugging options, due to the expected syntax. I feel a bit silly about leaving the return empty. In this case though it didn't help much to solve my problem. I am getting the same feedback - @Area2D@6, etc. Not sure if there is anything further to do with this response. \$\endgroup\$
    – BoydyBoydy
    Jul 25, 2023 at 12:47
  • \$\begingroup\$ The return of the func it's just for you to know. Have you tried typing the var party : PartyMember? \$\endgroup\$
    – Alex CB
    Jul 25, 2023 at 13:00
  • \$\begingroup\$ yes. thanks. i did add the var party: partymember. I had copied your example as another test. I updated my Party Member.gd to have a class_name PartyMember based on the other comment :) \$\endgroup\$
    – BoydyBoydy
    Jul 25, 2023 at 13:25
  • \$\begingroup\$ I still wasn't able to figure out why the objects were named as they were after the second object was created. But through the static typed format I have worked out how to properly compare objects. It seems that even if the names aren't correct the object is still a PartyMember Class and so can be used as I had originally expected. I have then updated my code to reflect this class comparison rather than the name of the object itself. ... if i is PartyMember, etc. \$\endgroup\$
    – BoydyBoydy
    Jul 26, 2023 at 7:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .