0

In a method of a custom class View:

public class View {
    private Timer timer;
    ...
    private double[][] allLevels;
    ...

I have a method with an abstract call that needs to point to the variable allLevels. The variable is produced by another class GameLogic, but in the Main of the application. In the Main, the return argument from a public method is then passed to the View:

public class Game extends ApplicationAdapter {
    View view;
    GameLogic gameLogic;

    @Override
    public void create () {
        System.out.println("Creating");
        this.gameLogic = new GameLogic();
        this.gameLogic.prepareStimulus();
    }

@Override
    public void render () {
        Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        this.view = new View();
        this.view.presentStimulus(this.gameLogic.allLevels);

    }
}

Because of a very complex game/business logic I thought I would try to separate and encapsulate as much as possible in a MVC-ish pattern. The Main uses libgdx, which requires create and render to be separate.

My main problem is that I am unable to reach the variable in the class View from the abstract call to the class scope.

public void presentStimulus(double[][] allLevels){
...
    timer = new Timer();
...
        timer.scheduleTask(new Task(){
            @Override
            public void run(){
                DO SOMETHING WITH that.allLevels[0][0]
            }
        }, .....);

I have looked at a similar issue, but I guess my question is more basic.

The IDE is unable to autocomplete the reference to the properties using the keyword "this". How can I make the Run() method access the property of the instance of the outer class?

Community
  • 1
  • 1
noumenal
  • 1,077
  • 2
  • 16
  • 36
  • 2
    I have read the question twice, and still have no idea what your concrete question is. What the run() method should call is up to you: you seem to want to schedule a task, and the run() method defines what this scheduled task should do. If that's what you're asking, the `allLevels` argument of presentStimulus() must be declared as final if you want to be able to access it from run() (unless you're using Java 8) – JB Nizet Apr 05 '15 at 09:56
  • it's not clear how an architecture solves the problem you have here. – ChiefTwoPencils Apr 05 '15 at 09:58
  • Sorry about the vagueness and what turned out to be a very basic question. I do not know Java enough to understand that what I am trying to achieve was actually possible. I am using Java 8, but adding final seems to work. Eclipse is no longer complaining. I don't know how I could miss this using an IDE. The design consequence is that I would have to call presentStimulus() again if I change the level structure. – noumenal Apr 05 '15 at 10:41
  • @JB Nizet I have now edited the question to better reflect the solution using "final" that helped me define the problem. – noumenal Apr 05 '15 at 10:47
  • 1
    So, your question is: how to access properties of the enclosing instance from an inner class? This is covered by the Java tutorial. First page returned by the google search 'inner class tutorial": https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – JB Nizet Apr 05 '15 at 10:52
  • Edited again to reflect terminology (hopefully more accurate now). I will have to start reading more about inner classes. I erroneously thought my problem was due to the abstract invocation. – noumenal Apr 05 '15 at 11:20

1 Answers1

0

The problem ( I rather doubt it) is here -

public void presentStimulus(double[][] allLevels){
...
    timer = new Timer();
...
    timer.scheduleTask(new Task(){
        @Override
        public void run(){
            DO SOMETHING WITH that.allLevels[0][0]
        }
    }, .....);

You see, in this code time.scheduleTask, you are just only creating a new Task and not executing it. It is executed after a while with another thread I supposed, thus it is run in a different context, which does not have allLevels value at the time of execution. So there is no way you can access that allLevels in that run method unless you use closure. I am not sure whether java supports closure or not, but here is a similar answer that might help you - Closure in Java 7

You can use some other solutions, like save the hash and allLevels in a separate static dictionary that is accessible globally and then pick the value from there.

Community
  • 1
  • 1
brainless coder
  • 6,310
  • 1
  • 20
  • 36