-
-
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
Conditional GDScript parsing #6340
Comments
I don't like the C preprocessor. It's a completely different language on top of C. If we are going to add compile time code generation I would prefer to do it the D way: https://dlang.org/pretod.html e.g: func do_something():
version Tools:
# do something that requires a tools build
else version Debug:
# do something that requires a debug build
else:
# do whatever else BTW, scripts are not compiled in debug mode (IINW). This means the preprocessor would need to be executed every time you run the project and that could be slow. |
@neikeq I just used the C preprocessor as a comparison of functionality. It doesn't have to be done the exact same way. The "D way" of handling such situations looks nicer of course. |
While i'd love some Conditional GDScript parsing, i guess the mentioned problem could be solved if the ObjectTypeDB::instance function would be exposed to GDscript.
This would make it easy to use a module class if the module is available and fall back to a GDscript version if not. For a dirty workaround to conditionally instance a class without the parser complaining if not present:
Use like: |
@SleepProgger you can already do that using the standard way of instancing (by calling if type_exists("SomeClass"):
var some_instance = SomeClass.new() |
@vnen You'd get a parser error if SomeClass does not exists. |
@SleepProgger you are correct. I've seen this pattern applied before so I assumed it would work. |
What about having (again D-way a bit) a parser-level func stuff():
const if type_exists("SomeClass"): # if not, won't really parse contents, probably
add_child(SomeClass.new())
else: # No need of const here, but would be needed for elif I think
show_unsupported_dialog("SomeClass") |
Here is a dirty trick you can use to avoid this specific problem for now: <edit>Ugh.. this was actually mentioned by the op. Any ways, I leave it if anyone is interested in an actual code example.</edit> # someclass_factory.gd
static func get_new():
return SomeClass.new()
# whatever.gd
var SomeClassFactory = null setget , get_someclass_factory
func get_someclass_factory():
# must lazy initialize in order to work
if SomeClassFactory == null:
SomeClassFactory = load("someclass_factory.gd")
return SomeClassFactory
func _ready():
if type_exists("SomeClass"):
var someclass_instance = SomeClassFactory.get_new() From 3.0 there is a better and not hackish solution if all you want is to create a new instance (what else could you want? 😛): func _ready():
if ClassDB.can_instance("SomeClass"):
var someclass_instance = ClassDB.instance("SomeClass") |
To clarify, I am not sure if |
Without knowing this was being discussed, I opened another issue. I've closed it and I'm pasting here what I proposed: Optimizing out
|
As there does not seem to be much interest in this and above improvements are now merged, as well as with 3.0 making some of this easier in other ways, vote for closing, @neikeq? |
You are right. Issue seems to be irrelevant already because of the merged improvements. |
Operating system or device - Godot version:
Godot 2.1 stable
Issue description (what happened, and what was expected):
Like we have preprocessor statements in C or other languages where we can notify the compiler that such and such code should be compiled if a condition is true or not (like am I compiling on Windows or Linux for example), it would be nice to have such conditional parsing in GDScript.
Since 2.1 we have in GDScript the nice function "type_exists(string)", which returns a boolean value if a given type exists or not in our Godot instance.
The problem is, I can't use this tool for anything other than checking just that.
If I'd put that method call in an IF statement, the code under true/false conditions is still parsed by Godot and obviously this will result in an error because I write something regarding a non-existent type.
Thankfully Godot can just ignore the script file in which I write such statements, but still I have to make special arrangements just for that (like keeping all my platform-specific or module-specific code in separate scripts).
This is of course just a single example use-case.
Or maybe there is a way to force conditional parsing of which I'm not aware of? I asked around on the IRC channels but got no answers in this regards, so I suppose that it's not possible to do such a thing currently.
The text was updated successfully, but these errors were encountered: