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

2013/5/20

UserInfo

JetLennit JetLennit

2013/5/20

#
Continuing the http://www.greenfoot.org/scenarios/8400 conversation, what code do I change for it?
bourne bourne

2013/5/20

#
Depends on what you are wanting to do. To access data from all Users, you need to use methods I mentioned earlier (usually use getTop(int)) to obtain a list, which then can be iterated - pulling data (like the strings or ints) from their elements, which then can be displayed, etc.
JetLennit JetLennit

2013/5/20

#
I was going to start off with one and one alone, in which whenever someone changes it, it changes
bourne bourne

2013/5/20

#
Here is an example of getting the first String from Users:
if (UserInfo.isStorageAvailable())
{
    List<UserInfo> users = UserInfo.getTop(10000); // Big number to try to get all
    for (UserInfo user : users)
        System.out.println(user.getString(0));
}
To make it a single message between all Users, my first thought would be: (with the UserInfo from UserInfo.getMyInfo()) 1. Set one of the Strings to the message 2. Set score to be that of the top score plus 1. ( i.e. setScore(UserInfo.getTop(1).get(0).getScore() + 1) with added accommodation for if UserInfo.getTop(1) returns an empty list - being the first messager) 3. Store Then the current message is obtained by UserInfo.getTop(1).get(0).getString(int)
JetLennit JetLennit

2013/5/20

#
It gets an error on .getScore() saying it "can not find symbol - method getScore()"
bourne bourne

2013/5/20

#
Ah. That is because UserInfo.getTop(1).get(0) returns an Object and not a UserInfo type - unfortunately, although it has to be under the context. Sorry, Try using a cast like so: UserInfo topUser = (UserInfo)UserInfo.getTop(1).get(0); topUser.getScore()
JetLennit JetLennit

2013/5/20

#
So, then I should set the index number to be the score?
bourne bourne

2013/5/20

#
Index for what?
JetLennit JetLennit

2013/5/20

#
For example
getInt(this part)
bourne bourne

2013/5/20

#
No, in my example/approach, you don't need to use the ints. Just the separate score int (makes it easy to find the current, since will be at the top of the getTop list of users)
JetLennit JetLennit

2013/5/20

#
No, I mean that index in all of the storage things
bourne bourne

2013/5/20

#
To store the message, use UserInfo.setString(0, message) - Sets the first String to message. Doesn't matter so long as you are reading from that String as well: ((UserInfo)UserInfo.getTop(1).get(0)).getString(0)
You need to login to post a reply.