Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm writing an Android application that needs to pass a single boolean into an Android game that I am also writing. The idea is that the user does something in the App which will affect how the game operates.

This is tricky with LIBGDX since I need to get the bool value into the Java files of the game, but of course, you can't call Android specific things from within LIBGDX's main Java files. I tried using an intent but of course the same problem persists. I can get the boolean into the MainActivity.Java of the android output of the game, but can't pass it along any further since the android output and the main java files don't know about each other. I have seen a few tutorials that explain how to use set up an interface in the LIBGDX java files that can call android things. This seems like wild overkill for what I want to do.

I've been trying to use Android's Shared Preferences with LIBGDX's Gdx.app.getPreferences, but I can't make it work.

Anyhelp would be MUCH appreciated.

I've set up two hello world applications. One is a standard Android app, with a single button that is supposed to write "true" into the shared preferences. The other is a standard LIBGDX hello world that is supposed to do nothing but check that bool when launched and if true display one image to the screen, if false, display a different one.

Here's the relevant bit of the Android code:

import android.preference.PreferenceManager;

 public void onClick(View view) {
    if (view == this.boolButton){

        final String PREF_FILE_NAME = "myBool";
        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_WORLD_WRITEABLE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("myBool", true); 
        editor.commit();        
    }
}

And here's the relevant bit of the code from the LIBGDX main file:

Preferences prefs = Gdx.app.getPreferences("myBool");
    boolean switcher = prefs.getBoolean("myBool");

    if(switcher == true){
        texture = new Texture(Gdx.files.internal("data/worked512.png"));
        prefs.putBoolean("myBool", false);
        }
        else
        {
        texture = new Texture(Gdx.files.internal("data/libgdx.png"));
        }

Everything compiles fine, it just doesn't work. I've spent HOURS googling trying to find a way to pass this single boolean from android into a LIBGDX main and I'm totally stumped.

Thanks for your help.

share|improve this question
    
I answered a question similar, but using sqllite :) gamedev.stackexchange.com/questions/24754/sqlite-in-libgdx/… With callbacks and interfaces you can pass the variables –  Rudy_TM Aug 1 '12 at 0:26
    
Thanks, I'll give that a try. –  Doug Henning Aug 1 '12 at 0:56
add comment

3 Answers 3

Even if the interface thing sounds like overkill, it will solve your problem, and is not much work. However, here's another solution: Just pass the value to your game when you create it, no need for the interface.

Example:

public class MyLibgdxGame implements ApplicationListener {
    // Or extends Game

    boolean passedInFlag;

    public MyLibgdxGame(boolean flag) {
        this.passedInFlag = flag;
    }

    // rest of the methods
}

Then you can use that boolean in your game.

Then you will need to change your starters to pass in a boolean. For Android:

public class AndroidStarter extends AndroidApplication {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        boolean myBoolToPass = false;

        AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        // config stuff

        initialize(new MyLibgdxGame(myBoolToPass), cfg);
  }
}

All you do is pass it through a normal constructor.

share|improve this answer
add comment

Try some read firstly: http://code.google.com/p/libgdx-users/wiki/IntegratingAndroidNativeUiElements3TierProjectSetup .

share|improve this answer
    
Ya I've seen that. Like I said, HOURS of Googling. This is the interface thing I mentioned that just feels like silly over kill. –  Doug Henning Aug 1 '12 at 0:57
add comment

Have you tried using postRunnable?

http://code.google.com/p/libgdx/wiki/ApplicationThreading

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.