-
-
Notifications
You must be signed in to change notification settings - Fork 99
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 object initializers to GDScript #3076
Comments
See also #1513. |
Would it make more sense to have the initializer list be a post-colon indented list of void-returning expressions? var custom_multiplayer = MultiplayerAPI.new():
peer = _peer,
root_node = self Only bad thing is that it would be incompatible with scenarios where the post-creation-initialized expression is itself part of some other block-opening statement. But I'm not sure if that should be supported anyway. @export var card_display: PackedScene
@export var card_name: String
func create_card():
# Could work? Only performs post-init if .instance() returns non-null value.
if card_display.instance() { card_data.name = card_name }:
pass
# Will NOT work (can't tell what : is for till later)
if card_display.instance():
card_data.name = card_name:
pass Would also be interesting if it supported other "builder"-like APIs seamlessly by supporting method calls as well. var list_of_nums := PackedIntArray():
append(1),
append(2),
append(3)
print(list_of_nums) # prints 1, then 2, then 3 Edit: Ah, but if you do a colon, then it might conflict with the new setter/getter syntax anytime you used it on a member variable as opposed to a local variable. Unless you just make the assumption that you have to pick either |
The proposal @Calinou linked (#1513) looks like a superset of this one, functionally at least. I would prefer that option, because it does not only allow for initialising variables, but for even more comprehensive initialisation, e.g. sanity checks and passing on values to the init function of a parent class. |
This is not so important when there is autocompletion. |
See also #1935. Scripts with scene bindings would have the language implementation defer to the scene during instantiation, but they could also potentially have a custom post-instantiation initialization function that is called. |
Describe the project you are working on
A Multiplayer Card Game (akin to Hearthstone, Magic The Gathering: Arena etc)
Describe the problem or limitation you are having in your project
When instantiating classes I don't own that require properties to be set, such as the peer and root node of a custom MultiplayerAPI I have to do so individually.
A similar issue also occurs when I want to pass data to a scene that I instance (such as card data to a card scene).
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Add Object Initializers (similar to those that already exist in C#) so that users can set named properties immediately after object construction without the need for an explicit and or accessible constructor.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
If the object that is to be created is followed by an unassigned dictionary, it consumes it with the keys referencing its own properties.
The internal code would perform the equivalent of this.
Alternatively there is the With Syntax mentioned back in 2016 in which case the examples would be something like this
If this enhancement will not be used often, can it be worked around with a few lines of script?
I believe this would be used quite often with scene instances.
Is there a reason why this should be core and not an add-on in the asset library?
It affects core GDScript Syntax.
The text was updated successfully, but these errors were encountered: