Godot Version
Godot 4.4 custom build (with no modifications to built in classes)
Question
I’m working with NodePath in one of my projects and I’ve come across a good use case for the Slice method. I’m using C# for this part of the project I’m working on. I tried to call NodePath’s slice method in C#, and I discovered that not only does my IDE not recognize that the method exists, but the project also won’t build when I try to call Slice. The build error says that NodePath does not contain a definition for Slice.
Furthermore, I looked at the C# source code for NodePath and indeed, I could find no evidence that Slice is defined on the C# version of NodePath. My assumption was that the C# version can’t be called Slice because it would conflict with a built in C# method, but I can’t find any evidence that there’s an equivalent method for the C# version.
So my question is, am I missing something, or is the Slice method just missing from the C# version? If it is missing, is there a reason that it is missing or is it just an oversight?
Based on my reading of the source code, I’m going to make a preliminary guess and say that this method is indeed simply missing. If anyone has any insight into why it might be missing, I’d love to hear it.
By the time you typed all this, you could have just implemented your own ;). Here’s the source from node_path.cpp:
NodePath NodePath::slice(int p_begin, int p_end) const {
const int name_count = get_name_count();
const int total_count = get_total_name_count();
int begin = CLAMP(p_begin, -total_count, total_count);
if (begin < 0) {
begin += total_count;
}
int end = CLAMP(p_end, -total_count, total_count);
if (end < 0) {
end += total_count;
}
const int sub_begin = MAX(begin - name_count, 0);
const int sub_end = MAX(end - name_count, 0);
const Vector<StringName> names = get_names().slice(begin, end);
const Vector<StringName> sub_names = get_subnames().slice(sub_begin, sub_end);
const bool absolute = is_absolute() && (begin == 0);
return NodePath(names, sub_names, absolute);
}
Yes, I’m well aware of the fact that it’s not hard to implement, but the documentation specifies that the method exists and I’d rather not be forced to recreate the wheel for basic stuff like this.
I wanted to know whether anybody had insight into this and to see whether I was just missing something or if there was a technical reason the method is missing. My question isn’t “How do I accomplish this?”, but rather “Why is this method missing?”.
And while this implementation is trivial in the C++ source, based on the NodePath API exposed to C#, it does not look like implementing it in C# is going to be as straightforward.
Yeah, it’s strange that it’s missing.
Why not? It’s trivial. It does several calls to other NodePath methods and that’s it. Those are there. The only “advanced” thing is that is’s recursive.
C++ can use the get_names() method to get all the parts of the node path as a vector, but there is no equivalent to get_names() in the C# API. So, I’m going to have to get the full string representation and break it apart into a list manually; not hard, but it does add overhead that would be unnecessary if Slice was present in C#.
Yeah, it’s annoying but what can you do. Either implement or pester devs to do it and wait for it.
Well I appreciate the help.
I ended up picking the third option that is also the most complicated. I ended up reading through the source until I figured out how the C# API interacts with the engine and then added Slice to the engine and the C# API. You can view my pull request to solve the issue if you like. Since I use a custom build, I can already use my implementation of Slice even though my pull request isn’t merged as of the writing of this post.
So for future reference, the answer to the question of why Slice was missing seems to be simply that there was an oversight and Slice wasn’t added originally when it should have been.
1 Like