What is an inner class and when would we use this feature?

Is that when you define a class inside of a script? It's useful as an alternative to just using a dictionary for one example. Like instead of { "my_var": 1, "my_var2": 2 } you could define it as a class and probably see a slight performance gain?

You would have more uniform data in any case.

Not only that. It has most if not all the same functions that a regular class has. Variables, methods, signals and such. This allows you to have better structure in your code.

For example, I may have a custom particle system script ('cause the CPUParticles node isn't enough :/) and I need to store the particles. An inner class would be perfect for this. Not only can it store stuff like position and velocity, but can also have its own methods to interact with other particles.

@SIsilicon28 In that case why would you not create a new file? I thought the question was more about why you wouldn't just create a new script.

If I found myself adding too much to an inner class I'd move it to it's own file.

@Kequc said: @SIsilicon28 In that case why would you not create a new file? I thought the question was more about why you wouldn't just create a new script.

If I found myself adding too much to an inner class I'd move it to it's own file.

Yes. If. The example I gave shows an inner class with only a little functionality by itself. The main class uses that class to more easily get the job done. Having an inner class over a separate class is a decision that depends on the design of the program, or your own preference.

So basically a script inner class is just another means of encapsulation?

It's the same as creating a new file, it has all the same functionality (as far as I know)

3 years later