Sandro Kakhetelidze
Sandro Kakhetelidze

Reputation: 409

Where should I keep game level data?

I have game level data hardcoded in onCreate method of Activity Class, but b I want to access them from other classes to. The data itself doesn't change. Where should I keep the data, what is the most efficient way?

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_game)
    val words=arrayOf("aris","arsi","ris","risa","asi","sari")
    val wordstwo=arrayOf("mzeo","ezo","ezom","zemo","mze")//me
    val wordsthree=arrayOf("misni","is","minis","simi","mis","misi")
    val wordsfour=arrayOf("Tqveni","vqeniT","iqnevT","Tve","qve","vqeni")//"tqven"
    val challege:Array<Array<String>> = arrayOf(words,wordstwo,wordsthree,wordsfour)
    startGame(challege)
}

Upvotes: 0

Views: 96

Answers (3)

Thelouras
Thelouras

Reputation: 880

You can have a static class DataHandler that has the data and access it from this class in all activities.

Update(2022): Don't use static classes :) You can just use a file.

Upvotes: 0

leonardkraemer
leonardkraemer

Reputation: 6783

For data that does not change I suggest you use a TypedArray-Resource

XML file saved at res/values/arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="words">
        <item>"aris"</item>
        <item>"arsi"/item>
        <item>"ris"</item>
        //... other items
    </string-array>
    //... other arrays
</resources>

Then in code:

val array: Array = resources.getStringArray(R.array.words)

This way you have a clean separation between code and data and can not accidentally change it.

Upvotes: 2

Amani
Amani

Reputation: 3979

The fastest way to save and change data in game play for your need (saving string array) is to use shared preferences:

https://developer.android.com/training/data-storage/shared-preferences

Upvotes: 0

Related Questions