Hello. I am learning new knowledge about custom class by Gdscript. I want to learn if there is anyway to declare a method inside another method body instead of inside class body.

Like this:

extend Node
class_name Custom-class
var a:float =
var b:float =
func _ready()
 pass
func _custommethod()
 if a>b:
  func _newcustommethod()
  • xyz replied to this.
  • Capixo You cannot define methods inside methods. Methods are associated with classes so it only makes sense to define them in the class scope. However you can define lambdas inside other functions/methods.

    Btw. why would you need methods inside methods?

    Capixo You cannot define methods inside methods. Methods are associated with classes so it only makes sense to define them in the class scope. However you can define lambdas inside other functions/methods.

    Btw. why would you need methods inside methods?

      xyz Btw. why would you need methods inside methods?

      1. Encapsulation.
      2. It's cool.

      Jesusemora In languages that support it, nested functions are useful as 'helper functions' that are only called by a single function. e.g. say you are refactoring a function b/c it's too long or complex. You want to extract some of that functionality into some smaller functions. But if you move those smaller functions into the class, it implies that those functions could be called by any other class member function, and that's not always the intent. Declaring a nested function ensures that it can only be called by the outer function that contains it.

      You can do something similar in C++ with lambdas, but it's not as readable as nested functions (IMNSHO).

        Here's a GDScript (Godot 4) example of using a lambda to create a function inside a function:

        func _ready() -> void:
        
        	var square: Callable =\
        		func(x: int):\
        			var x_squared: int = x * x;\
        			print("%d squared=%d" % [x, x_squared])
        
        	for i: int in 10:
        		square.call(i + 1)

        The syntax can get messy when you need line continuation characters (\) and statement separators (;).

        • xyz replied to this.

          DaveTheCoder You don't actually need any ; or \.

          var f = func():
          	print("This")
          	print("is")
          	print("an")
          	print("ex")
          	print("parrot")
          f.call()

          xyz Hello. I want to learn if I can just declare custom method like "func attack" inside func _physics_process instead of call it. Don't have any practical purpose just for curious. Thank you for kind help.

            Haystack But if you move those smaller functions into the class, it implies that those functions could be called by any other class member function, and that's not always the intent. Declaring a nested function ensures that it can only be called by the outer function that contains it.

            That's sounds extremely specific. without an example, I can't think of any situation that would require or benefit from this "feature".
            I'm sorry, I don't get it. It just doesn't sound useful or even good, at least not to me. Maybe I'm wrong.

            Capixo I want to learn if I can just declare custom method like "func attack" inside func _physics_process instead of call it.

            In my post above, I defined a lambda function named "square" inside _ready.

            But why do you want to declare a method if you're not going to call it?