jeudi 25 mai 2023

show MainActivity console readout from android studio onto app screen

I have a function that works in android studio and spits out correct results to the "MainActivityKt" console window in the bottom of android studio, However I need the results to show on the main activity page on screen so the user can see the list. ive tried setting the Text(text = "$RandomQuest"), however when i do that i get the error the "unresolved reference - create local variable". When following the suggested fixes and making new variables i end with my 'random' function not working in the: val randomQuestGiver = questGiver[random.nextInt(questGiver.size)] Value. `


`package com.example.trainingapplication
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.sp
import com.example.trainingapplication.ui.theme.TrainingApplicationTheme
import java.util.Random

class MainActivity : ComponentActivity() {



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        TrainingApplicationTheme {
            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ){
                Text(
                    text = "Quest Giver: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Quest Objective: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Quest Location: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Quest Rewards: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Potential Complications: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "NPCs Involved: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Quest Hooks: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Timeline to Complete: ",
                    fontSize = 20.sp
                )
                Text(
                    text = "Consequences: ",
                    fontSize = 20.sp
                )
                Button(onClick = {
                    generateRandomQuest()

                }) {
                    Text(text = "Generate A Quest!")

                }
            }
            }

        }
    }
}


fun generateRandomQuest() {


/// Values containing list

val questGiver = arrayOf("Wandering Knight", "Village Elder")
val questObjective = arrayOf("Rescue a kidnapped individual.", "Recover a stolen item.")
val questLocation = arrayOf("Somewhere amidst an azure ocean.", "second option")
val questRewards = arrayOf("A hefty bag of gold coins", "A magical weapon")
val questHooks = arrayOf("A mysterious letter arrives", "The party stumbles")
val consequences = arrayOf("A cherished NPC dies.", "A dangerous creature is unleashed.")

/// Values that populate the list with random results
val random = Random()
val randomQuestGiver = questGiver[random.nextInt(questGiver.size)]
val randomquestObjective = questObjective[random.nextInt(questObjective.size)]
val randomquestLocation = questLocation[random.nextInt(questLocation.size)]
val randomquestRewards = questRewards[random.nextInt(questRewards.size)]
val randomNPCsInvolved = npcsInvolved[random.nextInt(npcsInvolved.size)]
val randomquestHooks = questHooks[random.nextInt(questHooks.size)]
val randomtimeline = timeline[random.nextInt(timeline.size)]
val randomconsequences = consequences[random.nextInt(consequences.size)]

/// The below lines display in the android studio console under "MainActivityKt"
/// However I need them to be used in/with the corresponding text on screen
println("hello World!")

println("Quest Giver: $randomQuestGiver")
println("Quest Objective: $randomquestObjective")
println("Quest Location: $randomquestLocation")
println("Quest Rewards: $randomquestRewards")

println("NPCs Involved: $randomNPCsInvolved")
println("Quest Hooks: $randomquestHooks")
println("Timeline: $randomtimeline")
println("Consequences: $randomconsequences.")

/// Next step is to print above results OUTSIDE this function, and onto a text page

}`






Aucun commentaire:

Enregistrer un commentaire