Hi All!

Is there a way to declare funcs within funcs? Mostly for organizing code in a tree like structure and codefolding. eg.:

func initall():

    func loadallpngs():

        func loadapng(id:int):
            var s=TextureRect.new()
            s.texture=load(resnames[i])
            loadedpngs.append(s)

        for i in range(10):
            loadapng(i)

    func initmap():
    ...
    func initvars():
    ...

  loadallpngs()
  initmap()
  initvars()

//end of initall()

etc.

I always get: Error parsing expression, misplaced: func

? Thanks for all your answers.

Edit: Thanks for the tip with Edit

Basically, I would like to create functions in the local scope of another function. In the example above I would just call initall() from somewhere in the program and it would do all the things inside of it (loadallpngs, initmap, initvars) by itself. I could then fold initall(), making my program look neat and easy to browse, like a tree of functions.

C# calls them local functions https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/local-functions

here is a c++ example https://notesformsc.org/c-local-functions/

Edit: local function in title name now

Wow. I Really should have used preview didnt I. Sorry, just imagine everything is properly indented.

Sorry, what do you want to do? I don't understand at all.

Also, there is edit button. Use it!

I accident click this link and found thing is edit.

About your question, seem to be more like class and local space than local function (I even though that you mean some kind that Javascript do)

class PrintEngine:
	class SubClass extends Reference: # Can extend node like on top of every script
		var message = "I am sub class message"
		func get_message():
			return message # Get message from local class
	var class_message
	var print_count
	func _init(count):
		print_count = count
		class_message = SubClass.new()
	func print_this():
		for _ in range(print_count):
			print(class_message.get_message())
			#print(class_message.message)
func _ready():
	var print_message = PrintEngine.new(5)
	print_message.print_this() # "I am sub class message" * 5

Thank You Sent44!

Your answer did lead to my solution.

I have learned, that there are no functions local to functions, they always have to be inside a class (which can create a new scope)

So I kinda can do what I wanted by declaring classes of classes and put functions inside of them. I just have to remember to always instantiate them.

Not as elegant as I hoped, but usable.

Thank You very much again, Sent44, have a wonderful day!

@"Ave Vipogad" Declare function as static and you don't need to instantiate(but also the function cannot access to class variable)

class Print:
   static func hello():
      print("world")

func _ready():
   Print.hello()