<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=Debugging_Scripts</id>
	<title>Debugging Scripts - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://digida.mgpu.ru/index.php?action=history&amp;feed=atom&amp;title=Debugging_Scripts"/>
	<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Debugging_Scripts&amp;action=history"/>
	<updated>2026-05-15T13:33:02Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=Debugging_Scripts&amp;diff=1868&amp;oldid=prev</id>
		<title>Patarakin: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Debugging_Scripts&amp;diff=1868&amp;oldid=prev"/>
		<updated>2022-07-21T08:33:18Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 11:33, 21 июля 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Patarakin</name></author>
	</entry>
	<entry>
		<id>http://digida.mgpu.ru/index.php?title=Debugging_Scripts&amp;diff=1867&amp;oldid=prev</id>
		<title>scratch&gt;Jvvg в 21:46, 6 июня 2022</title>
		<link rel="alternate" type="text/html" href="http://digida.mgpu.ru/index.php?title=Debugging_Scripts&amp;diff=1867&amp;oldid=prev"/>
		<updated>2022-06-06T21:46:49Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{External Programs}}&lt;br /&gt;
A common problem for [[Scratchers]] is when a [[script]] that may seem to be perfect does not do what it is expected to. These are usually small mistakes the user made and can be fixed. This is commonly known as &amp;#039;&amp;#039;&amp;#039;debugging&amp;#039;&amp;#039;&amp;#039;, the term for which was popularized in the 20{{th}} century when Grace Hopper, a computer scientist, removed an actual moth from a machine and said she was &amp;quot;debugging the machine&amp;quot;.&amp;lt;ref&amp;gt;[[wikipedia:Software bug#History]]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;https://www.computerworld.com/article/2515435/moth-in-the-machine--debugging-the-origins-of--bug-.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Inspecting Scripts ==&lt;br /&gt;
Inspecting a script that does not work is often not easy. Finding a problem with a script is difficult because it makes sense to the writer. Often, &amp;#039;&amp;#039;&amp;#039;thinking from the computer&amp;#039;s perspective&amp;#039;&amp;#039;&amp;#039; will highlight the issue. For example, to find the sum of the fractions 1/2+1/3+1/4+…+1/100 one may use the following script:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
set [n v] to [100]&lt;br /&gt;
set [total v] to [0]&lt;br /&gt;
repeat until &amp;lt;(n) &amp;lt; (1)&amp;gt;&lt;br /&gt;
 change [n v] by (-1)&lt;br /&gt;
 change [total v] by ((1) / (n))&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
This will not work, because on inspecting the script, the &amp;quot;n&amp;quot; variable will be equal to 0 on the last iteration (loop/repeat), and will cause a zero division error.&lt;br /&gt;
&lt;br /&gt;
Other issues can occur when actions do take place but are not seen by the user. For example, if a sprite is hidden or has a ghost effect of 100. A practical example is a continuation of the script above:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
say [I have found the sum!]&lt;br /&gt;
say (total)&lt;br /&gt;
say [Wasn&amp;#039;t that great?]&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
The script will just say &amp;quot;Wasn&amp;#039;t that great&amp;quot;, because the previous two actions happen in a split second, or sometimes, within a single render cycle of the evaluator. To fix these problems, either use the [[Wait () Seconds (block)|&amp;lt;sb&amp;gt;wait () seconds&amp;lt;/sb&amp;gt; block]] in the &amp;quot;Control&amp;quot; section between the blocks, or use the [[Say () for () Seconds (block)|&amp;lt;sb&amp;gt;say () for () seconds&amp;lt;/sb&amp;gt; block]] instead.&lt;br /&gt;
&lt;br /&gt;
Sometimes, the error is purely logical and appears sensible only at the moment. These are often the hardest to track down because there is no true &amp;quot;error&amp;quot;. For example, accidentally inserting the wrong variable, or changing the order in subtraction will mess up a script without giving a trace. The best way to find these mistakes is to &amp;#039;&amp;#039;&amp;#039;ask a friend&amp;#039;&amp;#039;&amp;#039;. Walk through the entire program, block-by-block, and make sure the chosen friend understands each step. If there is a logical mistake, they will probably catch it.&lt;br /&gt;
&lt;br /&gt;
== Preventing Errors ==&lt;br /&gt;
* The best way to prevent silly mistakes is to have &amp;#039;&amp;#039;&amp;#039;neat, organized code&amp;#039;&amp;#039;&amp;#039;. Order scripts by their functions or order of evaluation will help find mistakes and prevent them.&lt;br /&gt;
&lt;br /&gt;
* Leave [[comment]]s wherever a lot of time was spent on a script; this way, if it breaks again, they can be referred back to for a glimpse of how the script worked in the past. Anyone with enough experience should be able to understand the script at first glance.&lt;br /&gt;
&lt;br /&gt;
* Do not blindly copy scripts from other projects unless that was the intention of the project. Other than moral copy theft issues, chances are the script will not work in the context of the program. To learn how to make easy, integrative tools, see [[Advanced Clone Usage]].&lt;br /&gt;
&lt;br /&gt;
== Checking Values ==&lt;br /&gt;
If a value needs to be known to determine whether or not a script is functioning, there are a few ways to find it.&lt;br /&gt;
&lt;br /&gt;
Using the [[Say ()]] block. The say block can be used to constantly checking a value.&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
forever&lt;br /&gt;
say (((var1) + (var2)) * (var3)) //Outputs the value of (var1 + var2) * var3&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Updating a variable. A variable can also be updated constantly.&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
forever&lt;br /&gt;
set [check v] to (((var1) + (var2)) * (var3)) //Outputs the value of (var1 + var2) * var3&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These methods can also be used to check a boolean:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
forever&lt;br /&gt;
say &amp;lt;touching [Sprite1 v]?&amp;gt; //Will say whether or not the sprite is touching Sprite1&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Testing Code ==&lt;br /&gt;
Testing a large, complex project is daunting because every change can potentially wreck something.&lt;br /&gt;
&lt;br /&gt;
Often, the best way is to simply run the project again and again. However, large games or animations would be painful to debug like this. Instead, introduce certain testing codes, which can be removed in the final release. For example, to test level 27 of a game, add a script where when &amp;quot;space&amp;quot; is pressed, the game goes to the next level.&lt;br /&gt;
&lt;br /&gt;
To make sure that the code remains functional, automate certain tests. For example, scripts that should return a numerical answer can be automatically tested against some built-in cases:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
define get cube of (5)&lt;br /&gt;
if &amp;lt;not &amp;lt;(result) = (125)&amp;gt;&amp;gt; then&lt;br /&gt;
 say [Oops!] for (5) secs&lt;br /&gt;
 say [error with input 5] for (5) secs&lt;br /&gt;
 stop [all v]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
define test CUBE with (n) expect (k)&lt;br /&gt;
get cube of (n) :: custom&lt;br /&gt;
if &amp;lt;not &amp;lt;(result) = (k)&amp;gt;&amp;gt; then&lt;br /&gt;
 say [Oops!] for (2) secs&lt;br /&gt;
 say (n) for (2) secs&lt;br /&gt;
 stop [all v]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
when gf clicked&lt;br /&gt;
test CUBE with (1) expect (1) :: custom&lt;br /&gt;
test CUBE with (2) expect (8) :: custom&lt;br /&gt;
test CUBE with (3) expect (27) :: custom&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Adding Waits and showing variables==&lt;br /&gt;
A way to debug scripts is to add waits and show variables so things can be seen things step by step.&lt;br /&gt;
&lt;br /&gt;
== Debug List ==&lt;br /&gt;
Creating a Debug list and printing step to step can also help. There is not a best way to debug log. For example, with the fraction addition example listed above:&lt;br /&gt;
&amp;lt;Scratchblocks&amp;gt;&lt;br /&gt;
set [n v] to [100]&lt;br /&gt;
set [total v] to [0]&lt;br /&gt;
repeat until &amp;lt;(n) &amp;lt; (1)&amp;gt;&lt;br /&gt;
 change [n v] by (-1)&lt;br /&gt;
 change [total v] by ((1) / (n))&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/ScratchBlocks&amp;gt;&lt;br /&gt;
To log The variable N and Total to the list Debug, Add to list blocks can be added:&lt;br /&gt;
&amp;lt;Scratchblocks&amp;gt;&lt;br /&gt;
delete all of [debug v]&lt;br /&gt;
set [n v] to [100]&lt;br /&gt;
set [total v] to [0]&lt;br /&gt;
repeat until &amp;lt;(n) &amp;lt; (1)&amp;gt;&lt;br /&gt;
 change [n v] by (-1)&lt;br /&gt;
 change [total v] by ((1) / (n))&lt;br /&gt;
 add (join(join(join(t)(e))(join(s)(t)))(join(9)(8))) to [debug v]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/Scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Common causes of errors ==&lt;br /&gt;
=== Invalid operations ===&lt;br /&gt;
{{main|Script#Script Errors}}&lt;br /&gt;
Often, Scratchers write programs with a script similar to the following:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
set x to (10)&lt;br /&gt;
forever&lt;br /&gt;
change x by (-1)&lt;br /&gt;
say ((1) / (x position))&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
This script will cause an error after the 10th repeat of the forever block, when x = 0, as nothing can be divided by 0 in [[Scratch 1.4]]. (Divison by zero mathematically returns &amp;quot;undefined&amp;quot; or &amp;quot;no solution&amp;quot; while it programmatically returns an error or &amp;quot;NaN&amp;quot;.) When this occurs, the script will be highlighted in red and the ()/() block will turn red. To counter this error, the best way would be to start with x = 10.0001 rather than 10. This way, x will never reach 0, it will hit 0.0001 and -0.9999.&lt;br /&gt;
&lt;br /&gt;
Similar problems occur with square roots, logarithms, and trigonometric functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
set x to (10)&lt;br /&gt;
forever&lt;br /&gt;
change x by (-1)&lt;br /&gt;
say ([sqrt v] of (x position))// this will error when x = -1.&lt;br /&gt;
say ([sqrt v] of ([abs v] of (x position)))// this will not error as abs x is always positive.&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Order of Commands ===&lt;br /&gt;
The most common problem for Scratchers is for the commands to be in the wrong order. For example, to make a [[sprite]] lunge at another sprite, a Scratcher uses commands:&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;&lt;br /&gt;
point in direction (45 v)&lt;br /&gt;
wait (0.5) secs&lt;br /&gt;
glide (2) secs to x: (54) y: (0)&lt;br /&gt;
point in direction (90 v)&lt;br /&gt;
&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
but it does not look like the sprite is lunging.&lt;br /&gt;
&lt;br /&gt;
A good solution for this problem is to just move around the commands until the script works. Ex., to make the sprite lunge, the Scratcher moves the &amp;quot;point in direction 90&amp;quot; command to right after the &amp;quot;wait 0.5 secs&amp;quot; command, thus making the sprite look like it&amp;#039;s lunging:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;point in direction (45 v)&lt;br /&gt;
wait (0.5) secs&lt;br /&gt;
point in direction (90 v)&lt;br /&gt;
glide (2) secs to x: (54) y: (0)&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Timing ===&lt;br /&gt;
Another common problem for Scratchers is the timing of commands. This is often seen in [[project]]s that involve 2 scripts cooperating without [[broadcasts]] or [[variable]]s, such as in the [[Sample Projects|sample project]] [[projects:SampleProjectsTeam/62825|Joke]]. When timing is not perfect the entire project could be thrown off. But do not worry, here are some ways to solve that problem:&lt;br /&gt;
&lt;br /&gt;
==== Broadcasts and Variables ====&lt;br /&gt;
Using broadcasts and variables will allow the scripts to coordinate with each other. Broadcasts involve hat blocks, so be prepared for a lot of scripts. Using variables will save scripts. If variables are used in a project like Joke, the &amp;quot;[[Wait Until () (block)|wait until]]&amp;quot; block will most likely be used. Otherwise the &amp;quot;[[If () Then (block)|if]]&amp;quot; block could be used instead or possibly the &amp;quot;[[Repeat Until ()|repeat until]]&amp;quot; block. Either way, the scripts will be more accurately coordinated.&lt;br /&gt;
&lt;br /&gt;
==== Careful Timing ====&lt;br /&gt;
If broadcasts or variables are strongly unwanted for some reason the amount of time between blocks can be adjusted, by using the &amp;quot;wait () secs&amp;quot; block. This would be easy for a project like Joke but not for a complicated [[Game Projects|game]].&lt;br /&gt;
&lt;br /&gt;
==== Repeated Process ====&lt;br /&gt;
This method is mainly for very complicated games. To make accurate detection, tests should be repeated a few extra times before activating a reaction. It may be unnoticeable but it takes a small amount of time to run a command. If a test block is repeated, it&lt;br /&gt;
will give the detection a little bit longer to change itself. An example of this can be found in [[projects:845502|this project]].&lt;br /&gt;
&lt;br /&gt;
==== Timer ====&lt;br /&gt;
Blocks can also revolve around the [[Timer (value)|timer]]. Scratch&amp;#039;s timer can be reset and manipulated, so much can be done with it.&lt;br /&gt;
&lt;br /&gt;
=== Incompatibility ===&lt;br /&gt;
The third and least common reason for scripts not to work would be that the commands just do not work with each other. When this happens, the script will be highlighted in red. The sprite is &amp;quot;stumped&amp;quot;. When this is the case there really isn&amp;#039;t anything to do about it except to rebuild the script completely, with a different approach.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;when gf clicked&lt;br /&gt;
say [Hello World!] for (2) secs&lt;br /&gt;
if &amp;lt;visible?&amp;gt; then&lt;br /&gt;
say [I see you!] for (1) secs&lt;br /&gt;
end&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Obsolete Blocks ====&lt;br /&gt;
{{main|Obsolete Blocks}}&lt;br /&gt;
When using older versions of [[Scratch]], [[blocks]] may be labeled &amp;quot;obsolete!&amp;quot;. These are usually blocks in the newer versions of Scratch and are not compatible with older versions. Using these blocks in the older versions will often cause the script to fail.&lt;br /&gt;
&lt;br /&gt;
In 2.0:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;when gf clicked&lt;br /&gt;
create clone of [myself v]&lt;br /&gt;
reset timer&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In 1.4:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;scratchblocks&amp;gt;when gf clicked&lt;br /&gt;
obsolete!&lt;br /&gt;
reset timer&amp;lt;/scratchblocks&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Blocks like &amp;quot;say nothing&amp;quot; are hidden in Scratch 1.4, but will not cause a script error.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Script]]&lt;br /&gt;
* [[Scratch Bugs]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Scripting Tutorials]][[Category:Scratch Program]]&lt;br /&gt;
[[de:Debuggen von Skripten]]&lt;/div&gt;</summary>
		<author><name>scratch&gt;Jvvg</name></author>
	</entry>
</feed>