Here’s a tutorial I wrote on using components to help keep your code organized.

3 points
*

Why are people still using string references? Like in the case of “Bumpable” - this could be a class name and reference could be via the class name instead of checking the node name by string. String references are super dangerous, as you will get a potential error only at runtime if you make a typo, IDE can’t help you. Don’t get me wrong - I like the idea behind component pattern and I use it myself sometimes, but the execution could be better in my opinion, unless I’m missing some obvious reasons for doing it this way.

permalink
report
reply
1 point

That would definitely be an improvement, using classes is probably more robust. In my case, the project wasn’t too huge and it was solo, so I never hit issues with string references.

permalink
report
parent
reply
1 point
*

It’s a good practice to adhere to proper design patterns even on smaller projects. I also work solo on small projects :). It doesn’t really cost you anything to work with classes, it’d even say it’s easier once you get a hang of it

permalink
report
parent
reply

@mrsgreenpotato @TheLongPrice Isn’t the string reference just in the GetNodeOrNull call? I prefer to just export the nodes I need, so I don’t build up Node-structure dependencies in the code. Therefore I don’t have “Magic strings” in my code (almost) at all.

permalink
report
parent
reply
1 point

Correct, I’d also use exported values if possible. But that isn’t flexible enough in some cases, because you’d need to individually export all possible attributes that the node might have.

For it to be more flexible, you could have something like this:

for child in get_children():
  if child is ClassNameHere:
    return child

That would give you the same result as described in the article, without string reference. You could make a static func for it and call it a day :)

permalink
report
parent
reply

@mrsgreenpotato what case do you have in mind where an export is not flexible enough? Also, do you return a Variant or generic Node(2D/3D) of a function like that or are you working with Generics then? So you do: GetNode<Bumpable>() for example?

permalink
report
parent
reply
3 points

Nice! I like it!

permalink
report
reply
3 points

Thank you! I just started on a new project and it felt nice to remind myself what worked well in the previous one.

permalink
report
parent
reply
2 points

Thanks duder

permalink
report
reply

Godot

!godot@programming.dev

Create post

Welcome to the programming.dev Godot community!

This is a place where you can discuss about anything relating to the Godot game engine. Feel free to ask questions, post tutorials, show off your godot game, etc.

Make sure to follow the Godot CoC while chatting

We have a matrix room that can be used for chatting with other members of the community here

Links

Other Communities

Rules

  • Posts need to be in english
  • Posts with explicit content must be tagged with nsfw
  • We do not condone harassment inside the community as well as trolling or equivalent behaviour
  • Do not post illegal materials or post things encouraging actions such as pirating games

We have a four strike system in this community where you get warned the first time you break a rule, then given a week ban, then given a year ban, then a permanent ban. Certain actions may bypass this and go straight to permanent ban if severe enough and done with malicious intent

Wormhole

!roguelikedev@programming.dev

Credits

  • The icon is a modified version of the official godot engine logo (changing the colors to a gradient and black background)
  • The banner is from Godot Design

Community stats

  • 808

    Monthly active users

  • 702

    Posts

  • 2.8K

    Comments