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

2017/11/16

UserInfo

Troxter Troxter

2017/11/16

#
Hey @danpost :) I am working on a game like clicker hereos and want to safe the playerdata so that a player doesn't loose his progress. I read some posts about the UserInfo class and wanted to use it but i am not sure how to import the class or even use it. When istry things like:
    public Speicher()
    {
        if(UserInfo.isStorageAvailable()){
            if (UserInfo.getMyInfo() != null){
                player = UserInfo.getMyInfo();
            }
        }
    }
It doesnt know the methodes and variables. I simply need to safe a few things like attackDMG, idleDMG, Mob progress, gold, upgrade progress. Could you pls explain me how to use UserInfo. (pls like i am stupid... maybe i am... sad life :x)
Super_Hippo Super_Hippo

2017/11/16

#
Which methods don't work? You only need to use the usual 'import greenfoot.*;' and it should work. The API shows how to do it: https://www.greenfoot.org/files/javadoc/greenfoot/UserInfo.html
Troxter Troxter

2017/11/16

#
Super_Hippo wrote...
Which methods don't work? You only need to use the usual 'import greenfoot.*;' and it should work. The API shows how to do it: https://www.greenfoot.org/files/javadoc/greenfoot/UserInfo.html
i imported greenfoot.* but he just tells me that he dont know the method UserInfo.isStorageAvailable() or UserInfo.getMyInfo and just everything with UserInfo.*
danpost danpost

2017/11/16

#
Troxter wrote...
i imported greenfoot.* but he just tells me that he dont know the method UserInfo.isStorageAvailable() or UserInfo.getMyInfo and just everything with UserInfo.*
As always, show what you tried !!! (show the entire class -- including import lines)
Troxter Troxter

2017/11/16

#
danpost wrote...
Troxter wrote...
i imported greenfoot.* but he just tells me that he dont know the method UserInfo.isStorageAvailable() or UserInfo.getMyInfo and just everything with UserInfo.*
As always, show what you tried !!! (show the entire class -- including import lines)
Okay i had a world that was named UserInfo thats why it didnt worked and i already expected that but i needed to restart Greenfoot to get it work. I want to safe the Data that i discribed. Could you explain me how to save a value (lets say double attackDMG) when the game is closed and how to get it back started when i start the game? I will implement this for the other values when i have seen how i have to do it by my self. Sorry if my english was bad i wrote fast^^
danpost danpost

2017/11/16

#
A UserInfo object can store eleven int values (using an int score field and 10 general purpose int fields) and five general String object, each up to 50 characters long. To store a double value, you would need to either round it to an int or convert it to an int or to a string before saving it in a UserInfo object. Refer to the UserInfo class documentation to see what methods you will need to use.
Troxter Troxter

2017/11/16

#
danpost wrote...
A UserInfo object can store eleven int values (using an int score field and 10 general purpose int fields) and five general String object, each up to 50 characters long. To store a double value, you would need to either round it to an int or convert it to an int or to a string before saving it in a UserInfo object. Refer to the UserInfo class documentation to see what methods you will need to use.
is it possible to let him save the progress bevore the game is closed? So if the player just click the "x".
danpost danpost

2017/11/16

#
Troxter wrote...
is it possible to let him save the progress bevore the game is closed? So if the player just click the "x".
It might be best to store the game status at the end of each benchmark (end of each level and at game over). You can also have an added possibility to store upon user request. It is possible to have a World object store the status when the 'Pause' button is clicked; but, I doubt you can program it to do it when the project is aborted (when the 'x' is clicked or the page is abandoned or project is refreshed). Keep in mind that UserInfo is only available from within the greenfoot application or on the greenfoot site. It is not available from a standalone jar file (when you turn your project into an application).
Troxter Troxter

2017/11/16

#
danpost wrote...
Troxter wrote...
is it possible to let him save the progress bevore the game is closed? So if the player just click the "x".
It might be best to store the game status at the end of each benchmark (end of each level and at game over). You can also have an added possibility to store upon user request. It is possible to have a World object store the status when the 'Pause' button is clicked; but, I doubt you can program it to do it when the project is aborted (when the 'x' is clicked or the page is abandoned or project is refreshed). Keep in mind that UserInfo is only available from within the greenfoot application or on the greenfoot site. It is not available from a standalone jar file (when you turn your project into an application).
okay thank you :) very helpful! But could you pls show me how i store 1 string corrctly? I know i can see the methodes on the documentation but i never used a string and there are the following arguments: (int index, java.lang.String value) so what do i have to insert there?
danpost danpost

2017/11/17

#
Troxter wrote...
could you pls show me how i store 1 string corrctly? I know i can see the methodes on the documentation but i never used a string and there are the following arguments: (int index, java.lang.String value) so what do i have to insert there?
The index is a number from one to four, depending on which of the five strings you want to set. The string is what you want the indexed string to be set to. You can convert a double to a string and save it to user info as follows:
// with
UserInfo me = UserInfo.getMyinfo(); // presuming info is available
Double doubleValue = 123.456;
// to convert to string
String stringValue = Double.toString(doubleValue);
// then to set the first UserInfo object string field
me.setString(0, stringValue);
// finally, save the changes made to the UserInfo object
me.store();
Troxter Troxter

2017/11/17

#
danpost wrote...
Troxter wrote...
could you pls show me how i store 1 string corrctly? I know i can see the methodes on the documentation but i never used a string and there are the following arguments: (int index, java.lang.String value) so what do i have to insert there?
The index is a number from one to four, depending on which of the five strings you want to set. The string is what you want the indexed string to be set to. You can convert a double to a string and save it to user info as follows:
// with
UserInfo me = UserInfo.getMyinfo(); // presuming info is available
Double doubleValue = 123.456;
// to convert to string
String stringValue = Double.toString(doubleValue);
// then to set the first UserInfo object string field
me.setString(0, stringValue);
// finally, save the changes made to the UserInfo object
me.store();
Okay nice! I didnt know that. And when i want to use the double again do i have to use
Double doubleValue = String.toDouble(Stringvalue)
or can i simply use the string as my double?
danpost danpost

2017/11/17

#
Troxter wrote...
when i want to use the double again do i have to use
Double doubleValue = String.toDouble(Stringvalue)
or can i simply use the string as my double?
You cannot use a String as a double -- and there is no 'toDouble' method in the String class. Did you even look at the String API documentation to see what methods were available to use?
You need to login to post a reply.