Discuss Scratch

julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

How can I detect if a player quit the game and either hit the stop button or goes to a different website? I also would like it to change/set some variables by -1. Help will be appreciated.
peppermintpatty5
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

Maybe you could have your project detect if a key has been pressed in the last 30 or so seconds:
whenANYkeypressedresettimer. . .other scripts here
whenclickedforeveriftimer>30setAFKtotrue
So something along these lines. If your game uses the mouse, then I could probably think up a script for that.
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

peppermintpatty5 wrote:

Maybe you could have your project detect if a key has been pressed in the last 30 or so seconds:
whenANYkeypressedresettimer. . .other scripts here
whenclickedforeveriftimer>30setAFKtotrue
So something along these lines. If your game uses the mouse, then I could probably think up a script for that.
Thanks for the idea , but I want something that detects if the user quits the game and changes a variable. Not an AFK system.
peppermintpatty5
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

whoops, double post…

Last edited by peppermintpatty5 (Aug. 17, 2015 01:08:00)

peppermintpatty5
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

Hmm, there is a way to detect when the stop sign is clicked.
It works because this block will run even when the project has stopped:
whentimer>1keeps going!sayStop sign clicked!
whenclickedforeverresettimer
Hope this helps!

Btw, you won't be able to use the timer anywhere else in your project if you implement this.
There's a workaround to not having the timer though…

Last edited by peppermintpatty5 (Aug. 17, 2015 01:06:30)

julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

peppermintpatty5 wrote:

Hmm, there is a way to detect when the stop sign is clicked.
It works because this block will run even when the project has stopped:
whentimer>1keeps going!sayStop sign clicked!
whenclickedforeverresettimer
Hope this helps!

Btw, you won't be able to use the timer anywhere else in your project if you implement this.
There's a workaround to not having the timer though…
Thanks so much ! But that's kind of disappointing that you can't use the timer anywhere else in your project
I'll look it up to see if there's any other methods. If there isn't, I'll just use this method and use a workaround for the timer.
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

julianandwill wrote:

peppermintpatty5 wrote:

Hmm, there is a way to detect when the stop sign is clicked.
It works because this block will run even when the project has stopped:
whentimer>1keeps going!sayStop sign clicked!
whenclickedforeverresettimer
Hope this helps!

Btw, you won't be able to use the timer anywhere else in your project if you implement this.
There's a workaround to not having the timer though…
Thanks so much ! But that's kind of disappointing that you can't use the timer anywhere else in your project
I'll look it up to see if there's any other methods. If there isn't, I'll just use this method and use a workaround for the timer.
Ok, I looked it up, and everything I see is using the timer. Kind of disappointing , but still worth it. Oh btw, do Scratch projects stop if you leave the website?
deck26
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

What you really need is a cloud variable that is being changed repeatedly (eg once a second) by the player. Other players can see that the variable is changing by saving a copy of the variable and waiting a few seconds before seeing if it has changed. This works for the player pressing the stop button or just closing the web page which is not the case for anything using the timer and the stop button. You need something like this for managing player slots in a multiplayer.

My demo combines the x,y position, costume number and this player-slot-reservation in a single cloud variable per player. https://scratch.mit.edu/projects/63501310/
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

deck26 wrote:

What you really need is a cloud variable that is being changed repeatedly (eg once a second) by the player. Other players can see that the variable is changing by saving a copy of the variable and waiting a few seconds before seeing if it has changed. This works for the player pressing the stop button or just closing the web page which is not the case for anything using the timer and the stop button. You need something like this for managing player slots in a multiplayer.

My demo combines the x,y position, costume number and this player-slot-reservation in a single cloud variable per player. https://scratch.mit.edu/projects/63501310/
Exactly except I already have the player slots set up.

Last edited by julianandwill (Aug. 17, 2015 20:07:48)

julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

julianandwill wrote:

deck26 wrote:

What you really need is a cloud variable that is being changed repeatedly (eg once a second) by the player. Other players can see that the variable is changing by saving a copy of the variable and waiting a few seconds before seeing if it has changed. This works for the player pressing the stop button or just closing the web page which is not the case for anything using the timer and the stop button. You need something like this for managing player slots in a multiplayer.

My demo combines the x,y position, costume number and this player-slot-reservation in a single cloud variable per player. https://scratch.mit.edu/projects/63501310/
Exactly except I already have the player slots set up.
But, do scratch projects stop when you leave the website? I'm just wondering. I'm pretty sure it's yes because they reset.
liam48D
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

julianandwill wrote:

julianandwill wrote:

deck26 wrote:

What you really need is a cloud variable that is being changed repeatedly (eg once a second) by the player. Other players can see that the variable is changing by saving a copy of the variable and waiting a few seconds before seeing if it has changed. This works for the player pressing the stop button or just closing the web page which is not the case for anything using the timer and the stop button. You need something like this for managing player slots in a multiplayer.

My demo combines the x,y position, costume number and this player-slot-reservation in a single cloud variable per player. https://scratch.mit.edu/projects/63501310/
Exactly except I already have the player slots set up.
But, do scratch projects stop when you leave the website? I'm just wondering. I'm pretty sure it's yes because they reset.
They “reset” because when you leave a project, Scratch doesn't save the project's data to the server. When you re-open the page Scratch loads what the project was last time it was saved.
gtoal
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

julianandwill wrote:

Thanks so much ! But that's kind of disappointing that you can't use the timer anywhere else in your project
I'll look it up to see if there's any other methods. If there isn't, I'll just use this method and use a workaround for the timer.

Yes you can :-)

https://scratch.mit.edu/projects/58656772/
gtoal
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

julianandwill wrote:

Oh btw, do Scratch projects stop if you leave the website?

No, unfortunately. I'm working on trying to create a reliable mechanism to do that (http://scratch.mit.edu/projects/41470142/) but to be honest I don't hold out a lot of hope that I will ever get it to work…

G
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

gtoal wrote:

julianandwill wrote:

Thanks so much ! But that's kind of disappointing that you can't use the timer anywhere else in your project
I'll look it up to see if there's any other methods. If there isn't, I'll just use this method and use a workaround for the timer.

Yes you can :-)

https://scratch.mit.edu/projects/58656772/
I know. I said “I will use a workaround for the timer”.
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

liam48D wrote:

julianandwill wrote:

julianandwill wrote:

deck26 wrote:

What you really need is a cloud variable that is being changed repeatedly (eg once a second) by the player. Other players can see that the variable is changing by saving a copy of the variable and waiting a few seconds before seeing if it has changed. This works for the player pressing the stop button or just closing the web page which is not the case for anything using the timer and the stop button. You need something like this for managing player slots in a multiplayer.

My demo combines the x,y position, costume number and this player-slot-reservation in a single cloud variable per player. https://scratch.mit.edu/projects/63501310/
Exactly except I already have the player slots set up.
But, do scratch projects stop when you leave the website? I'm just wondering. I'm pretty sure it's yes because they reset.
They “reset” because when you leave a project, Scratch doesn't save the project's data to the server. When you re-open the page Scratch loads what the project was last time it was saved.
No, I mean when you leave the website, does the project act as if the stop sign is clicked?
deck26
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

julianandwill wrote:

No, I mean when you leave the website, does the project act as if the stop sign is clicked?

If, for example, you close the browser the project cannot detect that - even with the timer/stop button trick. I'd guess the same would apply if you told the browser to go to another page.

So leaving the website cannot be detected in the same way as the stop sign being clicked.
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

deck26 wrote:

julianandwill wrote:

No, I mean when you leave the website, does the project act as if the stop sign is clicked?

If, for example, you close the browser the project cannot detect that - even with the timer/stop button trick. I'd guess the same would apply if you told the browser to go to another page.

So leaving the website cannot be detected in the same way as the stop sign being clicked.
Hmm… Is there any workaround for this?
deck26
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

That's why people use the method of a variable being constantly updated - it detects the stop flag or moving away from the page/closing browser since the variable no longer gets updated. If you're concerned about cloud variables detecting the change that's the answer - the instance of the project can't possibly detect that it's not running if the browser window has disappeared under its feet!
julianandwill
Scratcher
500+ posts

Detecting If Player Quit On Online Multiplayer

deck26 wrote:

That's why people use the method of a variable being constantly updated - it detects the stop flag or moving away from the page/closing browser since the variable no longer gets updated. If you're concerned about cloud variables detecting the change that's the answer - the instance of the project can't possibly detect that it's not running if the browser window has disappeared under its feet!
Hmm… make a project that explains what happens.
deck26
Scratcher
1000+ posts

Detecting If Player Quit On Online Multiplayer

It's actually quite simple.

When an instance of the project is running a cloud variable for that player is constantly being changed. Another instance of the project can see the cloud variable and easily see that it is changing by

- copy cloud variable to a local variable
- wait a few seconds
- check the cloud variable against the local variable

If the cloud variable has changed some instance of the project is changing it. If it is unchanged there is no instance of the project managing that variable (indicating a free player slot).

Clicking the stop sign, closing the browser or linking that browser tab to another page will all stop the local instance of the project from updating the variable so other instances will see the player slot as no longer busy.

(An instance of the project just means a copy running somewhere. If I run your project my instance will book a slot and if you run it your instance will book another.)

Powered by DjangoBB