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

Add _script_init() virtual method in GDScript #1673

Closed
me2beats opened this issue Oct 15, 2020 · 3 comments
Closed

Add _script_init() virtual method in GDScript #1673

me2beats opened this issue Oct 15, 2020 · 3 comments

Comments

@me2beats
Copy link

me2beats commented Oct 15, 2020

Describe the project you are working on:
GDScript plugins

Describe the problem or limitation you are having in your project:
I noticed that I often would like to have a method that is called only once before any _init() called (and usually after all the methods and script variables declared by the user in the script code have been created).

  • This would be useful in order to do some initialization before creating any object. This initialization could be based on script properties, constants, and methods (declared in the script by user).

  • This could also be used, for example, for setting default values ​​for variables in case the default value of a variable requires some preliminary calculations.
    Then there is no need to calculate this value elsewhere, or (even worse) whenever an object is created in _init()

  • This could possibly allow setting the constants values (of any type, such as strings, integers, etc.) in this method, which cannot be done from other methods, such as _init

  • It would be convenient when doing some complex preliminary initialization exactly where it should be - in the script code, in a separate method.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
Adding _script_init() virtual method would overcome the problem

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

var number: int
const text = ''

func _script_init(script:GDScript)->void:
    #will be called only once when current script is created (loaded?)
    if 'my_signal' in script.get_script_signal_list(): #<--pseudocode
        number = 1 #setting default var
        text = 'hello' #maybe even setting a constant value?

That is in _script_init(), we do not yet know the object that will be created using script.new(), but we already know the script object itself (passed as script arg) and we can work with it, for example, get a list of script methods, properties and signals declared by the user

To some extent, _script_init() is similar to Python __new__(), but no need for the kind of flexibility that py __new__() has.
So maybe it could be called _script_new() instead.

If this enhancement will not be used often, can it be worked around with a few lines of script?:
There are work-rounds, but they all at least obfuscate the code.

Is there a reason why this should be core and not an add-on in the asset library?:
[gdscript feature]

@vnen
Copy link
Member

vnen commented Mar 2, 2021

This doesn't seem really feasible. Setting default values, maybe, but would require the compiler to have a special path just for the _script_init() function to make that happen, otherwise it will try to set the values on an instance that don't exist exist.

As for constant values, those are optimized at compile time. So changing this in a function would requiring pretty much recompiling the script after this is called. This would be quite complex to pull off, if even possible.

So this to me looks like a lot of work for not much gain. I would like to see some actual use cases where this would be a preferable solution.

@me2beats
Copy link
Author

me2beats commented Mar 2, 2021

Yea, when I wrote this, I didn't think it would take a lot of work, but now I tend to think so.
Also I don't seem to know enough about gdscript to solve some problems, right now I don't see much need for this, so I think it can be closed.

@me2beats me2beats closed this as completed Mar 2, 2021
@dalexeev
Copy link
Member

This is implemented to some extent as static variables and the _static_init() method.

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

No branches or pull requests

4 participants