Spawning Instances: Best Practices?

martymarty Posts: 114Member
edited March 25 in Programming

I was wondering how to best handle creating and managing multiple instances of a given object in Godot.

In other contexts, I've stored references to like instances in an array. This is handy since you can easily reference all of the instances or specific ones by index to call their methods, etc.

It seems that Godot supports this approach.

But it also looks like Godot prefers that we accomplish this sort of thing using Groups and Signals. This would work, maybe even more efficiently, but it would also make for less portable code as it would leverage Godot-specific features (ie. Godot signals and groups).

So, what's the consensus on accomplishing this common task in Godot?

Thanks!

Answers

  • SolarLuneSolarLune Posts: 35Member

    It's up to you with the use case at hand. I haven't used Godot for long, but for messaging objects specifically, there's a few different ways to do it - it seems a bit redundant, to be honest. You should just go with whatever is necessary for your particular use case as it grows and goes along. Rewriting code isn't a huge deal.

  • TwistedTwiglegTwistedTwigleg Posts: 907Admin

    I would go with whatever you feel most comfortable with. I'm not sure on what the best practice is, but I've always followed "make it work, and then make it pretty".

    Personally I use dictionaries for storing instances, occasionally lists when I need to. That's just my preference and even then, it changes based on what I'm doing and how I'm feeling. Sometimes I go back and rewrite things, most times I don't. I've never used groups, and I sometimes use signals.

    I'd just go with whatever is the most enjoyable/comfortable for you at the time.

  • martymarty Posts: 114Member
    Answer ✓

    So nothing wrong going either way, sounds like.

    I'm thinking then that I'll opt to stick with arrays full of references to instances instead of anything Godot-centric.

Sign In or Register to comment.