0

This question is different because it's about creating a 3D world editor.. at the moment I want to get the updated positions of my models to work..

so I've worked my way up to a rather simple problem. In order to update the scene I want to have a boolean look if a specific input was given and then change from false to true once this input was given.. my problem though is that the Input method has to be in another class and I don't know how I can affect the boolean from there.. here's the code:

public class EngineSetup extends Game
{
    public boolean doStuff = true;

    public void Init()
    {   
        CreationTool updateCoords = new CreationTool(this);
        while(doStuff == true) { updateCoords.Environment(); doStuff = false; }
    }
}

This is what happens when the engine starts. It's going to run the Environment method in my CreationTool class only once, because after the first execution doStuff changes to false.

public class CreationTool extends GameComponent
{
    public void Input(float delta)
    {   
        if(Input.GetKeyUp(getCoords))
        // change the boolean value in EngineSetup to true
    }
}

In the CreationTool class I have this Input method and getCoords is just a variable for the enter key.

So the idea is once I hit the enter key the boolean in the EngineSetup class is set to true and the Environment method is being executed once again and the boolean is changed back to false again.

How can I change the boolean value from within the CreationTool class as decribed above?

Thanks a lot for any help!

EDIT: I've gotten some answers but none of them worked..

public class EngineSetup extends Game
{   
    private static EngineSetup INSTANCE;
    public boolean doStuff = true;

    public void Init()
    {    
        INSTANCE = this;
        CreationTool updateCoords = new CreationTool(this);
        while(doStuff == true) { System.out.println("Work"); doStuff = false; }
}

public class CreationTool extends GameComponent
{
    public void Input(float delta)
    {   
        if(Input.GetKeyUp(getCoords))
            { EngineSetup.getInstance().doStuff=true; }
    }
}

It works the first time around when the program starts but when I hit the enter nothing happens.

public class EngineSetup extends Game
{   
    public static boolean doStuff = true;

    public void Init()
    {    
        CreationTool updateCoords = new CreationTool(this);
        while(doStuff == true) { System.out.println("Work"); doStuff = false; }
}

public class CreationTool extends GameComponent
{
    public void Input(float delta)
    {   
        if(Input.GetKeyUp(getCoords))
            EngineSetup.doStuff = true;
    }
}

Also this didn't work, the first time around when the program start everything's fine but when I hit enter: nothing..

How to fix..?

DisasterCoder
  • 477
  • 1
  • 3
  • 17
  • method names in Java are `lowerCamelCase` –  Jul 23 '15 at 22:42
  • It looks like CreationTool takes an instance of EngineSetup as an argument in its constructor. Why not utilize this to set the boolean? – JamesB Jul 23 '15 at 22:46
  • I don't thik this is really a duplicate. The answer there is specialized for that post. I think my answer is more generic and better. – Roberto Anić Banić Jul 23 '15 at 22:46

3 Answers3

0

Okay, I've restructured my code a little and got the booleans to work the way I wanted. I moved the boolean from the EngineSetup class to the CreationTool class, where the Input is and change the booelans from there:

public class EngineInput extends Game
{       
    public void Init()
    {           
        if(CreationTool.doStuff == true) {
        System.out.println("updateVegetation "+CreationTool.doStuff);       
            this.UpdateVegetation(); // method that updates coordinates
        } else
        if(CreationTool.doStuff == false) 
        System.out.println("updateVegetation "+CreationTool.doStuff);           
    }
}

and in my CreationTool class I can affect the boolean:

public class CreationTool extends GameComponent
{   
    public static boolean doStuff = true;

    EngineInput updateVegetation = new EngineInput();

    public void Input(float delta)
    {           
        if(doStuff == true) { if(Input.GetKeyUp(getCoords)) { System.out.println(doStuff); doStuff = false; updateVegetation.UpdateVegetation(); } } else
        if(doStuff == false) { if(Input.GetKeyUp(getCoords)) { System.out.println(doStuff); doStuff = true; updateVegetation.UpdateVegetation(); } }
    }
}

Thanks for your help! The coordinates are now being read from a text file once I hit the enter key, however the updated positions are still only rendered after restarting the program, this is another riddle I need to solve.

DisasterCoder
  • 477
  • 1
  • 3
  • 17
-1

Ok so let's suppose the EngineSetup class is a Singleton. There will only be 1 object of it created.

You should create a static instance variable, and a get method!

public class EngineSetup extends Game
{
    private static EngineSetup INSTANCE;
    public boolean doStuff = true;

    public void Init()
    {   
        INSTANCE = this;
        CreationTool updateCoords = new CreationTool(this);
        while(doStuff == true) { updateCoords.Environment(); doStuff = false; }
    }

    public static EngineSetup getInstance(){
        return INSTANCE;
    }
}

Now you just need to do this: EngineSetup.getInstance().doStuff=true;

Roberto Anić Banić
  • 1,411
  • 10
  • 21
-2

You can make doStuff static (public static boolean doStuff = true;) so that it can be accessed without having an instance of the EngineSetup class.

Then, in your CreationTool.Input(float) method you can say EngineSetup.doStuff = true;.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57