-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
'version' keyword in GDScript for conditional compilation at build time #26649
Comments
Was also proposed/discussed in the now-closed #6340. |
I didn't spot that one when searching. Not much point keeping this open then so closing. |
Reopening. Note the other issue is closed; there can still be merit in discussing this one. |
So... that's basically a specialized #ifdef. Conditional script parsing is a recurring theme, notably in #26177 as well. |
Perhaps something like D's static if would be more appropriate then. It would allow of more complex conditions such as
vs
but
I think that As for checking the engine version that could be added to
In comparison to #ifdef and #ifndef an important difference between the C preprocessor and D’s Now that I think about it there would need to be warnings for when code outside of a
|
After some thinking I realised that this could work by skipping unused branches when building the AST. This would make |
I would prefer a shorter syntax, for example: $$debug:
print("debug")
$$mobile:
print("mobile") |
@dalexeev In GDScript, we tend to favor easy-to-read constructs. Code is read way more often than it is written 🙂 |
@Calinou There is already a short syntax for Also easiness is a relative concept: #!ifdef debug
func _ready():
# ...
#!endif In any case, the functionality is primary, not the name. ADD. @Calinou Which is easier to read: |
At the very least, it would be nice to give up quotes and brackets. And it is advisable to choose a shorter keyword.
|
Nim uses the |
I think that we should focus on clarity rather than verbosity. Personally, I think that
or
|
Actually, it's used to create NodePaths 🙂 |
You learn something new everyday.
|
@matthew1006 We should prefer using fully spelled-out keywords to symbols for language features that aren't used very often. |
@Calinou I agree. I was just humouring some of the other suggestions for an alternative shorthand. |
This comment has been minimized.
This comment has been minimized.
Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine. The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker. If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance! |
Edit: The original idea was to do the optimisations when compiling GDScript to bytecode but I realised that it makes more sense to instead do conditional AST generation to allow for optimisation even when interpreted.
I would like to propose adding
version
as a keyword in GDScript to allow conditional code execution and compilation based on the OS and custom features that are available.At first glance
version (“feature”):
appears to just be syntactic sugar for
if OS.has_feature (“feature”):
and when interpreted this would be the case.
The true advantage of the version keyword would be when GDScript is compiled to gdc bytecode.
The version keyword could be detected at compile time and used to conditionally compile blocks of code depending on the feature set of the chosen export preset/template.
This would result in faster runtime speeds, smaller bytecodes and potentially faster compile times as code that is never used by your exported game will be skipped.
It also (in my opinion) results in better looking code that is arguably easier to maintain.
Here are some psudocode examples of use cases along with their current, run time counterparts:
Debug statements:
Run time
Compile time
Different code for different platforms:
Run time
Compile time
Custom features & else:
Run time
Compile time
Tool scripts:
Run time
Compile time
At compile time when a version block is found the compiler can check if the specified feature is available on the export platform (OS.has_feature) and either include the code in the block or ignore it meaning no runtime checks are required.
e.g when exporting for android
compiles as
completely omitting the iOS and web code from the compiled bytecode.
This proposal is based on the version keyword featured in D https://dlang.org/spec/version.html
The text was updated successfully, but these errors were encountered: