Discuss Scratch

BeaconStudiosMaster2
Scratcher
7 posts

A way to find the number of people currently viewing a project

This is a way to monitor concurrent viewers (the amount of different people viewing the project at that moment)
Note: You can change the when green flag clicked button to something else if you wanted.

when green flag clicked
set [index v] to [0]
repeat (length of [list v] :: list)
change [index v] by (1)
if <not <(item (index) of [list v] :: list) = (username)>> then
add (username) to [list v]
change [☁ concurrent viewers v] by (1)
end

and to detect when the project stops:

when green flag clicked
forever
reset timer
end
when [timer v] > (0)
delete (item # of (username) in [list v]) of [list v]
change [☁ concurrent viewers v] by (-1)

But if you're using the timer for other purposes, here's an alternative:

when green flag clicked
forever
set [Time v] to (timer)
end

when [timer v] > (Time)
delete (item # of (username) in [list v]) of [list v]
change [☁ concurrent viewers v] by (-1)

The only problem with it is that it can't detect if the user closed the tab.

Last edited by BeaconStudiosMaster2 (Yesterday 22:54:53)

AsherColeman1234
Scratcher
1 post

A way to find the number of people currently viewing a project

tidelgl
Scratcher
100+ posts

A way to find the number of people currently viewing a project

thanks
PowPro1502
Scratcher
100+ posts

A way to find the number of people currently viewing a project

Make an inactivity timeout. If you're offline for over 15 seconds, you get a warning and have to press a key to go online otherwise the project stops and you need to restart
BeaconStudiosMaster2
Scratcher
7 posts

A way to find the number of people currently viewing a project

to make an inactivity checker, you can do the following:

when green flag clicked
set [inactive? v] to (0)
forever
if <<key [any v] pressed?> or <mouse down?>> then // this checks for inputs and updates a variable to the last time an input was made
set [lastInputTime v] to (days since 2000)
end
if <((days since 2000) - (lastInputTime)) > [0.00018]> then // this checks how much time has passed since an input was made by comparing the variable to the current time, the 0.00018 is roughly 15 seconds
set [inactive? v] to (1)
end
end

After that, you can do whatever you want with it. It also works if you go off of the tab, but not if you stop the project or close the tab. You could also use the timer for this, but it's already used for other purposes in most projects.

Last edited by BeaconStudiosMaster2 (Yesterday 22:58:04)

purplebunny63
Scratcher
100+ posts

A way to find the number of people currently viewing a project

BeaconStudiosMaster2 wrote:

to make an inactivity checker, you can do the following:

when green flag clicked
set [inactive? v] to (0)
forever
if <<key [any v] pressed?> or <mouse down?>> then//This conditional checks for inputs and updates a variable to the last time an input was made
set [lastInputTime v] to (days since 2000)
end
if <((days since 2000) - (lastInputTime)) > [0.00018]> then//this checks how much time has passed since an input was made by comparing the variable to the current time, the 0.00018 is roughly 15 seconds
set [inactive? v] to (1)
end
end

After that, you can do whatever you want with it. It also works if you go off of the tab, but not if you stop the project or close the tab. You could also use the timer for this, but it's already used for other purposes in most projects.

Fixed
BeaconStudiosMaster2
Scratcher
7 posts

A way to find the number of people currently viewing a project

whoops (oh yea i also fixed the concurrent players code since it had a bunch of errors)
awesome-llama
Scratcher
1000+ posts

A way to find the number of people currently viewing a project

Most users won't stop the project in your intended way. The number of users stored in a cloud variable will not be reliably maintained.

A more robust way of doing it is letting the users count the number themselves. The cloud variable can be used for communication rather than storage. All users send out a unique ID of themselves and everyone can count the number of unique IDs seen. Those that haven't been seen for a while can be assumed to be no longer using the project.

Example project: https://scratch.mit.edu/projects/746127076/

Powered by DjangoBB