Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow binding of constants #636

Open
cbackas42 opened this issue Dec 23, 2024 · 2 comments
Open

Allow binding of constants #636

cbackas42 opened this issue Dec 23, 2024 · 2 comments

Comments

@cbackas42
Copy link
Contributor

I'm wrapping a C library (FMOD) which contains a ton of constants. Since a lot of the @callable surface area I'm exposing to Godot take arguments which correspond to these constants, it would be ideal to be able to export these constants to Godot. The GDExtension API has a way to do this via bind_integer_constant() and friends, but we can't access them conveniently from Swift

Describe the solution you'd like
Assuming it's possible, it might be nice to extend the @export macro to handle constants as well. But I'm not tied to a particular syntax.

Describe alternatives you've considered
Re-defining these constants directly in a GD script, but that's messy and I prefer for the "source of truth" for the values to be the original headers. (Also I like having pure Swift in my project)

@elijah-semyonov
Copy link
Contributor

elijah-semyonov commented Jan 2, 2025

Swift macros don't have access to anything but AST, so C constants won't be visible for it to generate GDE API boilerplate.
Or did I misunderstand what you mean?

Can you provide an example of Swift code you want to write and what you expect to see on Godot side?

@cbackas42
Copy link
Contributor Author

Oh, I don't want it to see and literally export the #defines for me. I expect I'd have to register them myself someplace as a Swift symbol.
For instance, I have the FMOD constant:
#define FMOD_INIT_3D_RIGHTHANDED 0x00000004
This is already brought into Swift via the Bridging header, but I need to turn around and export the value to Godot so, eg, GDScript could access it for instance.
In C++ it's just:
BIND_CONSTANT(FMOD_INIT_3D_RIGHTHANDED)

Which is defined in class_db.hpp as:
#define BIND_CONSTANT(m_constant) \ ::godot::ClassDB::bind_integer_constant(get_class_static(), "", #m_constant, m_constant);

Since these are global things, maybe it makes sense to do it in the initHook? Something like:
#bindGodotConstant(,"ConstantName")
Though, looks like enums and bitfields are supported too thanks to Miguel here:
godotengine/godot-proposals#2242

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants