Discuss Scratch
- gamer270
-
Scratcher
18 posts
"At exit" block
I think it would be nice if there was an at exit block like if somebody were to get on a project and if something could be changed when they got off
- gamer270
-
Scratcher
18 posts
"At exit" block
like a cloud variable or somethingsomething could be changedlike what?
- Zambonifofex
-
Scratcher
100+ posts
"At exit" block
If they've just exited the project, what would run the blocks? .-. This doesn't make sense
- 1234abcdcba4321
-
Scratcher
1000+ posts
"At exit" block
This is the reason why I put full scratchers only in my suggestion.
It would require a check to check if someone is using a project.
It would require a check to check if someone is using a project.
- gamer270
-
Scratcher
18 posts
"At exit" block
If they've just exited the project, what would run the blocks? .-. This doesn't make senseWell, I'm a programmer and with websites if someone tries to get out of the website it will run special commands and then let them exit
I just wish scratch had something like this because I'm making a project that keeps track of how many people are on.
- mariobros406
-
Scratcher
500+ posts
"At exit" block
Hm…I like this idea, however, there is a workaround.
when green flag clicked
forever
reset timer
end
when [timer v] > (0.1)
change [variable v] by (1)
- 1234abcdcba4321
-
Scratcher
1000+ posts
"At exit" block
Hm…I like this idea, however, there is a workaround.when green flag clicked
forever
reset timer
end
when [timer v] > (0.1)
change [variable v] by (1)
*refreshes page*
Haha! I didn't get caught by the filter!
- MCAnimator3D
-
Scratcher
500+ posts
"At exit" block
Yes. The workaround only applies to the stop sign being clicked. If you were to leave the project page, the game would not be running the scripts anymore due to the interruption in the project. Unfortunately, nothing can control the change in the project. However, if you had one computer monitoring the join or leave of a player, they can automatically change a cloud variable to the desired value. It would be silly to keep one computer on all the time just to keep track of the project.
Last edited by MCAnimator3D (Jan. 8, 2014 03:26:25)
- Zambonifofex
-
Scratcher
100+ posts
"At exit" block
Hm… I see, so that how some pages can ask if you really want to close a page, like, on Scratch, if you try to close a unsaved project, it asks if you really want to leave, and warn you that your changes will be lost… But I don't think that will be a good adicion to Scratch, I can't see many uses for that…If they've just exited the project, what would run the blocks? .-. This doesn't make senseWell, I'm a programmer and with websites if someone tries to get out of the website it will run special commands and then let them exit
I just wish scratch had something like this because I'm making a project that keeps track of how many people are on.
Yes. The workaround only applies to the stop sign being clicked. If you were to leave the project page, the game would not be running the scripts anymore due to the interruption in the project. Unfortunately, nothing can control the change in the project. However, if you had one computer monitoring the join or leave of a player, they can automatically change a cloud variable to the desired value. It would be silly to keep one computer on all the time just to keep track of the project.You can have the viewers to check for other viewers…
- Scratchedbcat
-
Scratcher
100+ posts
"At exit" block
same! Support!If they've just exited the project, what would run the blocks? .-. This doesn't make senseWell, I'm a programmer and with websites if someone tries to get out of the website it will run special commands and then let them exit
I just wish scratch had something like this because I'm making a project that keeps track of how many people are on.
- braxbroscratcher
-
Scratcher
1000+ posts
"At exit" block
just make a user change their own timer by 1 while they're online. Then check if the timer's changing, and if it isn't, they're either disconnected or lagging hard.
- _nix
-
Scratcher
1000+ posts
"At exit" block
Actually, that's a special thing that web browsers can do with JavaScript, and it's kind of interesting..If they've just exited the project, what would run the blocks? .-. This doesn't make senseWell, I'm a programmer and with websites if someone tries to get out of the website it will run special commands and then let them exit
I just wish scratch had something like this because I'm making a project that keeps track of how many people are on.
JavaScript is a single-threaded language. That means that all the code you, and websites, run – and usually other page-related browser code, such as scrolling – happen in one process at your computer. Only one thing can happen at a time, mostly. You can either be multiplying a bunch of numbers by two, or removing list items, or clicking something on the page, but only one of those things at a time. Your browser is usually quick enough that you don't notice that it's doing things like that behind the scenes (usually in the site's code), but it is, and only one at a time.
That's critical to the way your web browser runs code when you close a page – it only runs one frame of code. That means it only goes and run whatever scripts you attach to this function:
window.onbeforeunload = function() { // do things }
..and nothing else. If you make something happen later, like this:
window.onbeforeunload = function() { setTimeout(function() { console.log('This runs 1000ms later, riiiiiight?') }, 1000) }
..it actually doesn't run that “console.log”, because the page is closed as soon as “onbeforeunload” (really “setTimeout”) is done being ran. “setTimeout” scheduled something to happen later, but then the page got closed, so it never happened.
Back to Scratch – it's a multi-threaded language (close enough). You can have two scripts running at once; that's basically the same as having two threads. Threads can cause a delay (e.g. “wait 2.5 secs”) but that won't cause your whole browser to freeze, it'll just wait until that's done and then continue the script.
So can you see how it wouldn't really work to run something when the project is closed? Take a look at this:
when project is closed :: events hat
change [cloud var 1 v] by (1)
wait (1) secs
change [cloud var 2 v] by (2)
Only cloud var 1 would be changed, not cloud var 2. That's because before “wait 1 secs” finished, the page was really closed, and with it, Scratch.
And those delays can be more subtle:
when project is closed :: events hat
repeat (5)
change [cloud var 1 v] by (1)
end
Cloud var 1 would only be changed once, since the “repeat” block has a little delay every time it gets to the end. Your best bet would be using a custom block with the “no screen refresh” option enabled, but that's not even perfect, since – I'm pretty sure – it'll give a screen refresh every 5-ish seconds or so.
And, to continue this, if you cause something long to happen in a “no screen refresh” block because you need to put it under a “when project is closed” hat, you're going to make the user have to wait unexpectedly, and that just isn't very good for user experience. You'd be sure to get comments saying “why does this my page lag so much when I close this project??”

Also, finally, there's one last problem directly relating to your idea. You want this block so that you can use it to detect how many users are online – something like this:
when green flag clicked
if <(I'm online) = [0]> then
set [I'm online v] to [1] // so that we only change "users online" once
change [CLOUD users online v] by (1)
end
when project exited :: events hat
change [CLOUD users online v] by (-1)
Suppose there's a situation something like this: two users get on. One force quits their browser (more on this soon), and the other exits the project normally (by closing the Scratch browser tab).
Now, by force quitting the browser, that user didn't run the “when project exited” script. That means that they changed the cloud variable “users online” by 1, but they never decreased it by 1 – so it will always look like there's another user online, according to that cloud variable.
So, you see, you need to rely on a different method to detect the number of users online. There's lots of ways to do this, and I think a few have already been posted here, but now at least you can see why your idea wouldn't work quite perfectly

Sorry that things always seem to be more complicated than they should be.. that's kind of how programming goes, especially with multiplayer projects.
(edit: fix scratchblocks)
Last edited by _nix (Jan. 17, 2017 21:06:23)
- dkim19375
-
Scratcher
17 posts
"At exit" block
Just wait like 3 secs…
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<COOL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>I know it doesn't match
Last edited by dkim19375 (Jan. 3, 2018 01:09:03)
- Charles12310
-
Scratcher
1000+ posts
"At exit" block
Sorry, but the Scratch Team will not add a block that will detect if the user stops a project. Here's why:
1. This breaks the purpose of the Stop Sign. The purpose of the Stop Sign is to stop all playing scripts.
2. Let's say you are making a project and you have a laggy script.
A user wants to get out of the project because it is lagging them. However, you also added this script:
Therefore, the user won't escape from the lag coming from the project.
1. This breaks the purpose of the Stop Sign. The purpose of the Stop Sign is to stop all playing scripts.
2. Let's say you are making a project and you have a laggy script.
when green flag clicked
... // laggy script
A user wants to get out of the project because it is lagging them. However, you also added this script:
when project closes :: events hat
... // same laggy script
Therefore, the user won't escape from the lag coming from the project.
22. When Stop Sign Clicked
This will cause more scripts to start when you want to stop the project.
(source)
Last edited by Charles12310 (Jan. 3, 2018 01:22:42)
- Sheep_maker
-
Scratcher
1000+ posts
"At exit" block
The proper way to detect if a user is offline is to firstly assign each online user a cloud variable, then have each of them constantly change their assigned variable. That way, if they happen to leave (by stopping the project, quitting the browser, or having their computer explode), they will stop changing the value of their assigned variable, and the other online users can detect that and assume that the user is offline.
- Da_king_of_Chocolate
-
Scratcher
2 posts
"At exit" block
have a code always setting a variable to a number. have a sprite always setting it to a different number. when the player leaves the sprite will deactivate causing the first code to set it to the first number.














