What is the correct way to communicate variables through nested functions in Godot?

Viewed 392
  • I have functions a, b, and c.
  • a calls b in 100 times.
  • b calls c 100 times.
  • Therefore if a is called once, c is called 10,000 times.
  • c requires many variables from a's scope.

Should I:

  1. Pass the variables through b and c as parameters.
  2. Define b and c in a's scope so they can access a's variables.
  3. Define b in a's scope and define c in b's scope.
  4. Make the variables member variables in the file that a is defined it, and change them from inside a.
  5. Do some other thing.

I don't want to do 1 because then b and c will have many parameters. I don't want to do 2 or 3 because then b and c will be unnecessarily re-defined every time a is called. I don't want to do 4 because having member variables that are only used in a single place in a lower scope seems bad. Is there another option? Are any of my reasons not to do 1-4 wrong?

1 Answers

I suggest to do 4 (Make the variables member variables in the file that a is defined it, and change them from inside a).

I don't want to do 4 because having member variables that are only used in a single place in a lower scope seems bad.

Make a new class for it.


If you start with 1 (Pass the variables through b and c as parameters).

You would have to solve the many parameters problem you describe:

I don't want to do 1 because then b and c will have many parameters

To do so, you can define a new class to hold them, so you can pass a single parameter, which is a object of that new class. Once you have a new class, and given it is only used from the function a... Then it makes sense to make the function a into a member of the new class… If you do the same for the other functions, then you don't need to pass parameters at all, and you effectively have solution 4.


I'm not sure about 2 and 3. Lambda functions are an upcoming feature in Godot 4.0 (see), which is not out yet. Are you using a custom build with that or… What?

Related