From Test-Scratch-Wiki
(Redirected from Eng:Offline/Online Detection)
Interface detection is when a project determines what player it is running in. For example, 2.0-1.4; player-editor; 1.4-Java-Flash. These take advantage of slight differences between the compared player.
Editor-Player
In order to detect between the project editor and player, you will need an alternate account with the Scratcher status. This involves cloud data and cannot be the creator of the project.
Copy these scripts into your project:
when gf clicked if <(username) = [(insert your username here)]> set [☁detect v] to [0] // you can click the green flag to set it to "editor" stop [other scripts in sprite v] // just confirming that it really is somebody else end when gf clicked set [☁detect v] to [1] // we already know you're not the creator of the project, so since you cannot edit someone else's cloud data when in the editor, it will get reverted to "0" when in the editor, but in the player it will stay "1"! wait (1.5) secs set [☁detect v] to [0] // after you announce it, change it back for the next person when gf clicked forever if <(☁detect) = [0]> set [detect v] to [EDITOR] // create a variable named detect again, but not a cloud variable. end if <(☁detect) = [1]> set [detect v] to [PLAYER] end end when gf clicked say [Detecting.....] for (1) secs // it will get reverted in 0.5 seconds, so say it in 1 second say (detect)
1.4-Java Player
Using these methods, it can be determined whether the player is Scratch 1.4 or the Java Player.
Here is an example of online and offline detection.
Using Obsolete Blocks
The following script requires obsolete blocks to properly check if the viewer was running projects in the offline or online editor.
when gf clicked set [Online/Offline v] to [Online] obsolete! set [Online/Offline v] to [Offline]
How it Works
First the script sets the variable Online/Offline to Online. In the Java Player, it will stop at an obsolete block. Therefore, it sets it to Offline if it is being played in the Experimental Viewer or offline because it will ignore the obsolete block and the script will continue to turn the variable online/offline to offline.
Using Script Errors
when gf clicked set [Online/Offline v] to [Offline] wait ([sqrt v] of (-1)) secs set [Online/Offline v] to [Online]
Other script errors, such as dividing by zero, can also be used.
How it Works
First the script sets the variable, Online/Offline, to Offline. In the Scratch Program (offline) it will not continue the script when it reaches the script error but online it will continue to run the script.
1.4-Java-Flash
This script is similar to the previous ones, but it additionally checks whether the project is playing in the modern Flash Player.
when gf clicked set [Playing... v] to [Offline] broadcast ([sqrt v] of (-1)) set [Playing... v] to [Java] obsolete! set [Playing... v] to [Flash]
How it Works
First, the script sets the variable "Playing..." to "Offline". Offline the script stops immediately. However, in the Java Player, the script ignores the error and carries on the script, therefore setting the variable to "Java". The Experimental Viewer and Flash Player will continue with the script no matter how many errors so it will then set the variable to "Flash".
Similarly, a script could be made that checks "Java", "offline", or "Flash". It works by first setting the variable to Java. If it is on Java, then it will stop there because of the obsolete block. Then if it is offline it will make it through the obsolete block but stop at the script error. Then if it is on Flash it will run through all of it setting it to Flash.
Example Uses
There are many reasons a user may want to detect if a project is online or offline. For example, you may want to automatically suggest the user to download the project (or use the Flash player) in case the project requires Turbo Mode. It is also useful to prompt a user to download and remix a project, or to disable certain features online which may not work properly. However, you can make a user download the project to play it by disconnecting a vital script.
- Only showing a sprite online
when gf clicked hide if <((0) / (0)) = (0)> then end show
- Changing the appearance of a sprite for online/offline
when gf clicked switch to costume [costume v] if <((0) / (0)) = (0)> then end switch to costume [costume2 v]
- Disallowing a feature online
when gf clicked if <(Online?) = [True]> then stop [this script v] end say [Welcome to the bonus level!] repeat (50) turn cw (5) degrees end
- Reporting where the user is playing the project
when this sprite clicked if <(Online?) = [True]> then say [You are on the internet!] else say [You are not on the internet!] end