this post was submitted on 16 Oct 2024
75 points (100.0% liked)

Godot

5866 readers
4 users here now

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

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

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[–] mrsgreenpotato@discuss.tchncs.de 3 points 3 weeks ago* (last edited 3 weeks ago) (9 children)

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.

[–] Paragrimm@mastodon.gamedev.place 1 points 3 weeks ago (1 children)

@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.

[–] mrsgreenpotato@discuss.tchncs.de 1 points 3 weeks ago (1 children)

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 :)

[–] Paragrimm@mastodon.gamedev.place 0 points 3 weeks ago (1 children)

@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?

[–] mrsgreenpotato@discuss.tchncs.de 1 points 3 weeks ago (1 children)

We don't have generics in gdscript unfortunately, there's a whole thread discussing about it here. You'd need to either have this piece of code in every place you want to retrieve the component, or you could have a static function in the component class that takes a node as a parameter and checks if it has children nodes of the correct type. Then it can return the correct type node as well and should be fully type safe. But you need to have this static function on each and every component class, which I think you can't overcome without generics.

[–] Paragrimm@mastodon.gamedev.place 0 points 3 weeks ago (1 children)

@mrsgreenpotato ahh ok, could you extend from a base node type and add this function respectively?

We've defined "entities" in our project and each Entity-Type has a script for some basic functionality (to define that it IS an entity etc.).

In our project a CharacterBody3D entity is an "Actor", while a RigidBody3D is an "Item" for example. And we're basically only working with these and define components for them that we can retrieve via a Dictionary<StringName, Resource>

[–] Paragrimm@mastodon.gamedev.place 0 points 3 weeks ago (1 children)

@mrsgreenpotato (2/2) and in our case, we've put every piece of data into resources where we export the values we need. If we need a node in a scene, we just export it and it's mostly a direct childNode of the scene. All in all we have a system where the example zombie consists of the following components: Ai, Model, Stats, Info, Sfx, Chemistry. These have their own scenes that get added to the entityNode. In the end there's no need for such a helper function basically :D (at least yet)

[–] mrsgreenpotato@discuss.tchncs.de 1 points 3 weeks ago* (last edited 3 weeks ago)

Yes, as long as you know what components are inside the Zombie scene, then you can export it. But the idea of being more flexible is that you shouldn't need to update the Zombie class when you add a new component to it. E.g. You want the zombie to be "Bumpable" now (for some reason :)), then you should be able to just add the Bumpable node to your Zombie scene and that's it. With your approach, you'd need to also reference and export it in the Zombie class first.

load more comments (7 replies)