Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do I make a 'Logged in player?' mechanism?
- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
I want to make a mechanism that can tell whether or not a player is using the project.
I've got this:
But when they stop the game, it won't reset, and it will still think there is someone playing. Because of this, the whole thing will break, and I don't want that.
Please help!
I've got this:
when green flag clicked
forever
if <(Loggedinplayer?) = [1]> then
switch costume to [There is a logged in player v]
end
if <(Loggedinplayer?) = [0]> then
switch costume to [No logged in player v]
end
end
But when they stop the game, it won't reset, and it will still think there is someone playing. Because of this, the whole thing will break, and I don't want that.
Please help!
- deck26
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
https://scratch.mit.edu/projects/77775784/ might help
- lederniersamourai
-
Scratcher
500+ posts
How do I make a 'Logged in player?' mechanism?
But when they stop the game, it won't reset, and it will still think there is someone playing. Because of this, the whole thing will break, and I don't want that.
What you said is the solution: Absence of user interection can be considered of being logged-off.
For games that expect intensive user interaction, you can say that the user is no longer logged-in when there is no interaction during 180 seconds. Then you can put a message and tell the user that if there is no interaction during the next 300 seconds, then you will do
stop [ v]and consider that player logged off.
You have to monitor user interaction as follows:
each player playing the game receives update information from each other player of the game. So when no information is received for 180+300 seconds for player “A” you just tell to all players that player “A” is logged off.
Of course the gam for player does the
stop [ v]
Or, instead of that you could draw the “login” message.
This way, there is no difference between absence of user interaction and pressing the red bullt of scratch, or closing th elid of the laptop (or switching off the computer).
Last edited by lederniersamourai (Oct. 24, 2015 14:35:19)
- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
What you said is the solution: Absence of user interection can be considered of being logged-off.
For games that expect intensive user interaction, you can say that the user is no longer logged-in when there is no interaction during 180 seconds. Then you can put a message and tell the user that if there is no interaction during the next 300 seconds, then you will dostop [ v]and consider that player logged off.
You have to monitor user interaction as follows:
each player playing the game receives update information from each other player of the game. So when no information is received for 180+300 seconds for player “A” you just tell to all players that player “A” is logged off.
Of course the gam for player does thestop [ v]
But my game doesn't need intensive user interaction. What do I do then?
- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
Or, instead of that you could draw the “login” message.Also, what's that?
- BowWhalley
-
Scratcher
100+ posts
How do I make a 'Logged in player?' mechanism?
Just send a virtual ping to the player, make the player return the ping, if he returns the ping, he is on the game, if he does not return the ping within say, 5-30s of it being sent, he will be registered as ‘offline’. Do this automatically behind the scenes so it does not require user interaction.
- lederniersamourai
-
Scratcher
500+ posts
How do I make a 'Logged in player?' mechanism?
Just send a virtual ping to the player, make the player return the ping, if he returns the ping, he is on the game, if he does not return the ping within say, 5-30s of it being sent, he will be registered as ‘offline’. Do this automatically behind the scenes so it does not require user interaction.Very simple and effective for projects with light or no interaction.
- BowWhalley
-
Scratcher
100+ posts
How do I make a 'Logged in player?' mechanism?
ThankyouJust send a virtual ping to the player, make the player return the ping, if he returns the ping, he is on the game, if he does not return the ping within say, 5-30s of it being sent, he will be registered as ‘offline’. Do this automatically behind the scenes so it does not require user interaction.Very simple and effective for projects with light or no interaction.
. If you have any trouble, just let me know.- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
Just send a virtual ping to the player, make the player return the ping, if he returns the ping, he is on the game, if he does not return the ping within say, 5-30s of it being sent, he will be registered as ‘offline’. Do this automatically behind the scenes so it does not require user interaction.
What's a ping?
- BowWhalley
-
Scratcher
100+ posts
How do I make a 'Logged in player?' mechanism?
Basically a packet being sent somewhere and it being sent back. Google itJust send a virtual ping to the player, make the player return the ping, if he returns the ping, he is on the game, if he does not return the ping within say, 5-30s of it being sent, he will be registered as ‘offline’. Do this automatically behind the scenes so it does not require user interaction.
What's a ping?
.- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
But that won't work if they just click the stop button! 

- lederniersamourai
-
Scratcher
500+ posts
How do I make a 'Logged in player?' mechanism?
A ping is a IT technique used in networks. IT specialists says that nodes in a network can send a message to another node to know if the line between them is still ok, i.e. to know that is nodes can speak to each others or to discover that the line is broken.
So, one sends a ping and the answer is a pong. So this makes a ping-pong.
In your case I would do the following.
FIRST, decide the names of the players you will invite. This will greatly simplify the code of your program. In a second version you can open to all scratchers.
Put all the names in a list.
Then do this:
Now you have just to collect the pings of everybody.
Now in the list “who is logged” you have the lst time you got a ping from them of all logged people
By comparing this wiht the current time (second and milliseconds), you can see if some program does not send its “ping” anymore for a long time.
So, one sends a ping and the answer is a pong. So this makes a ping-pong.
In your case I would do the following.
FIRST, decide the names of the players you will invite. This will greatly simplify the code of your program. In a second version you can open to all scratchers.
Put all the names in a list.
Then do this:
when green flag clickedYou have the user id and you send it in a cloud var repeatedly between 5 to 10 seconds. I'v put decimals to get a better randomization.
if <not <[invited usernames list v] contains (username)>> then
stop [all v]
end
set [my user id v] to [1]
repeat until <(item (my user id) of [invited usernames list v]) = (username)>
change [my user id v] by (1)
end
forever
wait (pick random (5.01) to (9.99)) secs
set [cloud ping v] to (my user id)
end
Now you have just to collect the pings of everybody.
when green flag clicked
delete (all v) of [who is logged v]
repeat (length of [invited usernames list v])
add [0] to [who is logged v]
end
replace item (my user id) of [who is logged v] with (days since 2000)
forever
wait (pick random (1.01) to (1.99)) secs
replace item (cloud ping) of [who is logged v] with (days since 2000)
end
Now in the list “who is logged” you have the lst time you got a ping from them of all logged people
By comparing this wiht the current time (second and milliseconds), you can see if some program does not send its “ping” anymore for a long time.
- lederniersamourai
-
Scratcher
500+ posts
How do I make a 'Logged in player?' mechanism?
But that won't work if they just click the stop button!Yes, this will work.
Suppose that you have players with usernames aaa, bbb, ccc
They play at the same moment with your same game.
Then each 5 seconds each one will send a ping to every others. Everybody will see the ping regularly and notice that everybody plays the game.
Suppose that aaa press “stop” but bbb and ccc continue to play.
The players bbb and ccc will continue to send and receive to each other their ping and they will notice that aaa does not send anymore a ping.
So they will conclude that aaa has pressed “stop” OR that the network for aaa is broken (i.e. aaa is not “on-line”).
- BowWhalley
-
Scratcher
100+ posts
How do I make a 'Logged in player?' mechanism?
Thankyou for explaining my thoughts! You read my mindBut that won't work if they just click the stop button!Yes, this will work.
Suppose that you have players with usernames aaa, bbb, ccc
They play at the same moment with your same game.
Then each 5 seconds each one will send a ping to every others. Everybody will see the ping regularly and notice that everybody plays the game.
Suppose that aaa press “stop” but bbb and ccc continue to play.
The players bbb and ccc will continue to send and receive to each other their ping and they will notice that aaa does not send anymore a ping.
So they will conclude that aaa has pressed “stop” OR that the network for aaa is broken (i.e. aaa is not “on-line”).
.- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
Oh! Thanks! I get it now! 

- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
A ping is a IT technique used in networks. IT specialists says that nodes in a network can send a message to another node to know if the line between them is still ok, i.e. to know that is nodes can speak to each others or to discover that the line is broken.
So, one sends a ping and the answer is a pong. So this makes a ping-pong.
In your case I would do the following.
FIRST, decide the names of the players you will invite. This will greatly simplify the code of your program. In a second version you can open to all scratchers.
Put all the names in a list.
Then do this:when green flag clickedYou have the user id and you send it in a cloud var repeatedly between 5 to 10 seconds. I'v put decimals to get a better randomization.
if <not <[invited usernames list v] contains (username)>> then
stop [all v]
end
set [my user id v] to [1]
repeat until <(item (my user id) of [invited usernames list v]) = (username)>
change [my user id v] by (1)
end
forever
wait (pick random (5.01) to (9.99)) secs
set [cloud ping v] to (my user id)
end
Now you have just to collect the pings of everybody.when green flag clicked
delete (all v) of [who is logged v]
repeat (length of [invited usernames list v])
add [0] to [who is logged v]
end
replace item (my user id) of [who is logged v] with (days since 2000)
forever
wait (pick random (1.01) to (1.99)) secs
replace item (cloud ping) of [who is logged v] with (days since 2000)
end
Now in the list “who is logged” you have the lst time you got a ping from them of all logged people
By comparing this wiht the current time (second and milliseconds), you can see if some program does not send its “ping” anymore for a long time.
It worked! Thank you soooooo much!







Last edited by Daring_Sailor (Nov. 1, 2015 10:11:09)
- lederniersamourai
-
Scratcher
500+ posts
How do I make a 'Logged in player?' mechanism?
Remeber that if you want to extend that to players not in the “master” list of players, you will have two options:
1) a “master” player must be on-line to accept unregistered players
2) unregistered players can play between them without any “master” players
In both cases you must
a) communicate usernames into cloud variables. For that you must encode those names into digits because cloud variables do not accept letters.
b) synchronize every players in order that they have the full list of current players.
You will find solutions by exploring projects of others.
Have fun!
1) a “master” player must be on-line to accept unregistered players
2) unregistered players can play between them without any “master” players
In both cases you must
a) communicate usernames into cloud variables. For that you must encode those names into digits because cloud variables do not accept letters.
b) synchronize every players in order that they have the full list of current players.
You will find solutions by exploring projects of others.
Have fun!
- Daring_Sailor
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
Remeber that if you want to extend that to players not in the “master” list of players, you will have two options:
1) a “master” player must be on-line to accept unregistered players
2) unregistered players can play between them without any “master” players
In both cases you must
a) communicate usernames into cloud variables. For that you must encode those names into digits because cloud variables do not accept letters.
b) synchronize every players in order that they have the full list of current players.
You will find solutions by exploring projects of others.
Have fun!
Couldn't I just:
when green flag clicked
if <not <[ invited usernames list v] contains [(username) ]>> then
add (username) to [ invited usernames list v]
end
Then do the ‘ping’ thing?
- deck26
-
Scratcher
1000+ posts
How do I make a 'Logged in player?' mechanism?
No point having the list in that case, just let anyone connect. Also the list would only be updated for that user - it's not in the cloud - so if the other players needed to know that player's name you'd have to encode and decode it to get them to see it.Remeber that if you want to extend that to players not in the “master” list of players, you will have two options:
1) a “master” player must be on-line to accept unregistered players
2) unregistered players can play between them without any “master” players
In both cases you must
a) communicate usernames into cloud variables. For that you must encode those names into digits because cloud variables do not accept letters.
b) synchronize every players in order that they have the full list of current players.
You will find solutions by exploring projects of others.
Have fun!
Couldn't I just:when green flag clicked
if <not <[ invited usernames list v] contains [(username) ]>> then
add (username) to [ invited usernames list v]
end
Then do the ‘ping’ thing?
- lederniersamourai
-
Scratcher
500+ posts
How do I make a 'Logged in player?' mechanism?
No point having the list in that case, just let anyone connect. Also the list would only be updated for that user - it's not in the cloud - so if the other players needed to know that player's name you'd have to encode and decode it to get them to see it.Remeber that if you want to extend that to players not in the “master” list of players, you will have two options:
1) a “master” player must be on-line to accept unregistered players
2) unregistered players can play between them without any “master” players
In both cases you must
a) communicate usernames into cloud variables. For that you must encode those names into digits because cloud variables do not accept letters.
b) synchronize every players in order that they have the full list of current players.
You will find solutions by exploring projects of others.
Have fun!
Couldn't I just:when green flag clicked
if <not <[ invited usernames list v] contains [(username) ]>> then
add (username) to [ invited usernames list v]
end
Then do the ‘ping’ thing?
I proposed the simple solution for a quick win for Drunken_Sailor
Today he understands quite well what happens and he can test his game with his friends.
The list is local for each user. So, you must initialise the list in the game in order for all palyers to get the same table for the identifiers.
At the very beginning, the scratch team planned to give a unique id to each scratcher (I guess it is the one used for your “scratch homepage”). Then they decided not do that.
In a number of projects, there are solutions for encod/decode of usernames and simulating lists in a cloud variable. Those solutions are more complicated to understand.
Inthe “explore” tab of the scratch website, just search for “encode”, or “decode”, of “top ranked” for cloud games.
- Discussion Forums
- » Help with Scripts
-
» How do I make a 'Logged in player?' mechanism?



