dalexeev / gdscript-preprocessor

An export plugin for stripping comments and "conditional compilation" of GDScript.
MIT License
78 stars 1 forks source link

code not being stripped #7

Closed ryanzec closed 2 months ago

ryanzec commented 2 months ago

So I have the follow code:

func _ready() ->void:
    #~if OS.is_debug_build()
    var c = 0
    var b = 0;
    var is_debug = OS.is_debug_build()
    var t1 = Time.get_ticks_usec()

    while c < 1000000:
        if is_debug:
            b += 1
        else:
            b -= 1
        c += 1

    print(Time.get_ticks_usec() - t1)
    print(b)
    #~endif
    SignalManager.main_hud_action_selected.connect(on_main_hud_action_selected)
    SignalManager.main_hud_consumable_select.connect(on_main_hud_consumable_select)

but when I export the project with the Export With Debug unchecked and unpack the pck file, the code looks like this:

func _ready() ->void:
    var c = 0
    var b = 0;
    var is_debug = OS.is_debug_build()
    var t1 = Time.get_ticks_usec()
    while c < 1000000:
        if is_debug:
            b += 1
        else:
            b -= 1
        c += 1
    print(Time.get_ticks_usec() - t1)
    print(b)
    SignalManager.main_hud_action_selected.connect(on_main_hud_action_selected)
    SignalManager.main_hud_consumable_select.connect(on_main_hud_consumable_select)

I am not sure why only comments are bring stripped out. I would expect the conditional block to be stripped. I did not change any of the setting in the export dialog and I restarted the editor after enabling the plugin to make sure that was good.

I also tried using #~if OS.has_feature("editor") (which I assume should be stripped regardless of it being in debug mode) and got the same result.

dalexeev commented 2 months ago

Hello! Thank you for your interest.

func _ready() ->void:
  #~if OS.is_debug_build()
  var c = 0

Currently, directives must start at the beginning of the line, like the C/C++ preprocessor. I can add indentation support if you wish.

func _ready() -> void:
#~if OS.is_debug_build()
    var c = 0

Note that the plugin also tries to resolve regular if/elif/else statements (albeit less reliably). I supposed that directives would be used primarily outside function bodies, since GDScript does not support if/elif/else outside functions.

func _ready() -> void:
    if OS.is_debug_build():
        var c = 0
ryanzec commented 2 months ago

Thanks, it does seem like both your suggestions work in this case. I think I only tried the directive version as initially I could not get it working with a normal if (but that was because I was unaware on how to export a project without debug which I have since figured out).

I am relatively indifferent to the formatting of the preprocessor, I only favor allow leading whitespace as it just make the code slightly cleaner but I will close this since the expected patterns do work and you can decide if it is worth allowing leading spacing since it is not really that big of a deal.

Thank you for building and maintaining this plugin.