You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
varnumber: intconsttext=''func_script_init(script:GDScript)->void:
#will be called only once when current script is created (loaded?)if'my_signal'inscript.get_script_signal_list(): #<--pseudocodenumber=1#setting default vartext='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]
The text was updated successfully, but these errors were encountered:
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.
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.
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 problemDescribe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
That is in
_script_init()
, we do not yet know the object that will be created usingscript.new()
, but we already know the script object itself (passed asscript
arg) and we can work with it, for example, get a list of script methods, properties and signals declared by the userTo 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]
The text was updated successfully, but these errors were encountered: