This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zamoht
Zamoht wrote ...

2012/8/8

Highscores using userinfo (Asked before)

1
2
Zamoht Zamoht

2012/8/8

#
I want to know how the userinfo works, and how I can use it to make highscores for each level in my game, like it is used in the scenario Kara Sokoban. Thanks for your time.
erdelf erdelf

2012/8/8

#
As I understand it, userinfo gives access to data which is saved on greenfoot.org For Highscores you could import the class for a scoreboard, you can import it in greenfoot itself, just click on edit, import class and then on scoreboard.
tylers tylers

2012/8/8

#
look at my scenarios, most of them have use userdata. this is a simple one http://www.greenfoot.org/scenarios/4045
Zamoht Zamoht

2012/8/8

#
But how do you 'upload' and 'download' the information, and how do you assign a value to an user? I'm not very good at coding, so I'm going to need a detailed description on how to do this.
tylers tylers

2012/8/8

#
when you die you could do:
if (PlayerData.isStorageAvailable())
            {
                PlayerData me = PlayerData.getMyData();
                if (me != null)
                {
                    if (getScore() > me.getScore())
                    {
                        // It's a high-score, only update the score if ours is now higher:
                        me.setScore(getScore());

                        me.store();
                    }
                    //Greenfoot.setWorld(new ScoreWorld(getWidth(), getHeight()));
                    Greenfoot.setWorld(new dead());
                }
            } 
            else
            {
                Greenfoot.stop();
            }
and for the getScore() method:
 public int getScore()
    {
        return Score; //returns you score 
    }
danpost danpost

2012/8/8

#
The way it is used in Kara Sokoban (right now) is not the optimal way to use it (but works for what it is). We were discussing a better approach to saving level scores. The main line is this: since a string consists of an array of characters, and each character is 16-bits long, then each character can 'hold' a value between 0 and 65,535. A character can be made from an integer with the following:
int score = 93;
char ch = (char) score;
To concatenate multiple scores into a String:
String levelScores = "";
int[] scores = { 93, 59, 33, 50, 114, 146, 91, 58, 188, 38 };
for (int i = 0; i < scores.length; i++) levelScores += (char) scores[i];
If the string is longer than 50, you can split it up among the 5 Strings available. Next, you need to able to get the information back out of the string.
String levelScores = "";
for (int i = 0; i < 5; i++) levelScores += getString(i);
int[] scores = new int[levelScores.length()];
for (int i = 0; i < scores.length; i++) scores[i] = (int) levelScores.charAt(i);
That is the basics for storing and retrieving the scores stored in the Strings. As far as the basics for using the UserInfo class, refer to the API and the code snippet supplied there. Make sure you save any new data to the UserInfo file by using the 'store()' method. If you have more specific question, do not hesistate to ask.
danpost danpost

2012/8/8

#
To explain the last code snippet: Line 2 concatenates the 5 Strings into one String variable. Line 3 creates an 'int' array to hold all the scores. Line 4 converts each character in the concatenated string back to 'int' values and stores them in the new 'int' array.
danpost danpost

2012/8/8

#
@tylers, your code is outdated (Beta code). The method names were changed in the final release. i.e. 'PlayerData' is now 'UserInfo'; 'getMyData()' is now 'getMyInfo()'; etc. @Zamoht, the code snippet in the header of the UserInfo API shows how to retrieve the UserInfo data for the current user. It also shows how to store the new (changed) data.
danpost danpost

2012/8/8

#
@tylers, your code is outdated (Beta code). The method names were changed in the final release. i.e. 'PlayerData' is now 'UserInfo'; 'getMyData()' is now 'getMyInfo()'; etc. @Zamoht, the code snippet in the header of the UserInfo API shows how to retrieve the UserInfo data for the current user. It also shows how to store the new (changed) data.
danpost danpost

2012/8/8

#
@Zamoht, what do you mean by 'how do you assign a value to a user'? Be specific as to what you would be trying to do.
tylers tylers

2012/8/8

#
oh yer, forgot to change the code for that scenario how come it still works???
Zamoht Zamoht

2012/8/8

#
I think i got some of it, i may get some new questions through the process. Oh first question you talk alot about 5 Strings, but does it matter what you call these Strings when you store() them and (how ever you get stored data back)?
danpost danpost

2012/8/8

#
Maybe the methods were re-directed to the appropriate 'new' methods, so you would not have to go back and re-edit everything? (possible! but, not sure.)
danpost danpost

2012/8/8

#
Zamoht wrote...
does it matter what you call these Strings when you store() them
You are given one Score 'int', 10 general purpose 'int's, and 5 general purpose 'String's per user. The methods getScore() and setScore(int) are used for the Score 'int'. The methods getInt(int) and setInt(int, int) are used for the general purpose 'int's. The methods getString(int) and setString(int, String) are used for the general purpose 'String's. The get... methods retrieve the data from the UserInfo object. The set... methods saves the new data to the UserInfo object. When you want to re-write the UserInfo file, use the 'store()' method. Once you get a UserInfo object with 'UserInfo myInfo = UserInfo.getMyInfo();', you can use the get and set methods (i.e. 'myInfo.setScore(score);') to retrieve and set the fields and 'myInfo.store();' to re-write the file. Whether creating a UserInfo object or storing one, the whole object is retrieved or stored (the score, the 10 ints and the 5 strings) at one time. The variables really have no names while in the UserInfo object. You can create a variable to contain the data retrieved from the UserInfo object, manipulate it, and then place the new value in the UserInfo object, but, the variable would have nothing (immediate) to do with the actual storing and retrieving of the UserInfo object.
Zamoht Zamoht

2012/8/8

#
@danpost sorry if this is just a stupid question and I'm repeating myself and you've already answered, but if i use UserInfo myInfo = UserInfo.getMyInfo(); (I use that to store the data in a runtime object right). Then I can store a string in a runtime string by using 'String whatEverName = myInfo.getString;' (something like that) what if I want to get the data from another string (there is 5 right) I can't use myInfo.getString again? This text might be as confusing as I'm confused. @tylers the scenario is great, but why do you use PlayerData and not UserInfo (saw danpost's answer the PlayerData was a beta)? And do you need to import anything or is the class imported with Greenfoot?
There are more replies on the next page.
1
2