Sunday 23 September 2012

#1.7 Tutorial (LibGDX & MTX) SettingsManager - Settings, Save/Load, Wirte/Read/Change, Log

TEST SCREEN

There is no test screen or test code that I have done for SettingsManager of MTX, because it is straightforward enough, however I will talk about what can you do with it, It will make your life easier and faster by writing less and less code.



SETTING MANAGER

Setting manager will give you bunch of static methods and variables that you can use in every game lets see, what are these. All methods are documented, so you can read if you do not understand.




Set as a first launch and check if it is first launch

You may need to create initial files or settings in the first launch of the game and check if it is the first launch or not in every game launch, methods below works with android preferences, and sets the game as first launch, and checks it if you need. You can use theses methods in create() method of the game to set some stuff

  if(SettingsManager.isFirstLaunch()){
   SettingsManager.setFirstLaunchDone(true);
// DO SOMETHING FOR FIRST LAUNCH
// Create database
// Create textfiles
// Set initial settings
// etc..
  } else {
// IT IS NOT FIRST LAUNCH
// Load previous settings
// Set some stuff
// etc..
}
 



Create text files in local storage

If I dont do big games (multiplayer rpgs or similar), I like to use text files for storing basic game data, best place to create thiese data is the LOCAL STORAGE

SettingsManager.createTextFileInLocalStorage(String fileName)






Read / Write / Changes existing line in text files

You may need to read, write new line or change existing line in text files. In Android text files could be in INTERNAL STORAGE (cant write or cahnge anything here, only read), LOCAL STORAGE and EXTERNAL STORAGE. (These are the FileTypes)

SettingsManager.readLine(String strFile, int lineNumber, FileType fileType)
SettingsManager.writeLine(String strFile, String newValue, FileType fileType)
SettingsManager.writeExistingLine(String strFile, int lineNumber, String newValue, FileType fileType) 





Number of lines and Comma Seperated Values in text files

You may need the number of lines in a text file or your text file lines can be comma separated values such as (Jack,24,Engineer) in a single line, my method returns a ArrayList for each value between commas.

SettingsManager.getNumberOflInesInTextFile(String strFile, FileType fileType)
SettingsManager.getValuesSeperatedByCommaInLine(String strFile, int lineNumber, FileType fileType)





Music, SoundFX, Vibration and Previous settings retrieving for each game launch


You can set music, soundFX and vibration settings and quickly access in every class of the game with static variables  also with a single method you can retrieve all the settings from android preferences in every game launch.

Here is a example for sound effects, setSound(), use this in your settings screen or anywhere in the game, it is static method.


// Set sound condition (Android Pref), also sets the static variable isSoundOn;
SettingsManager.setSound(boolean isSoundActive);

// Get sound condition, static variables (We do not retrieve from android pref ea// ch time we need, performance costs) 
SettingsManager.isSoundOn;


// Use this in game launchs, it sets music, soundFX and vibration settings from preferences
SettingsManager.setGeneralSettings();


2 comments:

  1. Can you plz suggest on how to add top 5 high scores using SettingsManager. I tried but couldn;t do. The High scores are being saved and displayed only during one game launch and restart after game over. Once I close my application and start the application, it shows me the default scores set as 100,80,60,20,0

    ReplyDelete
    Replies
    1. Check out the latest mtx (v2.1), check out my flame words game code. You use FileManager to create textfile and add scores line by line to there.

      Delete