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

Preprocessor defines for C#/Mono #27227

Closed
wfowler1 opened this issue Mar 19, 2019 · 9 comments · Fixed by #28786
Closed

Preprocessor defines for C#/Mono #27227

wfowler1 opened this issue Mar 19, 2019 · 9 comments · Fixed by #28786

Comments

@wfowler1
Copy link

The what: It would be great if Godot could automatically add a preprocessor define to the project for Mono. I believe Unity has done this since version 2.6.0.

The why: It's very convenient to be able to use the same source code in different places. I have built and still maintain a C# library for reading BSP files (from the Source engine, for example), and I've been able to make it work in .NET standalone projects as well as Unity by using Unity's C# preprocessor defines. I'd love to add support for Godot the same way. The project is at https://github.com/wfowler1/LibBSP

The how: It would probably be sufficient to simply add "GODOT" to the DefineConstants section of each build configuration in the CSPROJ. Then in C# code we could simply wrap Godot-specific code in #if GODOT and ENDIF.

@wfowler1
Copy link
Author

Unity uses this to allow conditional compiling of code based on the version of Unity being used. This might be an ok thing to have, but I'm more interested in compiling for the engine as a whole rather than any specific version, which is actually something Unity lacks.

@aaronfranke
Copy link
Member

I believe Unity has done this since version 2.6.0.

Unity has had this feature since Unity 5.3 (released in 2015).

@Akrucious
Copy link

Disappointing to see that this has been around in C# for over 25 years, Unity has had it implemented for more than a decade, but GoDot still doesn't have any plans to implement this in the foreseeable future. Quite a huge oversight, especially when you consider cross-engine library support, client versus server environments, and any other use case.

@aaronfranke
Copy link
Member

@Akrucious It is implemented in Godot.

@Akrucious
Copy link

@Akrucious It is implemented in Godot.

The documentation says you can't define them.

@akien-mga
Copy link
Member

akien-mga commented Feb 2, 2025

@Akrucious It is implemented in Godot.

The documentation says you can't define them.

Where does it say that you can't define them (genuine question, I don't find that statement)?

I'm pretty sure you can add any constant via DefineConstants in your csproj file:

This is a standard .NET feature and not specific to Godot. We could document it in the linked page though.

And this closed issue was resolved by adding some automatically defined constants specific to Godot that you don't need to define manually, as documented in the linked page.

@Akrucious
Copy link

@Akrucious It is implemented in Godot.

The documentation says you can't define them.

Where does it say that you can't define them (genuine question, I don't find that statement)?

I'm pretty sure you can add any constant via DefineConstants in your csproj file:

* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives

* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/language#defineconstants

This is a standard .NET feature and not specific to Godot. We could document it in the linked page though.

And this closed issue was resolved by adding some automatically defined constants specific to Godot that you don't need to define manually, as documented in the linked page.

Not sure if you realize this but manually specifying them overrides the GoDot defined ones, bricking the engine.

@raulsntos
Copy link
Member

raulsntos commented Feb 2, 2025

Make sure you are appending to DefineConstants and not overriding the value. This is how it should look like:

<DefineConstants>$(DefineConstants);MY_CONSTANT</DefineConstant>

@Akrucious
Copy link

Make sure you are appending to DefineConstants and not overriding the value. This is how it should look like:

$(DefineConstants);MY_CONSTANT

That actually works, thank-you raulsntos, you're awesome!

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

Successfully merging a pull request may close this issue.

7 participants