Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A nested class cannot access function outside its scope #4472

Closed
buckle2000 opened this issue Apr 29, 2016 · 14 comments
Closed

A nested class cannot access function outside its scope #4472

buckle2000 opened this issue Apr 29, 2016 · 14 comments

Comments

@buckle2000
Copy link

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

@buckle2000
Copy link
Author

buckle2000 commented Apr 29, 2016

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))

@buckle2000
Copy link
Author

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

@slapin
Copy link
Contributor

slapin commented Apr 29, 2016

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

@slapin
Copy link
Contributor

slapin commented Apr 29, 2016

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

@vnen
Copy link
Member

vnen commented Apr 29, 2016

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.

@akien-mga
Copy link
Member

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.

@akien-mga akien-mga added this to the 2.2 milestone Aug 2, 2016
@akien-mga
Copy link
Member

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.

@akien-mga akien-mga modified the milestones: 3.0, 2.2 Aug 2, 2016
@bojidar-bg
Copy link
Contributor

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

@reduz
Copy link
Member

reduz commented Aug 8, 2017

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

@amirea
Copy link

amirea commented Dec 3, 2017

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)

@akien-mga
Copy link
Member

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

@akien-mga akien-mga modified the milestones: 3.1, 3.2 Sep 15, 2018
@buckle2000 buckle2000 changed the title A Inner class can not access its outer class's functions A nested class can not access function outside its scope Dec 6, 2018
@buckle2000 buckle2000 changed the title A nested class can not access function outside its scope A nested class cannot access function outside its scope Dec 6, 2018
@FrederickDesimpel
Copy link

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

@vnen
Copy link
Member

vnen commented Mar 10, 2020

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.

@vnen
Copy link
Member

vnen commented Jun 2, 2020

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!

@vnen vnen closed this as completed Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants