GDScript inner class?

mr_moonmr_moon Posts: 9Member

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

Best Answers

  • KequcKequc Posts: 88
    edited March 25 Accepted Answer

    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.

  • SIsilicon28SIsilicon28 Posts: 286
    Accepted Answer

    @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.

Answers

  • KequcKequc Posts: 88Member
    edited March 25 Accepted Answer

    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.

  • SIsilicon28SIsilicon28 Posts: 286Member
    edited March 25

    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.

  • KequcKequc Posts: 88Member

    @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.

  • SIsilicon28SIsilicon28 Posts: 286Member
    Accepted Answer

    @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.

  • mr_moonmr_moon Posts: 9Member

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

  • KequcKequc Posts: 88Member
    edited March 26

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

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file