2017년 4월 15일 토요일

High Scores with Tinydb in AI2 - Simplified (lesson)


Hello, I just wanted to post this real quick for anyone who may be looking for this.   I was looking all over the place for tutorials for how to use Tinydb to store highscores and all i found were huge pages of lessons and some unexplained procedures etc.   All mainly shown in AI1 as well.
Well, for anyone new who is just starting to learn App Inventor...let me save you some headaches with Highscores for  
your game.

For one single highscore on one single screen.... this is literally ALL you need to make it work.

Now let me explain.   First off... Screen1 is a copy/paste version from my working program thats why you see the red flag.  I just wanted to isolate just what you need to know.

1- create a procedure and name it.
2- find the label you use to post your final score at the end of your game.  Mine is FinalScoreValueLabel.
3- where you see FinalScore > HighScoreLabel.....I created a high score page in my options.  The highscore is that label.
So basically that entire if loop says:  if the score i just got in the game is higher then what ever my current highest score ever is, then make my game score the new high score.  Obviously if its not higher then it will not do anything,

*I keep all the names close togather so it may look a bit confusing, but the purple box that says:  call HighScore.store value is not the highscore label....this is my Tinydb renamed to HighScore.

4-within your Tinydb drawer, select this block i have in the pic.  the:  call Tinydb.StoreValue block  In the tag, that was written on the spot, this is where you define the name of the tag to be stored on your phone.  I named mine HighestScore
ValueToStore:  this is the highscoreLabel.text ---  from my highscore page in my options.  The same one from the if loop.

Basically, if my score of the game is higher then the highest score, it changes to the new score otherwise it does nothing.  but weither the high score changed or not, the program keeps what ever the Highest score is..stored.

At the bottom of the pic is the single call procedure to call this entire thing,   You need to slot this in where ever your function that results in the game being over is.

Finally, the screen initialize block: 
5-  you need to call the highestscore tag that is saved on your phone when the screen first starts up.  This is the call Tinydb.getvalue.  block
I used this particular GetValue box for a reason:   The tag is obviously calling the name you saved it under on your phone from earlier.   But the other item....valueTagNotThere.....this needs to be set so that the very first time you load up your phone, there will obviously be nothing stored on your phone yet, but as we all know...computers need absolutely everything spelled out for them.  Without this tag, the computer will try to find a saved item that does not exsist yet.   Remember....the highestScore Procedure does not run until the program is already running, you finished a game and got a score.  So this GetValueBox in Screen Initialize will read waaaaay before you ever get to actually storing your first score.  So this is a very important step.  You NEED to define a value for when theres nothing to look up for the first time.

My explaining is way more work then building the actual blocks :)   I know there are alot of tutorials about Tinydb, but like i said.  I wanted to spell it out in my own words for new comers.   And for AI2.   There is no need to jump right to all the big stuff first shot like storeing 10 items on 3 different screens etc.
Understand whats happening first....then you wont need to look up a tutorials for the complex stuff when you get to it,,,, babysteps.

Hope it helps!

--
Thanks for sharing that, richard. That is a good, simple method that should prove to be very useful :)

--
Thanks a lot very helpful tips and simple indeed. I too found other tutorials on high scores a bit confusing and mainly on AI1 but you method is first class for beginners.

--
I have used this method and its fine and simple. But I have now started to work on a second screen and understand from other posts that I have read I cant use another TinyDB in my second screen?? So using the method I have got from here How do I go about saving a high score on my second screen????

I have read the colour dot tutorial if that's going to be suggested but I can't work through that method it goes over my head.

--
That's NOT correct Sid... You can use a TinyDB on EVERY screen if you want, but they will ALL use the same file, regardless of what you call the component.  For example, on Screen1 you have a TinyDB1 and you leave it's name as that.  On Screen2, you have a TinyDB but you called it TinyDB2.  Screen1 and Screen2 will share the same datastore.  You can't set up multiple databases with TinyDB for the same app.

--
As a follow up to Enis answer.
For a second screen that has separate data just use a different tag name to keep that data separate from the first screen's data.
You  can, however, access the data stored in the TinyDB from the first screen in the second screen with the tag name used in the first screen.

--
Thanks a lot, thought I would ask before I tried it and yes it worked on the other screen as I took both your advice and created another tag and used another TDB.

Amazing forum and the best support from all.

--
Thanks Sid!  We all appreciate that!  So glad you got it all figured out!
--

댓글 없음:

댓글 쓰기