godotengine/godot

function object(functor) and lambda expressions(anonymous functions) in GDscript

Closed this issue · 10 comments

Do you see these modules:

https://github.com/dbsGen/my_gdscript


example codes from repos:

func _ready():

    # Easier connection
    self.connect("renamed", on_renamed)

    var hello = "hello"
    # lambda expressions
    var lambda_func = func():
        print(hello)

    # or 
    func lambda_func2():
        print(hello)

    # lambda_func is a function object
    lambda_func.apply()
    lambda_func2.apply()

    # A native function object
    var native_func = self.set_name
    native_func.apply("new_name")


func on_renamed():
  print(get_name())

lambda functions Pros:

Only one place function
Code modularization
Self-contained
Reduce repitition
Increasing the expressive power of the language
Easy and optimized implementation of behaviour tree(see crytek softcode)
Parallelization(with adoption in game scripting)

and Cons:

Confusing(eventually)
Functional thinking for starters and learning curve



and while loop improvements for glsl:
https://github.com/sheepandshepherd/godot-glsl-while

Care to be more specific? The issue title doesn't say a thing, and the content of the first comment hardly more. If you want something to be implemented, you have to take part into doing some of the leg work, just throwing out links is not really helpful.

Excuse me because of bad title!
I wrote feature request not forced feature request.
Every user can take part in a shared project(Documentation, Examples, Propagation, bug report, bug fix, and development). Please give me a cleaner api doc to start development. I'm working on two free software cookbook and I started godot since two months ago. I'm happy if I can help you but It is hard to find a global map from less documented code so this is difficult to take part in engine documentation, too. because only developers of engine can understand their written code and it's optimization and fixing curve during time.

@alabd14313 these improvements looks nice! Lamba like functions would be cool!
Don't get @akien-mga comment so badly, it just would be a little easier for main devs to keep up with all this stuff happening here on github if more requests would have better description, like you could also write couple sentences about these repos in the first post.

Edit: OP updated the first comment as requested.

Sorry if my tone sounded harsh, as @kubecz3k said, I just meant that for our main devs to keep track of everything happening in the high number of GH issues, we need precise issue titles and descriptions. So I would ask you to edit the first comment to add more content about what you are proposing/asking for: What are those GDScript improvements? Did you test them and use them, or did you simply stumble upon them and thought it would be nice to see them upstreamed? How do they impact performance?

So basically, any contextual info that could save some time for the devs who browse through issues and have only a handful of seconds to give to each until they find one they really want to settle on and work on.

@alabd14313 is this already working? (btw i can't find a difference beatween lambda_func2 and any other function, why is it a lambda?)
Meanwhile, saving functions to a var seems cool, js does it and there're plenty of people asking for js.

vnen commented

I like the lambda functions. Godot has some passing around of functions, so to make them first-class would be great. As one note, the first argument of lambda.apply() should be the self object, so self references inside the lambda are properly resolved (or a lambda.bind(object) should be provided).

@MarianoGnu the thing about lambda_func2() is that it's defined inside another function. I believe @alabd14313 should better format the block so that's more visible, as it took me a while to understand.

vnen commented

BTW, while this is being worked on, it seems a nice opportunity to fix #2570.

Doesn't gdscript's "funcref" do something like this? (I may be wrong, I haven't had an excuse to use it yet...)

Would be also nice to connect a signal to a func var, then you can change the conection behabiour really easy without disconecting/conecting

vnen commented

It's on the plans to have first-class functions and lambdas in 4.0. In fact the first is pretty much working in my fork already, and I'll pull request soon.