<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://miniscript.org/w/index.php?action=history&amp;feed=atom&amp;title=How_to_do_many_things_at_once</id>
	<title>How to do many things at once - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://miniscript.org/w/index.php?action=history&amp;feed=atom&amp;title=How_to_do_many_things_at_once"/>
	<link rel="alternate" type="text/html" href="http://miniscript.org/w/index.php?title=How_to_do_many_things_at_once&amp;action=history"/>
	<updated>2026-04-13T06:39:31Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.0</generator>
	<entry>
		<id>http://miniscript.org/w/index.php?title=How_to_do_many_things_at_once&amp;diff=522&amp;oldid=prev</id>
		<title>JoeStrout: fixed typo</title>
		<link rel="alternate" type="text/html" href="http://miniscript.org/w/index.php?title=How_to_do_many_things_at_once&amp;diff=522&amp;oldid=prev"/>
		<updated>2020-06-03T03:36:02Z</updated>

		<summary type="html">&lt;p&gt;fixed typo&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 03:36, 3 June 2020&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot; &gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The standard way to do this is to have a main loop that runs the whole time your program is running, which does nothing but call other functions to update various things (move sprites, play or stop sounds, do calculations, etc.).  The main loop should call [[yield]] to give the computer a bit of a rest on each frame, and give you a consistent program speed; this will make your main loop run 60 times per second unless your update methods take longer than about 16 milliseconds.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The standard way to do this is to have a main loop that runs the whole time your program is running, which does nothing but call other functions to update various things (move sprites, play or stop sounds, do calculations, etc.).  The main loop should call [[yield]] to give the computer a bit of a rest on each frame, and give you a consistent program speed; this will make your main loop run 60 times per second unless your update methods take longer than about 16 milliseconds.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;As an extra feature, it &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;is &lt;/del&gt;may be useful to calculate the exact time step (often called ''dt'') between iterations of the main loop, and provide this value to your update methods.  They can use this to move things further when the time step is long (for example, because the computer is getting bogged down, or it's running on a slower machine or web browser).  That will give the motion a consistent speed regardless of frame rate.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;As an extra feature, it may be useful to calculate the exact time step (often called ''dt'') between iterations of the main loop, and provide this value to your update methods.  They can use this to move things further when the time step is long (for example, because the computer is getting bogged down, or it's running on a slower machine or web browser).  That will give the motion a consistent speed regardless of frame rate.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Example ==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Example ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>JoeStrout</name></author>
		
	</entry>
	<entry>
		<id>http://miniscript.org/w/index.php?title=How_to_do_many_things_at_once&amp;diff=521&amp;oldid=prev</id>
		<title>JoeStrout: Created page with &quot;MiniScript can only do one thing at a time, but by switching between many tasks very quickly, it can create the illusion of doing many things at once.  (This is in fact how al...&quot;</title>
		<link rel="alternate" type="text/html" href="http://miniscript.org/w/index.php?title=How_to_do_many_things_at_once&amp;diff=521&amp;oldid=prev"/>
		<updated>2020-06-03T03:35:02Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;MiniScript can only do one thing at a time, but by switching between many tasks very quickly, it can create the illusion of doing many things at once.  (This is in fact how al...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;MiniScript can only do one thing at a time, but by switching between many tasks very quickly, it can create the illusion of doing many things at once.  (This is in fact how all computers worked before the advent of multi-processor machines, and even today it is a common technique used in game engines such as Unity.)&lt;br /&gt;
&lt;br /&gt;
The standard way to do this is to have a main loop that runs the whole time your program is running, which does nothing but call other functions to update various things (move sprites, play or stop sounds, do calculations, etc.).  The main loop should call [[yield]] to give the computer a bit of a rest on each frame, and give you a consistent program speed; this will make your main loop run 60 times per second unless your update methods take longer than about 16 milliseconds.&lt;br /&gt;
&lt;br /&gt;
As an extra feature, it is may be useful to calculate the exact time step (often called ''dt'') between iterations of the main loop, and provide this value to your update methods.  They can use this to move things further when the time step is long (for example, because the computer is getting bogged down, or it's running on a slower machine or web browser).  That will give the motion a consistent speed regardless of frame rate.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
Let's prepare a sprite, and define an update method that does a [[How to do simple physics|simple physics simulation]] to make the sprite bounce.&lt;br /&gt;
&amp;lt;ms&amp;gt;ball = new Sprite&lt;br /&gt;
ball.image = file.loadImage(&amp;quot;/sys/pics/XO/O-blue.png&amp;quot;)&lt;br /&gt;
ball.x = 480&lt;br /&gt;
ball.y = 600&lt;br /&gt;
ball.vy = 0&lt;br /&gt;
display(4).sprites.push ball&lt;br /&gt;
ball.update = function(dt)&lt;br /&gt;
	self.vy = self.vy - 1000 * dt&lt;br /&gt;
	self.y = self.y + self.vy * dt&lt;br /&gt;
	if self.y &amp;lt; 100 then self.vy = abs(self.vy)&lt;br /&gt;
end function&amp;lt;/ms&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that the ''ball.update'' function takes a ''dt'' (time step) parameter.  It uses this to calculate how much the ball's vertical speed (''vy'') changes due to gravity, and then uses it again to apply this speed to the ball's position.&lt;br /&gt;
&lt;br /&gt;
Now let's make another update function, that simply draws a counter based on the [[time]] value on the text display.&lt;br /&gt;
&amp;lt;ms&amp;gt;updateCounter = function(dt)&lt;br /&gt;
	counter = round(time)&lt;br /&gt;
	if counter == prevCounter then return&lt;br /&gt;
	text.row = 25&lt;br /&gt;
	print counter&lt;br /&gt;
	globals.prevCounter = counter&lt;br /&gt;
end function&amp;lt;/ms&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function doesn't use the ''dt'' parameter, but it's defined to accept it anyway.  This is convenient when you have a lot of things to update, especially if they are methods on an object, since your main loop can then zip over all such objects and update them all in the same way.&lt;br /&gt;
&lt;br /&gt;
But in this case we have only two things to update; let's make a main loop that updates them.&lt;br /&gt;
&amp;lt;ms&amp;gt;lastTime = time&lt;br /&gt;
while not key.pressed(&amp;quot;escape&amp;quot;)&lt;br /&gt;
	now = time&lt;br /&gt;
	dt = now - lastTime&lt;br /&gt;
	lastTime = now&lt;br /&gt;
	updateCounter dt   // Update the counter&lt;br /&gt;
	ball.update dt     // Update the ball&lt;br /&gt;
	yield&lt;br /&gt;
end while&amp;lt;/ms&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each time through the main loop, we grab the current [[time]] value, subtract the last time from it to calculate the time step ''dt'', and then store the current time as ''lastTime'' for our next iteration.  Then we pass the ''dt'' value to each of our update methods.  Finally, remember to [[yield]] before the end of the loop.&lt;br /&gt;
&lt;br /&gt;
=== Complete Program ===&lt;br /&gt;
Paste this into Mini Micro to see it in action.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ms&amp;gt;clear&lt;br /&gt;
prevCounter = &amp;quot;&amp;quot;&lt;br /&gt;
updateCounter = function(dt)&lt;br /&gt;
	counter = round(time)&lt;br /&gt;
	if counter == prevCounter then return&lt;br /&gt;
	text.row = 25&lt;br /&gt;
	print counter&lt;br /&gt;
	globals.prevCounter = counter&lt;br /&gt;
end function&lt;br /&gt;
&lt;br /&gt;
ball = new Sprite&lt;br /&gt;
ball.image = file.loadImage(&amp;quot;/sys/pics/XO/O-blue.png&amp;quot;)&lt;br /&gt;
ball.x = 480&lt;br /&gt;
ball.y = 600&lt;br /&gt;
ball.vy = 0&lt;br /&gt;
display(4).sprites.push ball&lt;br /&gt;
ball.update = function(dt)&lt;br /&gt;
	self.vy = self.vy - 1000 * dt&lt;br /&gt;
	self.y = self.y + self.vy * dt&lt;br /&gt;
	if self.y &amp;lt; 100 then self.vy = abs(self.vy)&lt;br /&gt;
end function&lt;br /&gt;
&lt;br /&gt;
// Main loop:&lt;br /&gt;
lastTime = time&lt;br /&gt;
while not key.pressed(&amp;quot;escape&amp;quot;)&lt;br /&gt;
	now = time&lt;br /&gt;
	dt = now - lastTime&lt;br /&gt;
	lastTime = now&lt;br /&gt;
	updateCounter dt   // Update the counter&lt;br /&gt;
	ball.update dt     // Update the ball&lt;br /&gt;
	yield&lt;br /&gt;
end while&amp;lt;/ms&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Once you get in the habit of using this pattern, it becomes really easy to do even more things at once: just define an appropriate update method, and call it from the main loop.&lt;br /&gt;
&lt;br /&gt;
=== More Examples ===&lt;br /&gt;
Many of the demos in '''/sys/demo/''' use this pattern, including '''balloons''', '''drumMachine''', '''flappyBat''', '''inputCheck''', '''mochiBounce''', '''platformer''', '''speedConquest''', '''theMatrix''', '''typing''', and '''wumpusTrap'''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Mini Micro]]&lt;br /&gt;
[[Category:How To]]&lt;/div&gt;</summary>
		<author><name>JoeStrout</name></author>
		
	</entry>
</feed>