Godot: A nested class cannot access function outside its scope

Created on 29 Apr 2016  路  14Comments  路  Source: godotengine/godot

...With or without static keyword.
well I think it should.

archived feature proposal gdscript

Most helpful comment

Inner class is not a subclass, so it's expected that it doesn't have access to the outer class' methods. I would propose to add a way to reference the outer class, so you can explicitly make use of inheritance and access static methods.

All 14 comments

extends Object

func calc_offset(dt, target):
    # do whatever thing here
    return ...

class FourWayCtrl:
    func update(dt, target):
# Invalid call. Nonexistent function 'calc_offset' in base 'Reference ()'.
        var offset = calc_offset(dt, target)
        target.apply_impulse(Vector2(0,0), Vector2(offset.x*target.max_acc,offset.y*target.max_acc))

Also, the completion shows calc_offset inside FourWayCtrl. It is another question if I am not supposed to do that.

This is normal, as Godot do not implement closures.
You will need to provide appropriate references.

If you want closures to be added, just add proper request. And send PR fixing it.

Inner class is not a subclass, so it's expected that it doesn't have access to the outer class' methods. I would propose to add a way to reference the outer class, so you can explicitly make use of inheritance and access static methods.

I confirm that it's a bug, actually the inner class has access to its outer class' functions if it's place _at the end_ of the file. If place at the beginning, it can only see the outer constants, but all the rest will be seen as undefined.

Nevermind, looks like I misinterpreted the situation in my script, even when placed at the end of the file the inner class does not have access to its outer class' functions and member variables, only constants.
I do believe this would be an interesting feature though, as various languages do support it (e.g. Java). As @vnen mentions, it could be something explicit and not implicit like in Java.

In any case, static functions should be accessible by all inner classes, since it is currently impossible to do it without a recursive load or preload...

@bojidar-bg you can give it a try if you want, meanwhile I will kick to 3.1

I suspect this is related with a few other problematic cases (and in general with the order of traversing the script):

  1. Class inter-relation (depends on order)
class A1:
    func B1():
        return A2.new()

class A2:
    func B2():
        return A1.new()
  1. Recursive components
class A:
    var a 
    func B():
        a = A.new()

EDIT:
I might be speaking nonsense but if only GDScript scripts could be parsed in two passes... (one preliminary to establish identifiers)

Moving to the next milestone as 3.1 is now feature frozen.

Will this work in Godot 4 trough use of the new callable type ?

Callable doesn't help with this. As I mention, we either need some way to reference the outer class or automatically resolve the static method in the compiler (like it does with constants).

But yes, I intend to work on this for 4.0.

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mefihl picture mefihl  路  3Comments

RayKoopa picture RayKoopa  路  3Comments

SleepProgger picture SleepProgger  路  3Comments

nunodonato picture nunodonato  路  3Comments

testman42 picture testman42  路  3Comments