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

GDScript suggestion: The "With" Construction from GMS #18345

Closed
NoodleSushi opened this issue Apr 22, 2018 · 8 comments
Closed

GDScript suggestion: The "With" Construction from GMS #18345

NoodleSushi opened this issue Apr 22, 2018 · 8 comments

Comments

@NoodleSushi
Copy link

It executes the given statement to a given node with the "other" keyword, for example:

extends Node2D
var random_color = Color()
var random_position = Vector2()

func _process(dt):
   var window_size = OS.get_window_size()
   #set random color
   random_color = Color(randf(),randf(),randf())
   random_position = Vector2(rand_range(window_size.x),rand_range(window_size.y))
   #The expression/node is a Polygon2D ($rectangle)
   with ($rectangle):
      set_color(other.random_color)
      set_position(other.random_position)
@vnen
Copy link
Member

vnen commented Apr 22, 2018

I don't really see the point of it, only makes the code harder to read because you have to switch context in your head. What's wrong with regular indexing?

var rect = $rectangle
rect.color = random_color
rect.position = random_position

@manokara
Copy link
Contributor

manokara commented Apr 22, 2018

The with keyword is a solution to a problem Godot doesn't have. It performs a context switch, so you could do things like event_perform() and use scripts to change instance vars. We've got classes, objects have their own functions and you can initialize an instance with arbitrary values by overriding the _new() function.

In your example with would be just syntax sugar for vnen's code. Actually, you could even drop the rect reference and use $rectangle directly, saving an extra line.

@nobuyukinyuu
Copy link
Contributor

nobuyukinyuu commented Apr 22, 2018

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/with-end-with-statement

For some reason I feel like I remember seeing some kinda syntax either in gdscript or python or something similar where you can initialize several fields of a variable at once using with-like syntax, except it took a dictionary as an initializer, something like this:

func with(target, fields):  #fields is Dict of (String, Variant)
  for key in fields.keys:
    var set_property = target.set(key, fields[key])

    if set_property == false:  #Maybe a method
      if target.has_method(key):
        target_func = funcref(target, key)
        target_func.call_func(fields[key])

@raymoo
Copy link
Contributor

raymoo commented Apr 24, 2018

@nobuyukinyuu with statements in Python let you initialize something that needs cleanup and it will automatically run the cleanup if there is an exception within the with statement body (or if the body exits normally). It's like RAII in C++ for variables on the stack.

@eon-s
Copy link
Contributor

eon-s commented Apr 25, 2018

Looks like duplicate of #1736, and even being old, has better syntax and examples.
Edit: no, is not duplicate of that one, sorry.

Personally, I find this confusing and error prone related to indentation.

@nobuyukinyuu
Copy link
Contributor

nobuyukinyuu commented Apr 25, 2018

#1736 is describing something like VB's Using blocks. I'm not so fond of the utility of that one – especially if Godot ends up implementing an Elvis operator or null-coalescence operator – but unlike that request, I don't find the syntax for this one confusing at all, especially if existing dictionary syntax can be used (a curly block can simply be proceeded by with x:.)

@coppolaemilio
Copy link
Member

Coming from GM:S, I don't think GDScript needs this feature. For those of you who want to know more about it, here you have the official docs on it: https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml%20language%20overview/401_18_with.html

But again, I don't think this should be implemented.

@akien-mga
Copy link
Member

As described in #1736 (comment), and according to the various comments here mentioning that the feature from GMS is not particularly relevant/needed in GDScript, I'll close this.

This issue was closed.
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

8 participants