webrtc-native
webrtc-native copied to clipboard
Checklist for adding a new class and method?! + How are hashes generated in extension_api.json and how to add for a new class + methods.
Godot version
4.3 stable
Plugin version
4.3
System information
Windows 10, 64 bit
Issue description
I have been wanting to add a new class to the WebRTC src stack. I wanted to expose a new class + its functionality and I did it by doing the following steps but godot still gives an error that the methods or class I created does not exist.
I created the following in the relevant files and folders
src:
WebRTCLibDataChannel.cpp WebRTCLibDataChannel.hpp WebRTCLibPeerConnection.cpp WebRTCLibPeerConnection.hpp NewClass.cpp NewClass.hpp
src/net:
WebRTCDataChannelNative.cpp WebRTCDataChannelNative.hpp WebRTCPeerConnectionNative.cpp WebRTCPeerConnectionNative.hpp NewClassNative.cpp NewClassNative.hpp
I also updated the build profile - the other classes (PeerConnection and DataChannel were included with the GDNative + Extension versions, I added the Native version of NewClass + extension. The build_profile-json looks like
{
"enabled_classes": [
"WebRTCDataChannelExtension",
"WebRTCPeerConnectionExtension",
"NewClassExtension",
"WebRTCDataChannelGDNative",
"WebRTCPeerConnectionGDNative",
"NewClassNative",
"NativeScript",
"GDNativeLibrary",
"Window"
]
}
Moreover, I also added the NewClass file to the SConstruct in sources.append.
The Issue: I realise that the godot-cpp/gen/ has files generated by the binding_generator.py, which picks up json class and method definitions from the extension_api.json. The issue is that the main src files include gen files in their headers and right now I can not do that for the newclass classes as the extension_api.json does not have hashes for the new class and methods.
After a little study I realised that the hashes for a certain return type of a function are the same for e.g. all int return type methods seem to have the same hash. While I realise I can somehow get away with the hashes that already fulfill a few of my method return types, but for custom return types (like when a NewClass instance is returned for a function I created called create_newclass and create_from_newclass) I would need a proper hash to represent that for godot's understanding.
Now, I want to add my own custom class and hashes so my questions map down to:
- How do I create custom classes and methods?
- How do I add the hashes relevant to it that godot can read appropriately?
- How are hashes even generated?
- Am I missing something in the above steps in order to add my custom classes and functionality?
I will add more updates as I go.
Steps to reproduce
Minimal reproduction project
No response
I think there's a bit of confusion, this is likely due to the fact that the plugin, unlike most extensions, implements an interface that is directly exposed by Godot.
The build_profile.json, and the extension API (and hashes) comes from Godot itself, and when developing extensions you don't normally need to care about that.
How do I create custom classes and methods?
To add a new class, you just need to define them in the extension, and register them via godot::ClassDB::register_class<ClassName>(); (see src/init_gdextension.cpp).
To register methods of that classes, you must implement the static _bind_methods method and call ClassDB::register_method (see the example in the godot-cpp test).
Methods are not bound in this plugin because all the methods are in fact exposed directly by Godot, and as said the plugin only implements the interface. For testing purposes though, you can add the extra methods to WebRTCLibPeerConnection, register them, and they will work.
For proper integration in the long run, we would probably want to implemented them in the Godot exposed interface (see WebRTCPeerConnection and WebRTCPeerConnectionExtension). Again, this is a special case for this plugin, and not the usual GDExtension flow, but in general, the hashes are only relevant for Godot-exposed classes (and are auto generated by the Godot binary), so you don't need to worry about them.
I hope this helps, let me know if something is unclear.
@Faless thank you for letting me know! This was great help!
As you mentioned earlier, I've done the following steps:
- Add src and src/net files
- Registered the new class in src/init_gdextension.cpp via godot::ClassDB::register_class<ClassName>();
- Added relevant create_newclass method in WebRTCLibPeerConnection, registered them via bind_methods
void WebRTCLibPeerConnection::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_newclass", "mid", "t_channel_config"), &WebRTCLibPeerConnection::_create_newclass);
}
- Added the relevant methods in NewClass, registered them via bind_methods
void NewClass::_bind_methods() {
ClassDB::bind_method(D_METHOD("method1", "param1"), &NewClass::_method1);
ClassDB::bind_method(D_METHOD("method2", "param2"), &NewClass::_method2);
ClassDB::bind_method(D_METHOD("method3", "param3"), &NewClass::_method3);
}
When I build the extension and add it then I don't see any errors but when I try to add the NewClass it says its not existent. Secondly, I don't see how the register_methods function is used or needs to be called as even in the example, bind_methods is adding the methods. I've tried to run register_methods and it says that it is not a part of godot.
Nevertheless, upon implementing the above, I couldn't see the create_newclass method autocomplete with WebRTCPeerConnection as well.
Can you point me in the right direction here?
Can you point me in the right direction here?
I'm afraid I would need to see the code, because according to the step you described it should work, so I think there may be an error somewhere.
Closing, as it's been a few months and there's no reply from the OP.