<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BibleClinger</title>
    <description>The latest articles on DEV Community by BibleClinger (@bibleclinger).</description>
    <link>https://dev.to/bibleclinger</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1347627%2F35a3da4b-aee4-4ad9-aa6b-a1632a6e273d.jpg</url>
      <title>DEV Community: BibleClinger</title>
      <link>https://dev.to/bibleclinger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bibleclinger"/>
    <language>en</language>
    <item>
      <title>Hunting for a Bug in a Prototype Project</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Tue, 02 Sep 2025 18:52:05 +0000</pubDate>
      <link>https://dev.to/bibleclinger/hunting-for-a-bug-in-a-prototype-project-2gd2</link>
      <guid>https://dev.to/bibleclinger/hunting-for-a-bug-in-a-prototype-project-2gd2</guid>
      <description>&lt;p&gt;Every now and then, you have to really wonder how you ended up in a situation where you're hunting for a bug that seems impossible to be happening.&lt;/p&gt;

&lt;p&gt;I had decided to submit some pull requests to the &lt;a href="https://github.com/JoeStrout/MS2Prototypes" rel="noopener noreferrer"&gt;prototype project&lt;/a&gt; that Joe Strout is working on for MiniScript 2.0. Among the components of the prototype are an assembly language, an assembler, and a VM to run the assembled byte code. (The latest benchmarks are very promising, by the way, indicating that it is very much possible to get MiniScript into a new era of high speed scripting.)&lt;/p&gt;

&lt;p&gt;Now I already had added a sample factorial assembly program, as well as support in both the assembler and VM for MULT and DIV opcodes. I thought I was getting a quick sense for how the prototype was designed, and what I would need to do to add anything further. Redspark, from the MiniScript Discord, began adding support for more comparison opcodes to the VM. Today, I added support in the assembler for these same comparison opcodes, but in doing so, this is where I came across the unexpected bug.&lt;/p&gt;

&lt;p&gt;I wrote an assembly program to test the opcodes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# A program that tests the comparison operators.
#  0 -&amp;gt; Working
# -1 -&amp;gt; Not working

@main:
    LOAD r0, 0
    LOAD r1, 1

    IFLT r1, r0, error # Testing &amp;lt;
    IFEQ r0, r1, error # Testing ==
    IFNE r0, r0, error # Testing !=
    IFLE r1, r0, error # Testing &amp;lt;=
    RETURN
error:
    LOAD r0, -1
    RETURN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program should return 0, but it was returning -1, and only due to the line with IFEQ. The new IFEQ opcode was supposed to test if two registers are equal, and if so, jump to the given label. In this case, it was jumping when the results were clearly NOT equal.&lt;/p&gt;

&lt;p&gt;Where to start?&lt;/p&gt;

&lt;p&gt;Well, the assembly program was very simple, and it was in fact correct.&lt;/p&gt;

&lt;p&gt;I checked the code changes I made to the assembler and couldn't find anything wrong. I checked RedSpark's changes to the VM, and I couldn't find anything wrong there either. I began adding some debug print statements -- quick-and-dirty bug fixing isn't pretty and certainly not proper, but it seemed overkill to start figuring things out with a debugger at this point.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxahzdbqiz6bzjzvfnth7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxahzdbqiz6bzjzvfnth7.png" alt="Output from VM 1" width="347" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, this wasn't right.&lt;/p&gt;

&lt;p&gt;Why was it executing IFLT instead of IFEQ? I saw two main possibilities. Either:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the assembler was translating the wrong opcode; or else&lt;/li&gt;
&lt;li&gt;the VM was executing the wrong opcode.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Again, I went over both my changes and RedSpark's changes, but I still couldn't find anything wrong. I needed more information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd0p10iyduebgmzrtf4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd0p10iyduebgmzrtf4t.png" alt="Output from VM 2" width="348" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, this was even more puzzling: the opcodes being produced by my code in the assembler were correct, but they were not matching what the VM was executing. I added even more debug printing in the assembler, and turned on the existing &lt;code&gt;debug&lt;/code&gt; variable in the VM to get even more output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fryo3fbgtyfyswhfmt8id.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fryo3fbgtyfyswhfmt8id.png" alt="Output from VM 3" width="356" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I commented out the offending line in the assembly code, and saw the above output. My code was emitting opcodes 7, 9, and 10 correctly, but they were somehow getting to the VM all as opcode 7! Opcode 7 was IFLT, the original comparison opcode. This meant all of the comparison opcodes were somehow being run by the VM as IFLT. Somewhere in between my output from the assembler and the VM's execution, something was going awfully wrong. It was as if something was rewriting my output...&lt;/p&gt;

&lt;p&gt;... and that's exactly what was happening. A little bit of background first, though. All of these comparison opcodes take 3 arguments: 2 registers for comparison, and then an offset to jump if the comparison is true. The offset can be either an immediate value (ie. &lt;code&gt;IFEQ r0, r1, 2&lt;/code&gt;), or it can take a label (ie. &lt;code&gt;IFEQ r0, r1, someLabel&lt;/code&gt;). In this case, I was using a label (ie. "error"). When you use a label, it has to be translated into the other format during assembly.&lt;/p&gt;

&lt;p&gt;The bug was actually occurring during the assembler's 2nd pass. When it was translating the label into a direct offset, it was also forcefully rewriting the opcode associated with the line. This part of the code ensured that IFLT was &lt;em&gt;the only&lt;/em&gt; comparison opcode that was being sent off in the final rewrite of the line. Since IFLT was the only comparison opcode around when this part of the project was written, there were no consequences until people decided to start adding more opcodes.&lt;/p&gt;

&lt;p&gt;Programmers often say that programming is about thinking outside of the box. Troubleshooting can be that way, too. I had started with the assumption that the error was most likely going to be in the new code, but it's worth considering that an error can be in existing code that held to assumptions that aren't true anymore.&lt;/p&gt;

&lt;p&gt;Hopefully we see MiniScript 2.0 soon!&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>miniscript2</category>
      <category>troubleshooting</category>
      <category>bug</category>
    </item>
    <item>
      <title>Timer for MiniScript</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Tue, 29 Jul 2025 22:29:02 +0000</pubDate>
      <link>https://dev.to/bibleclinger/timer-for-miniscript-2io5</link>
      <guid>https://dev.to/bibleclinger/timer-for-miniscript-2io5</guid>
      <description>&lt;p&gt;If you try to write a game for &lt;a href="https://miniscript.org/MiniMicro/index.html#about" rel="noopener noreferrer"&gt;Mini Micro&lt;/a&gt;, you'll eventually find the need to write timer code. Perhaps you want the player to shoot projectiles of some sort, but you want to limit the rate of fire. Perhaps you want a bomb to explode after 60 seconds. No matter the case, a timer is going to be what you need if you're trying to do things after a set amount of time.&lt;/p&gt;

&lt;p&gt;The essence of a timer is simple. You can do it by storing the amount of time left in the timer, and then subtracting the amount of time passed with each frame from that. This requirement of getting the time passed with each frame means you require a game loop, &lt;a href="https://dev.to/bibleclinger/mini-micro-and-the-game-loop-4aog"&gt;which I wrote about already&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I wrote a Timer class in &lt;a href="https://github.com/BibleClinger/bclib" rel="noopener noreferrer"&gt;my MiniScript library bclib&lt;/a&gt;. This has proven to be very useful to me during my two recent game jams.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Usage
&lt;/h2&gt;

&lt;p&gt;A simple use of it would be as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"bclib"&lt;/span&gt;

&lt;span class="n"&gt;onTimeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"Hello world!"&lt;/span&gt;
    &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quit&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;

timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bclib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Timer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@onTimeout&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bclib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program waits 5 seconds and then prints "Hello world!" to the screen. Simple!&lt;/p&gt;

&lt;p&gt;The arguments to &lt;code&gt;Timer.Create&lt;/code&gt; in this example are simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The amount of time in seconds before the callback function should be called.&lt;/li&gt;
&lt;li&gt;A reference to the callback function, or a map that contains a function called &lt;code&gt;onTimeout&lt;/code&gt;. This is what will be called when the timer goes off.&lt;/li&gt;
&lt;li&gt;Whether or not the timer is considered started. We want it active right away.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are other options as well, but this is as simple as it gets.&lt;/p&gt;

&lt;p&gt;As for the last two lines, &lt;code&gt;Engine&lt;/code&gt; merely supplies the mechanism for the game loop, as I wrote in the article previously mentioned. Our callback function quits the program by calling &lt;code&gt;engine.quit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By the way, you don't need to use &lt;code&gt;Engine&lt;/code&gt; in order to use &lt;code&gt;Timer&lt;/code&gt;, but it does make your life easier in my opinion. Otherwise, you need to call &lt;code&gt;Timer.tick&lt;/code&gt; on every frame yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Another Example
&lt;/h2&gt;

&lt;p&gt;Let's say you have a class called Fighterjet that can fire missiles. You want to have a 5 second cooldown between missile shots for balancing. How might that look?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;Fighterjet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;{}&lt;/span&gt;

&lt;span class="n"&gt;Fighterjet&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
    instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;
    &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canFireMissile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timerCooldownMissile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bclib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Timer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@instance&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;

Fighterjet.fire&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canFireMissile&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;canFireMissile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timerCooldownMissile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Fire&lt;/span&gt; &lt;span class="n"&gt;missile&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;

Fighterjet.onTimeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timer&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;canFireMissile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;

Fighterjet.tick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timerCooldownMissile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tick&lt;/span&gt; &lt;span class="n"&gt;delta&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's assume you have an external mechanic that calls &lt;code&gt;Fighterjet.fire&lt;/code&gt; on your Fighterjet instance when the player attempts to fire a missile. Let's further assume that you have an external source (such as &lt;code&gt;Engine&lt;/code&gt;) calling &lt;code&gt;Fighterjet.tick&lt;/code&gt; every frame.&lt;/p&gt;

&lt;p&gt;In this case, we have a variable, &lt;code&gt;canFireMissile&lt;/code&gt; that determines if the player can fire the missile. We originally have it set to true. When the player fires, the variable is set to false, and the timer is started. The player is unable to fire another missile until the timer goes off, and the &lt;code&gt;Fighterjet.onTimeout&lt;/code&gt; function is called.&lt;/p&gt;

&lt;p&gt;As long as &lt;code&gt;Fighterjet.tick&lt;/code&gt; is being called every frame, this design should be fairly solid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Timers are essential for many games, if not most. You should definitely have a Timer class in your arsenal of coding tricks. The library I'm working on is still under development, but feel free to check out the &lt;a href="https://github.com/BibleClinger/bclib/wiki/Timer" rel="noopener noreferrer"&gt;Timer page on the Github wiki&lt;/a&gt; for the latest API documentation.&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>minimicro</category>
      <category>bclib</category>
      <category>timer</category>
    </item>
    <item>
      <title>Mini Micro and The Game Loop</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Tue, 29 Jul 2025 16:22:10 +0000</pubDate>
      <link>https://dev.to/bibleclinger/mini-micro-and-the-game-loop-4aog</link>
      <guid>https://dev.to/bibleclinger/mini-micro-and-the-game-loop-4aog</guid>
      <description>&lt;p&gt;Sometimes the lack of threads in MiniScript makes programmers wonder how to do multiple things. The MiniScript wiki has &lt;a href="https://miniscript.org/wiki/How_to_do_many_things_at_once" rel="noopener noreferrer"&gt;a good example&lt;/a&gt; of how to do this very thing. I'd like to bring your attention to this particular block of code from that example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pressed&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"escape"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
    &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lastTime&lt;/span&gt;
    &lt;span class="n"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;
    &lt;span class="n"&gt;updateCounter&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt;   &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Update&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;
    &lt;span class="n"&gt;ball&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt;     &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Update&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;ball&lt;/span&gt;
    &lt;span class="n"&gt;yield&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The way singlethreaded games work is to employ a single loop just like this one, often called "the main loop" or "the game loop." In the very simplest implementation, you're supposed to be doing all of your game calculations for one frame exactly within the body of the loop, and then sleep for the rest of the frame.&lt;/p&gt;

&lt;p&gt;In this example, we need to update both a counter &lt;em&gt;and&lt;/em&gt; the ball for each frame. This is handled in the loop by the two lines with comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Engine class
&lt;/h2&gt;

&lt;p&gt;The above loop is easy enough code to write, but it can be tedious to write it over and over again as boilerplate in a lot of programs. Also, sometimes you might have more complicated scenarios where you would like a bit more control over what gets updated without having to write all of that out again.&lt;/p&gt;

&lt;p&gt;I wrote a class for this that I simply called Engine as part of &lt;a href="https://github.com/BibleClinger/bclib" rel="noopener noreferrer"&gt;my MiniScript library called bclib&lt;/a&gt;. It provides a mechanism to handle a game loop and provide updates to each object/function that it is given. It can also handle adding and removing from the update list during runtime.&lt;/p&gt;

&lt;p&gt;The above code block can be replaced with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"bclib"&lt;/span&gt;

&lt;span class="n"&gt;checkEsc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pressed&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"escape"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt; &lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quit&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;

engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bclib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="x"&gt;([&lt;/span&gt;&lt;span class="nd"&gt;@updateCounter&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ball&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@checkEsc&lt;/span&gt;&lt;span class="x"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example Breakdown
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Engine.Create&lt;/code&gt; accepts a list. The elements of the list are simply either standalone functions, or else (perhaps preferably) maps that have a &lt;code&gt;tick&lt;/code&gt; or &lt;code&gt;update&lt;/code&gt; function (searched for in that order). You essentially hand it your code to deal with keyboard input, graphics changes, physics input -- whatever you want your game to do.&lt;/p&gt;

&lt;p&gt;In this case, we need two main updates to occur: the &lt;code&gt;updateCounter&lt;/code&gt; function and the &lt;code&gt;ball.update&lt;/code&gt; function. We pass a function reference directly to &lt;code&gt;updateCounter&lt;/code&gt;, but we pass &lt;code&gt;ball&lt;/code&gt; directly, since &lt;code&gt;Engine&lt;/code&gt; will look for and find its &lt;code&gt;update&lt;/code&gt; function. In order to also maintain the condition that the escape key will cause the program to exit, the function &lt;code&gt;checkEsc&lt;/code&gt; is written here, and a function reference to it is passed in to &lt;code&gt;Engine.Create&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;engine.run&lt;/code&gt; is where the main loop happens. It blocks while it is running. When &lt;code&gt;engine.quit&lt;/code&gt; is called, the engine ceases operations once that entire frame cycle has completed, and &lt;code&gt;engine.run&lt;/code&gt; returns.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other features
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Engine&lt;/code&gt; also looks for an &lt;code&gt;init&lt;/code&gt; function belonging to any update object. This allows for any object initialization that needs to be done. This function is entirely optional, and does not need to be provided. Similarly, it looks for an optional &lt;code&gt;deinit&lt;/code&gt; function on any objects that are removed -- or that are still present after &lt;code&gt;Engine.quit&lt;/code&gt; has been called.&lt;/p&gt;

&lt;p&gt;For more information, please see the &lt;a href="https://github.com/BibleClinger/bclib/wiki/Engine" rel="noopener noreferrer"&gt;Engine wiki page&lt;/a&gt; at the github. As the time of this writing, it is still a work in progress, as the library is still being worked on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In order to do multiple things at once in Mini Micro, programmers need a main loop. The &lt;code&gt;Engine&lt;/code&gt; class in the bclib library provides the mechanism to maintain a main loop.&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>minimicro</category>
      <category>bclib</category>
    </item>
    <item>
      <title>Reflections on Using bclib for Micro Jam #043</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Wed, 16 Jul 2025 12:17:28 +0000</pubDate>
      <link>https://dev.to/bibleclinger/reflections-on-using-bclib-for-micro-jam-043-gfd</link>
      <guid>https://dev.to/bibleclinger/reflections-on-using-bclib-for-micro-jam-043-gfd</guid>
      <description>&lt;p&gt;This last weekend, I participated in &lt;a href="https://itch.io/jam/micro-jam-043" rel="noopener noreferrer"&gt;Micro Jam #043&lt;/a&gt; in which participants had 48 hours to make a video game. The theme for this particular session, open to interpretation, was Colors. The prerequisite was that the player must not stop moving. I wrote my entry in &lt;a href="https://miniscript.org/" rel="noopener noreferrer"&gt;MiniScript&lt;/a&gt; for &lt;a href="https://miniscript.org/MiniMicro/index.html#about" rel="noopener noreferrer"&gt;Mini Micro&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Having done two previous game jams last year, I figured this could be another case of stress and deadlines where I might spend more time on the engine than the actual game. In order to mitigate that, I decided to work on &lt;a href="https://github.com/BibleClinger/bclib" rel="noopener noreferrer"&gt;my MiniScript library bclib&lt;/a&gt; ahead of time. When that was done, I used it to implement Pong as a test, which was simple enough that I could make a basic implementation in an afternoon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1oeth2rjhs3r9rr6d1m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi1oeth2rjhs3r9rr6d1m.png" alt="A screenshot of Insanity Pong" width="800" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Despite some wonky physics in my Pong clone, I decided my library and I were both ready to venture into Micro Jam #043.&lt;/p&gt;

&lt;h2&gt;
  
  
  Color Smash Tank
&lt;/h2&gt;

&lt;p&gt;I settled on making a game inspired by an old, arcade classic -- &lt;a href="https://en.wikipedia.org/wiki/Smash_TV" rel="noopener noreferrer"&gt;Smash TV&lt;/a&gt;. My game is a top-down shooter, where you play as a runaway tank that can turn left or right. Different colored shapes would come out and attack the tank. The player would need to load the correct ammo (associated by color) to eliminate the enemies. I named it Color Smash Tank.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpnrja57tj83jvlofk2q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpnrja57tj83jvlofk2q.png" alt="Screenshot of Color Smash Tank" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reflections
&lt;/h2&gt;

&lt;p&gt;I went into coding battle with an arsenal of classes that proved entirely useful to me. The main four I'll address are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enum&lt;/li&gt;
&lt;li&gt;Timer&lt;/li&gt;
&lt;li&gt;TextLabel&lt;/li&gt;
&lt;li&gt;Machine&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Enum
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/bibleclinger/a-miniscript-enum-class-4pei"&gt;I wrote about this one previously.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While using this class did help me easily keep track of enumerations, I learned that I needed to be careful about similarly named types. &lt;code&gt;eState.GAMEOVER&lt;/code&gt; could be confused for &lt;code&gt;eEvent.GAMEOVER&lt;/code&gt;, despite being entirely different enum types altogether. Thankfully, I caught this bug fairly early when &lt;em&gt;nothing&lt;/em&gt; happened instead of &lt;em&gt;something&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timer
&lt;/h3&gt;

&lt;p&gt;If you are developing for Mini Micro, you should probably have a Timer class in your arsenal. I had been writing timer code manually in MiniScript previously. This library gave me the freedom to spin up a timer whenever it made sense.&lt;/p&gt;

&lt;p&gt;One interesting thing that I learned is that, in MiniScript, inner functions are very powerful as callbacks. I hadn't been sure when I had designed the Timer class if I wanted to use callback functions or callback objects -- so I decided to support both.&lt;/p&gt;

&lt;h3&gt;
  
  
  TextLabel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/bibleclinger/textlabel-for-mini-micro-a0n"&gt;I wrote about this one also previously.&lt;/a&gt; Its API has changed a bit, and more changes will probably be on the way in the near future. I took advantage of &lt;a href="https://dev.to/bibleclinger/does-miniscript-need-variadic-functions-53a0"&gt;maps as parameters&lt;/a&gt; for &lt;code&gt;TextLabel.Create&lt;/code&gt;'s parameters, which were growing in number as I was adding more features.&lt;/p&gt;

&lt;p&gt;What I learned during the game jam was that I desperately needed alignment options for these TextLabel instances to make the most of them. For example, if you want text centered in the middle of the screen, but you are writing messages of different lengths to the same row and column, then what was once centered in the screen may not be centered later on.&lt;/p&gt;

&lt;p&gt;Unfortunately this was something I couldn't address during development of my entry. My text was a bit misaligned, but the TextLabel class proved very handy to me. With alignment added, the next game jam should be even easier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Machine
&lt;/h3&gt;

&lt;p&gt;Over a year ago, I wrote &lt;a href="https://bibleclinger.itch.io/axr-100-1" rel="noopener noreferrer"&gt;AXR-100: Operation First Wave&lt;/a&gt; which was my first game jam entry in MiniScript for Mini Micro. I ended up writing a lot of state code for it by hand, and it was very messy to me. Without timers, enums, or a state machine, this meant there was a lot of repeat code in order to do similar things all the time.&lt;/p&gt;

&lt;p&gt;With the implementation of this state machine class, I was able to model small state machines quite easily in MiniScript. It scaled well enough that I was able to model the basic lifecycle of the game itself as states, such as loading, displaying the title screen, actually playing the game, and displaying a game over message.&lt;/p&gt;

&lt;p&gt;What I learned during the game jam is that the code that I supply for my states can be rather complicated. States, transitions, and events should definitely be well thought out -- and indeed this library made it very easy for me to describe what I wanted -- but &lt;em&gt;after&lt;/em&gt; that I need also to do my best to keep my state code neat and tidy. Although this might be obvious in theory, in practice this requires the programmer to think very carefully about design choices, such as how certain game objects will interact with other game objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;If you'd like to play the game, you can do so here: &lt;a href="https://bibleclinger.itch.io/color-smash-tank" rel="noopener noreferrer"&gt;https://bibleclinger.itch.io/color-smash-tank&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're developing a game for Mini Micro, and feel that these libraries might help you, feel free to check out the library at: &lt;a href="https://github.com/BibleClinger/bclib" rel="noopener noreferrer"&gt;https://github.com/BibleClinger/bclib&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Just please be aware that this library is still under development and may change as time goes on.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>microjam</category>
      <category>miniscript</category>
      <category>minimicro</category>
      <category>bclib</category>
    </item>
    <item>
      <title>TextLabel for Mini Micro</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Sun, 29 Jun 2025 08:55:43 +0000</pubDate>
      <link>https://dev.to/bibleclinger/textlabel-for-mini-micro-a0n</link>
      <guid>https://dev.to/bibleclinger/textlabel-for-mini-micro-a0n</guid>
      <description>&lt;p&gt;I tested &lt;a href="https://selfish-dev.itch.io/manduka" rel="noopener noreferrer"&gt;a Mini Micro video game currently being developed for a game jam&lt;/a&gt;. A TextDisplay was being used as a combination HUD and debug screen. The health being displayed started at 100%. This was intuitive and made sense. After taking some damage, however, I noticed the player's health became "97%%".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiuitjw8nhlufig76c014.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiuitjw8nhlufig76c014.png" alt="A screenshot of the game in question." width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not only that, but other oddities are happening. "Ammo" is written as "%mmo", for example.&lt;/p&gt;

&lt;p&gt;What's going on?&lt;/p&gt;

&lt;h2&gt;
  
  
  Mini Micro's TextDisplay
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://miniscript.org/wiki/TextDisplay" rel="noopener noreferrer"&gt;Mini Micro's TextDisplay&lt;/a&gt; is a graphics layer specifically for text only. It is a two-dimensional grid of cells, with each cell accepting a single character. This grid has 68 columns and 26 rows. When you print to a TextDisplay, the TextDisplay prints the string character by character, starting at the row and column that it has stored internally. The starting row and column can be altered easily by the game, which is what is happening in order to print the HUD.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpwhm61urs56tvkcnxnv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpwhm61urs56tvkcnxnv.png" alt="TextDisplay visual example" width="800" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's consider the above screenshot, taken from the MiniScript wiki. If one were to set the TextDisplay's column and row to 30 and 20 respectively and print "Goodbye!" in this state, you would see "Goodbye!orld!" printed on the screen. Printing "Goodbye!" doesn't erase all of the text of "Hello World!" Printing only overwrites the cells that are in its way. In this case, we are only partially overwriting what is on the screen, leaving a mix of new text and old text.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;In the game, this same issue was happening. "100%" is 4 characters. "97%" is only 3 characters. The 4th character from "100%" (which is the '%' character) is never overwritten when "97%" is printed. This results in the extraneous percent character being left on the screen.&lt;/p&gt;

&lt;p&gt;"%mmo" is being written because originally it's written as "Ammo" -- but the string "ZomFrogs left: 100%" line is written chronologically &lt;em&gt;after&lt;/em&gt;. When there isn't enough room to write that final '%' character, it gets written to the next line, obliterating the 'A' in "Ammo".&lt;/p&gt;

&lt;h2&gt;
  
  
  A Worse Problem
&lt;/h2&gt;

&lt;p&gt;These are fairly easy for a player to spot as errors, but what happens when you are dealing with pure numbers? This is where things get much worse.&lt;/p&gt;

&lt;p&gt;Imagine if the player must manage the temperature of a critical piece of equipment in a game. Let's say that 150 degrees (in whatever unit) would be too hot, and the player must act quickly and cool the equipment whenever it is reaching this temperature. If it goes over 150 for too long, the facility is in danger, and the player must abandon the equipment and escape.&lt;/p&gt;

&lt;p&gt;Now let's further say the player sees "142" printed to the HUD. He recognizes that he must correct the issue by cooling the equipment. He does this successfully by bringing the temperature down to 98 degrees. When the HUD is updated, however, "98" is only 2 characters, whereas "142" is 3 characters. Printing "98" on the same row and column will result in a temperature being displayed of 982 degrees! The player might be convinced that he somehow failed and that he needs to escape immediately. Yet, internally, the game logic is treating the temperature as if it's 98 degrees, well within tolerance.&lt;/p&gt;

&lt;p&gt;This sort of mismatch between the internal state of the game vs the feedback given to the player will confuse players endlessly, since it won't always be so clear to them whether the feedback they are receiving is a visual bug or not. If they &lt;em&gt;are&lt;/em&gt; able to tell, then this could simply be a major source of frustration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;p&gt;We have a number of potential solutions. We could clear the TextDisplay before we write our data, thereby erasing all text and starting fresh. This is a valid solution in many cases, but it can be a bit expensive in terms of performance to erase the TextDisplay every frame of a game when we really only want to erase parts of it.&lt;/p&gt;

&lt;p&gt;We could import the &lt;code&gt;textUtil&lt;/code&gt; module (already included in &lt;code&gt;/sys/lib&lt;/code&gt;) and use &lt;code&gt;clearRow&lt;/code&gt; or &lt;code&gt;clearRect&lt;/code&gt; to clear a limited amount of screen space. This is perhaps a better solution in that you're erasing less than the entire screen, but this requires you to keep track of each area that you need to clear.&lt;/p&gt;

&lt;p&gt;A third solution could be to write the same amount of characters each time no matter what. Health, for example, in the original game mentioned, would &lt;em&gt;always&lt;/em&gt; be written as 4 characters (assuming, of course, that 4 is truly the maximum length it could be). Ninety seven percent would then really be written as "97% " with an extra space appended. This is rather nifty, and relatively efficient if you're dealing with smaller strings whose max lengths you already know.&lt;/p&gt;

&lt;h2&gt;
  
  
  TextLabel
&lt;/h2&gt;

&lt;p&gt;TextLabel is a new class I've added to &lt;a href="https://github.com/BibleClinger/bclib" rel="noopener noreferrer"&gt;my Mini Micro library&lt;/a&gt; to address the underlying issue. As it is brand new, it is subject to change in the API, but the concept behind it should remain the same. It tracks where you want to print, and it knows how many characters you wrote the last time, thereby handling the erasure of previous text.&lt;/p&gt;

&lt;p&gt;The current implementation, as of July 29, 2025, could be used simply like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "bclib"

health = bclib.TextLabel.Create(text, 55, 25)
health.print "100%"
health.print "97%"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv1e7xot5lmr7pztiosrj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv1e7xot5lmr7pztiosrj.png" alt="Screenshot of TextLabel being updated" width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Line 1 is just importing the library.&lt;/p&gt;

&lt;p&gt;The next line creates the label. It binds to the TextDisplay pointed to by &lt;code&gt;text&lt;/code&gt;, which by default points to the default TextDisplay used by Mini Micro on startup. If you have a custom TextDisplay that you created, you can bind it to that.&lt;/p&gt;

&lt;p&gt;The next line prints the message at column 55, row 25 of the TextDisplay pointed to by &lt;code&gt;text&lt;/code&gt;, and the line after that updates the text to read "97%". Because it knows the last message was 4 characters long, and the current message being printed is only 3 characters long, it knows to pad the rest of the output with spaces to overwrite only one extra character's worth.&lt;/p&gt;

&lt;p&gt;Also, if you want to clear the label, &lt;code&gt;health.clear&lt;/code&gt; would do the job.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Need
&lt;/h2&gt;

&lt;p&gt;This class could be useful because it achieves the results of the other solutions presented while seriously limiting the amount of work the programmer must do. For each update to the value being printed, the programmer only needs to write one print statement, with little to no calculation.&lt;/p&gt;

&lt;p&gt;Hopefully this class will be updated and finalized in the near future.&lt;/p&gt;

</description>
      <category>textlabel</category>
      <category>minimicro</category>
      <category>miniscript</category>
      <category>bclib</category>
    </item>
    <item>
      <title>A MiniScript Enum Class</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Mon, 02 Jun 2025 19:51:35 +0000</pubDate>
      <link>https://dev.to/bibleclinger/a-miniscript-enum-class-4pei</link>
      <guid>https://dev.to/bibleclinger/a-miniscript-enum-class-4pei</guid>
      <description>&lt;p&gt;Given that &lt;a href="https://www.miniscript.org/" rel="noopener noreferrer"&gt;MiniScript&lt;/a&gt; is a very minimalistic scripting language, it purposely lacks an Enum type. Its types are Number, String, List, Map, and funcRef (ie. a function reference) -- and that's it.&lt;/p&gt;

&lt;p&gt;That means that when you want to express an Enum type, you may find yourself using magic numbers. You can use Numbers after all -- in fact I would imagine most underlying Enum implementations do in fact use numerical types under the hood -- but if you find yourself writing code like &lt;code&gt;ship.firingMode = 2&lt;/code&gt;, you're probably going to be in for a world of hurt when you try to return to your space shooter game after a six month break. You may not be able to remember the significance, nuances, and intricacies of firing-mode 2 off a quick glance.&lt;/p&gt;

&lt;p&gt;Alternatively, you can use the String type. &lt;code&gt;ship.firingMode = "MISSILE_MODE"&lt;/code&gt; is valid MiniScript and is not a terrible alternative to Numbers, but it could lend itself to some interesting errors if you mistype the string somewhere, since &lt;code&gt;ship.firingMode = "MISSSILE_MODE"&lt;/code&gt; is also valid MiniScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Enum class
&lt;/h2&gt;

&lt;p&gt;I decided to implement a simple Enum class in MiniScript. I have &lt;a href="https://github.com/BibleClinger/bclib" rel="noopener noreferrer"&gt;just published it to GitHub&lt;/a&gt; in a library called bclib[1]. I hope to add more items to this library in the near future.&lt;/p&gt;

&lt;p&gt;Let's set up a simple example of how we might use this Enum class in a space shooter. In this theoretical game, our ship can fire off all sorts of different objects, but it can only fire items of one type at a time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"bclib"&lt;/span&gt;

&lt;span class="n"&gt;eShipShootingMode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bclib&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Enum&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Safety Guns Missile Mines Probe"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;eShipShootingMode&lt;/code&gt; represents your Enum type. We have chosen five firing modes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Safety (ie. Weapons off)&lt;/li&gt;
&lt;li&gt;Guns&lt;/li&gt;
&lt;li&gt;Missile&lt;/li&gt;
&lt;li&gt;Mines&lt;/li&gt;
&lt;li&gt;Probe&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note: we could have passed a list of strings, but in this example, we used a single string as our argument. Either will work. In the case of a single string, the &lt;code&gt;create&lt;/code&gt; function will split the string along spaces.&lt;/p&gt;

&lt;p&gt;Now the ship will need to store its firing mode in a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;ship&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;{}&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Define&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;ship&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;
&lt;span class="n"&gt;ship&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firingMode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eShipShootingMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Safety&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Weapons&lt;/span&gt; &lt;span class="n"&gt;off&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if the user presses the key for guns? We can switch firing modes easily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;ship&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firingMode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;eShipShootingMode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Guns&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above line should easily convey to any reader of your code what your intention is: you want the ship to have its firing mode set to fire guns.&lt;/p&gt;

&lt;p&gt;Also, trying to set your firing mode to &lt;code&gt;eShipShootingMode.Misssile&lt;/code&gt; (ie. a typo with one too many &lt;code&gt;s&lt;/code&gt; characters) will crash the program straight up when that line is executed. With a String, that won't happen unless you're doing extra error checking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Functions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;bclib.Enum.count&lt;/code&gt;: A function that returns how many members the particular enum type has.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bclib.Enum.getName&lt;/code&gt;: A function that returns the name instead of the numerical identifier. In our example, &lt;code&gt;eShipShootingMode.getName(eShipShootingMode.Guns)&lt;/code&gt; will return "Guns". This can be useful for logging purposes, for example, as you'd want the string instead of the numerical value when logging, or else you'd need to look up what firing mode 2 is all over again.&lt;/li&gt;
&lt;/ul&gt;







&lt;p&gt;[1] Short for "BibleClinger Library". Not too original, but straightforward.&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>minimicro</category>
      <category>bclib</category>
    </item>
    <item>
      <title>Adding Basic Animation to a Mini Micro Game</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Mon, 26 May 2025 05:12:55 +0000</pubDate>
      <link>https://dev.to/bibleclinger/adding-basic-animation-to-a-mini-micro-game-hhk</link>
      <guid>https://dev.to/bibleclinger/adding-basic-animation-to-a-mini-micro-game-hhk</guid>
      <description>&lt;p&gt;Recently on the MiniScript &lt;a href="https://discord.gg/7s6zajx" rel="noopener noreferrer"&gt;Discord&lt;/a&gt;, Dat_One_Dev posted code he was using for &lt;a href="https://youtu.be/FEJEeNP3PsY" rel="noopener noreferrer"&gt;a YouTube video tutorial&lt;/a&gt; on making video games for &lt;a href="https://miniscript.org/MiniMicro/index.html" rel="noopener noreferrer"&gt;Mini Micro&lt;/a&gt;. It was a game where you simply chase a balloon, clicking on it each time it appears. When you click on it, it moves to a new location, waiting for you to repeat the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmnxbcwrkqigjhuymnh7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmnxbcwrkqigjhuymnh7.png" alt="The Balloon Game" width="800" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now this got me thinking. As nice of a simple demo as this was (the code wasn't very long -- about 37 lines in the initial version posted on Discord), something about the balloon seemed off. It wasn't moving at all when it was on the screen. Why not make the balloon move a bit and demonstrate a basic animation technique?&lt;/p&gt;

&lt;p&gt;The idea that came to mind first was that we could make the balloon "breathe" -- that is, it would oscillate between shrinking and expanding. Sprites in Mini Micro have a property called &lt;code&gt;scale&lt;/code&gt;. We could subtract and add small amounts to this property on the balloon's sprite to achieve each effect respectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables Required
&lt;/h2&gt;

&lt;p&gt;First we need some variables. Due to how the original script was written[1], I decided to add these as global variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;breatheTime = 1
currentTime = breatheTime
breatheIncrement = 0.01
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;breatheTime&lt;/code&gt; is the amount of time in seconds for the balloon to perform either a shrink or an expansion.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;currentTime&lt;/code&gt; is our current timer. It's how much time is left before we swap between shrinking or expanding.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;breatheIncrement&lt;/code&gt; is how much we want to adjust the scale of the balloon's sprite per tick. In this case, we want to change it by a very small amount, since ticks happen quite frequently -- about 60 times per second.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Breathing Function
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;breathe = function(dt)
    globals.currentTime -= dt
    if currentTime &amp;lt;= 0 then
        globals.breatheIncrement *= -1
        globals.currentTime = breatheTime
    else
        Balloon.scale += breatheIncrement
    end if
end function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's walk through the code. First, our &lt;code&gt;breathe&lt;/code&gt; function has one parameter -- the delta time. This is the amount of time that has passed since the last tick or last cycle of the game loop.&lt;/p&gt;

&lt;p&gt;We immediately subtract &lt;code&gt;dt&lt;/code&gt; -- or delta time -- from &lt;code&gt;currentTime&lt;/code&gt;, which is the global timer we just created. We then check to see if our timer is less than or equal to 0. If it is, the timer has been triggered. We have gone far enough in shrinking or expanding. It's time to reverse course!&lt;/p&gt;

&lt;p&gt;We reverse course by inverting the sign of &lt;code&gt;breatheIncrement&lt;/code&gt;. This is achieved by simple multiplication of -1. We then reset the timer by setting &lt;code&gt;currentTime&lt;/code&gt; to &lt;code&gt;breatheTime&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the other hand, if the timer &lt;em&gt;didn't&lt;/em&gt; reach 0, then we continue our current operation by simply adding &lt;code&gt;breatheIncrement&lt;/code&gt; to &lt;code&gt;Balloon.scale&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calculating Time
&lt;/h2&gt;

&lt;p&gt;We have our &lt;code&gt;breathe&lt;/code&gt; function, but how do we call it, and how do we pass delta time to it?&lt;/p&gt;

&lt;p&gt;First, almost any game in Mini Micro should do something like this for its main game loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
    &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lastTime&lt;/span&gt;

    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Game&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;

    &lt;span class="n"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
    &lt;span class="n"&gt;yield&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the block where game logic is marked to go, that is where we put code that requires updates per tick -- like &lt;code&gt;breathe&lt;/code&gt;. Our new game loop should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
    &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lastTime&lt;/span&gt;

    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Game&lt;/span&gt; &lt;span class="n"&gt;logic&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;
    &lt;span class="n"&gt;breathe&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt;

    &lt;span class="n"&gt;lastTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
    &lt;span class="n"&gt;yield&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final animation looks like this. Spiffy:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0izzh98eg2410xvddzpn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0izzh98eg2410xvddzpn.gif" alt="Balloon " width="232" height="198"&gt;&lt;/a&gt;&lt;/p&gt;







&lt;p&gt;[1] Note: Since we're editing someone else's code, the goal is to accomplish what we want by changing as little as possible. We want to work with what exists, not rewrite it, for our purposes.&lt;/p&gt;

</description>
      <category>minimicro</category>
      <category>miniscript</category>
      <category>animation</category>
    </item>
    <item>
      <title>MiniScript's locals, outer, and globals</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Sun, 06 Apr 2025 21:09:51 +0000</pubDate>
      <link>https://dev.to/bibleclinger/miniscripts-locals-outer-and-globals-1pcn</link>
      <guid>https://dev.to/bibleclinger/miniscripts-locals-outer-and-globals-1pcn</guid>
      <description>&lt;p&gt;Just recently on the &lt;a href="https://discord.gg/7s6zajx" rel="noopener noreferrer"&gt;MiniScript Discord&lt;/a&gt;, someone was asking for support with a function of his that was supposed to modify a variable external to his function -- except it wasn't working. Given how this could be a gotcha, I figured it would be a topic worth exploring here in greater detail.&lt;/p&gt;

&lt;p&gt;Let's say we have a game where the player has an inventory that can only hold one item. We want to have a function that sets the inventory item, which we'll store in a global.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"empty"&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;

&lt;span class="n"&gt;setInventory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's test the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="s"&gt;"empty"&lt;/span&gt;
&lt;span class="n"&gt;setInventory&lt;/span&gt; &lt;span class="s"&gt;"key"&lt;/span&gt;
&lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="s"&gt;"empty"&lt;/span&gt; &lt;span class="o"&gt;????&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is going wrong? Why is the inventory still empty?&lt;/p&gt;

&lt;p&gt;With code this simple, it should be pretty clear that our function is not setting the global &lt;code&gt;inventory&lt;/code&gt; variable. We might ask for some help, and someone will tell us to change &lt;code&gt;inventory = item&lt;/code&gt; to &lt;code&gt;globals.inventory = item&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Perfect! It works.&lt;/p&gt;

&lt;p&gt;But why?&lt;/p&gt;

&lt;h2&gt;
  
  
  The locals and globals maps
&lt;/h2&gt;

&lt;p&gt;MiniScript has two particular maps worth knowing about right away:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;locals&lt;/li&gt;
&lt;li&gt;globals&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you set a variable by doing, let's say, &lt;code&gt;x = 5&lt;/code&gt;, it's the equivalent of doing &lt;code&gt;locals.x = 5&lt;/code&gt;. This &lt;code&gt;locals&lt;/code&gt; map is built into MiniScript's inner workings.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;globals&lt;/code&gt; is the map that contains all of the variables that are set at global scope. When your script begins to run, if you begin executing code without putting it inside any function, and if it's not an imported module, then the &lt;code&gt;globals&lt;/code&gt; reference points to the same map as the &lt;code&gt;locals&lt;/code&gt; reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;locals&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;globals&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;

&lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="n"&gt;locals&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;prints&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this script is run on its own and outside of any function, then &lt;code&gt;locals.x&lt;/code&gt; and &lt;code&gt;globals.x&lt;/code&gt; are indeed the same variable, because &lt;code&gt;locals&lt;/code&gt; and &lt;code&gt;globals&lt;/code&gt; are the same map. This is &lt;a href="https://dev.to/joestrout/if-locals-globals-38bh"&gt;why you can explicitly test&lt;/a&gt; &lt;code&gt;if locals == globals&lt;/code&gt; in order to run different code, allowing for unit tests in your modules, tests that can be executed on their own but ignored when imported.&lt;/p&gt;

&lt;h2&gt;
  
  
  Outer
&lt;/h2&gt;

&lt;p&gt;There is a third map available called &lt;code&gt;outer&lt;/code&gt;. This one is a bit more niche. It is used for inner functions to access variables local to its outer function.&lt;/p&gt;

&lt;p&gt;When your script first starts (again assuming it's being executed on its own and outside of any function) &lt;code&gt;locals == globals&lt;/code&gt; and &lt;code&gt;locals == outer&lt;/code&gt; and &lt;code&gt;outer == globals&lt;/code&gt; will all resolve to true. Easy enough. This is why you don't usually need to be concerned with &lt;code&gt;outer&lt;/code&gt;, because often enough, even from within a function, &lt;code&gt;outer == globals&lt;/code&gt; will resolve to true.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading vs Writing
&lt;/h2&gt;

&lt;p&gt;Let's say you are trying to print a variable named &lt;code&gt;total&lt;/code&gt;. How does MiniScript look up the variable?&lt;/p&gt;

&lt;p&gt;MiniScript first searches inside the &lt;code&gt;locals&lt;/code&gt; map. If it doesn't find it, it tries to find it inside the &lt;code&gt;outer&lt;/code&gt; map. If it doesn't find it there, then it tries &lt;code&gt;globals&lt;/code&gt;. Finally, it raises a runtime error if it hasn't been found.&lt;/p&gt;

&lt;p&gt;This is different from writing, in which case, MiniScript always assumes &lt;code&gt;locals&lt;/code&gt; is where you want to write your variables unless you explicitly specify a different map. This is why we can print the value of &lt;code&gt;globals.inventory&lt;/code&gt; without explicitly mentioning &lt;code&gt;globals&lt;/code&gt;, but we can't write to it without the explicit reference.&lt;/p&gt;

&lt;p&gt;Therefore, for reading, you do NOT need to use explicit references to &lt;code&gt;locals&lt;/code&gt;, &lt;code&gt;outer&lt;/code&gt;, or &lt;code&gt;globals&lt;/code&gt; -- &lt;em&gt;unless you are purposely trying to bypass shadowing&lt;/em&gt;. (Although if you are trying to do that, perhaps consider if shadowing is really a good idea for you in the first place.) For writing, you &lt;em&gt;must&lt;/em&gt; explicitly use a reference to the correct map, &lt;em&gt;if&lt;/em&gt; you do NOT wish to use &lt;code&gt;locals&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The order of variable lookup for reading is &lt;code&gt;locals&lt;/code&gt;, then &lt;code&gt;outer&lt;/code&gt;, and then &lt;code&gt;globals&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Variables are written to &lt;code&gt;locals&lt;/code&gt; by default.&lt;/li&gt;
&lt;li&gt;If you want to write to a variable outside of &lt;code&gt;locals&lt;/code&gt;, you must specify the map explicitly, whether &lt;code&gt;outer&lt;/code&gt; or &lt;code&gt;globals&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>miniscript</category>
      <category>globals</category>
      <category>scope</category>
      <category>locals</category>
    </item>
    <item>
      <title>MiniScript's Parent-Class Variable Pitfall</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Fri, 14 Mar 2025 05:34:07 +0000</pubDate>
      <link>https://dev.to/bibleclinger/miniscripts-parent-class-variable-pitfall-4koi</link>
      <guid>https://dev.to/bibleclinger/miniscripts-parent-class-variable-pitfall-4koi</guid>
      <description>&lt;h2&gt;
  
  
  The Scenario
&lt;/h2&gt;

&lt;p&gt;Let's say you're wanting to use &lt;a href="https://miniscript.org/" rel="noopener noreferrer"&gt;MiniScript&lt;/a&gt; to write a game for &lt;a href="https://miniscript.org/MiniMicro/index.html" rel="noopener noreferrer"&gt;Mini Micro&lt;/a&gt;. You're making a Shoot'em Up (or SHMUP for short). This is an older genre of video game that used to be quite popular. Let's say you want to make a bunch of enemy fighters for the player to shoot at in rapid succession as they try to shoot back at the player.&lt;/p&gt;

&lt;p&gt;Now let's suppose you have a background in an object oriented programming language like Java. For your enemy fighters, you figure you should make a class like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;EnemyFighter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"shotStrength"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"cargo"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt; &lt;span class="x"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are defining two properties for an enemy fighter:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The shot strength: how many damage units their shot does to the player ship. By default we want 1 unit of damage.&lt;/li&gt;
&lt;li&gt;The cargo that the enemy fighter is carrying. This will translate to what items will drop when the enemy ship is destroyed. This is how the player will pick up loot.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To simplify the loot system, let's just give &lt;code&gt;cargo&lt;/code&gt; a list of integers, with each integer corresponding to an item in some lookup table. Let's spawn three enemies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;enemies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Remember&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;inclusive&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shotStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cargo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="n"&gt;enemies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are intending the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first ship has a shot strength of 1 and a single cargo item of id 0.&lt;/li&gt;
&lt;li&gt;The 2nd enemy fighter has shot strength 2 and a single cargo item of id 1.&lt;/li&gt;
&lt;li&gt;The 3rd and last enemy has a shot strength of 3 and a single cargo item of id 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now adding bullets, collision detection, animations, and the destruction of the enemy fighter are all necessary, but let's bypass that and just test if our code is correct this far. First we'll write a function for &lt;code&gt;EnemyFighter&lt;/code&gt; to print its internal variables when destroyed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onDestroy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"Enemy Fighter destroyed: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shotStrength&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"; "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cargo&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's add some debugging to see what happens when the ship is destroyed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;enemies&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onDestroy&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enemy Fighter destroyed: 1; [0, 1, 2]
Enemy Fighter destroyed: 2; [0, 1, 2]
Enemy Fighter destroyed: 3; [0, 1, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh, that's not good. All three enemy fighters are showing that they are carrying three cargo items!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;At first glance it might look like we made a mistake and added all of the cargo items to each fighter explicitly in our loop, but we didn't; we added one item per fighter. The actual bug is harder to spot. Can you find it?&lt;/p&gt;

&lt;p&gt;Let's take a peek at a representation of the &lt;code&gt;EnemyFighter&lt;/code&gt; class and the &lt;code&gt;enemies&lt;/code&gt; list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6mmch59p6493ordlnmn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6mmch59p6493ordlnmn.png" alt="UML Diagram" width="523" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's only one instance of &lt;code&gt;cargo&lt;/code&gt; and it belongs to &lt;code&gt;EnemyFighter&lt;/code&gt;. In Java terms, &lt;code&gt;cargo&lt;/code&gt; is behaving more like a static class variable than an instance variable. Wow. How did that happen?&lt;/p&gt;

&lt;h2&gt;
  
  
  A Deeper Dive
&lt;/h2&gt;

&lt;p&gt;MiniScript uses Prototype Inheritance for its implementation of Object Oriented Programming. There is no difference between classes and objects. In fact, both classes and objects are just Maps.&lt;/p&gt;

&lt;p&gt;When you use the &lt;code&gt;new&lt;/code&gt; keyword, you are making a new map with one entry: &lt;code&gt;__isa&lt;/code&gt;. This entry is what MiniScript uses to go up the inheritance chain.&lt;/p&gt;

&lt;p&gt;But why did MiniScript duplicate &lt;code&gt;shotStrength&lt;/code&gt; for every &lt;code&gt;EnemyFighter&lt;/code&gt; instance object? Because we explicitly set it in the child instances. MiniScript adds shadow variables to these instances when we set a variable so we don't have this issue.&lt;/p&gt;

&lt;p&gt;But why didn't it duplicate &lt;code&gt;cargo&lt;/code&gt; for us? Because we didn't explicitly set the value of &lt;code&gt;cargo&lt;/code&gt; for each child instance. Instead we merely pushed another element into the already existing &lt;code&gt;cargo&lt;/code&gt; list. Where is that existing list? Inside &lt;code&gt;EnemyFighter&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix
&lt;/h2&gt;

&lt;p&gt;There are multiple ways of fixing this. Here's the simple way to fix this example: explicitly add the line &lt;code&gt;e.cargo = []&lt;/code&gt; directly before you push the cargo index value. This will create a separate cargo list for each enemy fighter. The instancing code becomes this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;enemies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Remember&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;inclusive&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shotStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Fixed&lt;/span&gt; &lt;span class="n"&gt;shared&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="n"&gt;bug&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cargo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="n"&gt;enemies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As an aside, something that might be hard to wrap one's brain around is the fact that the &lt;code&gt;EnemyFighter&lt;/code&gt; class could actually be entirely empty of properties, since we aren't using any functionality on this object directly.&lt;/p&gt;

&lt;p&gt;Remember &lt;code&gt;EnemyFighter&lt;/code&gt; is just a map. It's not inherently an object or a class -- it can be used however we want it to be. The instances of &lt;code&gt;EnemyFighter&lt;/code&gt; are also just maps. The problem is simple: if we are editing a list that is actually found in the parent instance, then naturally this will be reflected in all child instances. They are all using the same list, after all.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Factory Method Pattern
&lt;/h2&gt;

&lt;p&gt;We don't have constructors in MiniScript. Instead, we can make our own factory function that produces objects. Let's rewrite the &lt;code&gt;EnemyFighter&lt;/code&gt; class entirely. We want the following features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;EnemyFighter&lt;/code&gt; instance creation is handled by a member function that is similar, in Java terms, to a static method on a class.&lt;/li&gt;
&lt;li&gt;All instances of &lt;code&gt;EnemyFighter&lt;/code&gt; are stored in a list found within the class itself and shared by all instances.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EnemyFighter.onDestroy&lt;/code&gt; removes its instance from this internal list.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;EnemyFighter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Enemies"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt; &lt;span class="x"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shotStrength&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shotStrength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;shotStrength&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="k"&gt;isa&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cargo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="x"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cargo&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enemies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;

EnemyFighter.onDestroy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"Enemy Fighter destroyed: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shotStrength&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"; "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cargo&lt;/span&gt;
    &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enemies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remove&lt;/span&gt; &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enemies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexOf&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above is the secret internals of the &lt;code&gt;EnemyFighter&lt;/code&gt; implementation. If you are writing a sort of game engine for your team to use, they hopefully don't need to know too much about the above code. Rather, they just need to know how to use it.&lt;/p&gt;

&lt;p&gt;Now our enemy creation loop might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Remember&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;inclusive&lt;/span&gt;
    &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="x"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cleaner! And our debug code to simulate destroying all the enemies could be this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enemies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
   &lt;span class="n"&gt;EnemyFighter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enemies&lt;/span&gt;&lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="x"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onDestroy&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This now prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enemy Fighter destroyed: 1; [0]
Enemy Fighter destroyed: 2; [1]
Enemy Fighter destroyed: 3; [2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;MiniScript's implementation of the OOP paradigm can be a bit tricky. This is especially problematic if you experience something like the above while working hard on a 48 hour game jam where time is a commodity you can't afford to lose much of when tracking down bugs. We can mitigate the difficulties by writing clean instancing code, and adhering to certain programming patterns.&lt;/p&gt;

&lt;p&gt;I'll leave you with this last thought: &lt;em&gt;It might be argued that MiniScript's implementation of OOP is actually quite simple, but part of what adds complexity is the baggage we are bringing from other OOP languages into MiniScript.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you agree or disagree, please let me know in the comments. Alternatively, if you have other ideas on object creation, suggestions for better patterns or practices, or related contemplations on MiniScript, also leave a comment.&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>oop</category>
      <category>static</category>
    </item>
    <item>
      <title>Does MiniScript Need Variadic Functions?</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Thu, 13 Mar 2025 08:05:14 +0000</pubDate>
      <link>https://dev.to/bibleclinger/does-miniscript-need-variadic-functions-53a0</link>
      <guid>https://dev.to/bibleclinger/does-miniscript-need-variadic-functions-53a0</guid>
      <description>&lt;p&gt;A relatively common question asked at the &lt;a href="https://discord.gg/7s6zajx" rel="noopener noreferrer"&gt;MiniScript Discord&lt;/a&gt; is about variadic functions and why &lt;a href="https://miniscript.org/" rel="noopener noreferrer"&gt;MiniScript&lt;/a&gt; lacks this feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variadic Functions
&lt;/h2&gt;

&lt;p&gt;First of all, what even is a "variadic function?" This is a function that takes a variable number of arguments.&lt;/p&gt;

&lt;p&gt;For those familiar with C, the standard library function &lt;code&gt;printf&lt;/code&gt; is such a function. To a C newbie's surprise, the actual function signature of C's &lt;code&gt;printf&lt;/code&gt; is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;int printf ( const char * format, ... );&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The ellipsis (the three dots) represents an unknown number of arguments. You can print as many ints, floats, chars, or char *'s, etc. that you would like in one function call.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not in MiniScript?
&lt;/h2&gt;

&lt;p&gt;What new MiniScript programmers are usually looking for is a way to process an unknown number of &lt;em&gt;elements&lt;/em&gt;. They think variadic functions are the way of solving this, but let's reconsider such problems.&lt;/p&gt;

&lt;p&gt;Let's say you have a function that needs to accept an X and a Y coordinate, and then optionally process an unknown amount of numbers. One solution is to write a function with three arguments. The first two can be Numbers, and the last one can be a list. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg_list&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;arg_list&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function &lt;code&gt;f&lt;/code&gt; can be called like this, assuming the list is already known:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;215&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="x"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can create the list directly inside the function call as shown above. Such a list can also be obtained at runtime. The simplicity is here. Not only don't we &lt;em&gt;need&lt;/em&gt; to fiddle with an unknown amount of arguments, but the question should be -- why would we &lt;em&gt;want&lt;/em&gt; to do so when given the above?&lt;/p&gt;

&lt;h2&gt;
  
  
  Maps as Arguments
&lt;/h2&gt;

&lt;p&gt;Let's say you're building a complicated GUI toolkit for &lt;a href="https://miniscript.org/MiniMicro/index.html" rel="noopener noreferrer"&gt;Mini Micro&lt;/a&gt;. You want other programmers to be able to use it, so you are making an API. You want a complex function called &lt;code&gt;createWindow&lt;/code&gt; that takes in all sorts of properties, such as the width, height, and title of the window in question. Do we need a whole bunch of arguments to accomplish this? Not necessarily. We can use a Map instead to hold our properties.&lt;/p&gt;

&lt;p&gt;Although there are probably &lt;em&gt;much&lt;/em&gt; better ways of implementing the guts of the function, this is one incredibly straightforward way to write it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;createWindow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"width"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"height"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be called like this by users of your API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;createWindow&lt;/span&gt; &lt;span class="x"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"My Window"&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"width"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;320&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"height"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;240&lt;/span&gt; &lt;span class="x"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The map can be hardcoded or built dynamically at runtime. Easy!&lt;/p&gt;

&lt;h1&gt;
  
  
  Benefits of Maps as Arguments
&lt;/h1&gt;

&lt;p&gt;Now imagine there is a team using your GUI toolkit, and they report back to you that while they love the API, they would like the ability to change the color of the title bar. You agree that this is a needed feature. You can add this in fairly easily, and the function becomes this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;createWindow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"width"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"height"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;properties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hasIndex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"title_color"&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt;
        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a serious benefit of this design: your function signature didn't change. It still expects the same number of arguments. This means all of the legacy code already written will still work exactly as before! Interestingly enough, new code for the new version of your toolkit should still run on the old version simply because your function ignores entries in the map that it doesn't recognize! If every property needed its own argument, this could be a nightmare for both you as the developer of this GUI toolkit as well as the users of your API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Now obviously these examples are contrived, and may not be the best way of implementing GUI code, but the design pattern of using lists and maps in place of variadic arguments has been demonstrated to be incredibly useful within the design philosophy of MiniScript.&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>varargs</category>
      <category>variadic</category>
    </item>
    <item>
      <title>Mini Micro and the 6502: Adding Keyboard Input</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Sat, 16 Mar 2024 13:50:29 +0000</pubDate>
      <link>https://dev.to/bibleclinger/mini-micro-and-the-6502-adding-keyboard-input-1cjm</link>
      <guid>https://dev.to/bibleclinger/mini-micro-and-the-6502-adding-keyboard-input-1cjm</guid>
      <description>&lt;p&gt;As I wrote &lt;a href="https://dev.to/bibleclinger/mini-micro-and-the-6502-adding-retro-to-the-neo-retro-3mgh"&gt;last time&lt;/a&gt;, I had decided to embark on the journey of writing a &lt;a href="http://www.6502.org/"&gt;6502 microprocessor&lt;/a&gt; emulator for the &lt;a href="https://miniscript.org/MiniMicro/"&gt;Mini Micro&lt;/a&gt; with the intent to try to make a Pong clone written in 6502 assembly. I got as far as being able to write a working "Hello, World!" program in 6502 assembly -- a task that took me 25 lines just in 6502 assembly code alone!&lt;/p&gt;

&lt;p&gt;I improved my Hello World example to print the string directly. That is useful, but we already have text output. What we really need right now is input. Let's add some keyboard support.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mini Micro API
&lt;/h2&gt;

&lt;p&gt;Before we can do that, let's examine the Mini Micro keyboard API, which is written entirely in the fantastic language called &lt;a href="https://miniscript.org"&gt;MiniScript&lt;/a&gt;. We can see from the &lt;a href="https://miniscript.org/wiki"&gt;MiniScript wiki&lt;/a&gt; that we have the &lt;a href="https://miniscript.org/wiki/Key"&gt;&lt;code&gt;key&lt;/code&gt; class&lt;/a&gt;, which lets us obtain keyboard input. Fantastic.&lt;/p&gt;

&lt;p&gt;Here are the two functions we'll want to look at first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;key.available&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;key.get&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;key.available&lt;/code&gt; returns 1 if we have input in the buffer, and 0 otherwise.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;key.get&lt;/code&gt; returns the next character in the input buffer, but it blocks if no input is immediately available.&lt;/p&gt;

&lt;p&gt;In my last post, we discussed memory mapping, which is how the 6502 talks to hardware. We can map an address to each of the above functions. Let's map $4101 to &lt;code&gt;key.available&lt;/code&gt; and $4102 to &lt;code&gt;key.get&lt;/code&gt;. This means if we read from $4101 in assembly, we'll get the result of &lt;code&gt;key.available&lt;/code&gt;, and if we read from $4102 in assembly, we'll get the result of &lt;code&gt;key.get&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The BIT Op Code
&lt;/h2&gt;

&lt;p&gt;Loading the value of $4101 via &lt;code&gt;lda $4101&lt;/code&gt; takes 4 CPU cycles, and it overwrites what is already inside A. Overwriting A is not always ideal. Thankfully, the 6502 has a way of being slightly more efficient.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://www.6502.org/tutorials/6502opcodes.html#BIT"&gt;BIT opcode&lt;/a&gt; allows us to examine bits 7 and 6 of any value stored in memory without actually loading it first into the accumulator. This means we should map &lt;code&gt;key.available&lt;/code&gt; specifically to bit 7 (or 6, if we preferred) of $4101 to take advantage of this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading Input
&lt;/h2&gt;

&lt;p&gt;Let's assign names to the addresses $4101 and $4102. Let's call them KEYCTRL and KEYGET respectively. Our assembly code for polling for the next character can look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;read:
bit KEYCTRL        ; $4101
bpl read
lda KEYGET         ; $4102
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The MiniScript code for this project is already rather lengthy, so we'll just look at some snippets.&lt;/p&gt;

&lt;p&gt;Here's the code for reading KEYCTRL ($4101):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;KeyboardIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getKeyCtrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memoryAddress&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writeMemory&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memoryAddress&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;127&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;Note&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="n"&gt;goes&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;bit&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What about our code for reading KEYGET ($4102)? I was advised on the MiniScript Discord that it would not be in the spirit of the 6502 to block for input, as &lt;code&gt;key.get&lt;/code&gt; does when it doesn't have any input available. As a result of conforming to this design philosophy, here's the MiniScript code for reading KEYGET ($4102):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="n"&gt;KeyboardIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;memoryAddress&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="n"&gt;then&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writeMemory&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;memoryAddress&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When KEYGET ($4102) is read by the assembly environment, then if and only if &lt;code&gt;key.available&lt;/code&gt; returns 1 does the value of &lt;code&gt;key.get&lt;/code&gt; become converted into an integer and written to memory for the assembly program to read. Otherwise, the function returns immediately without changing anything, and the assembly program will read whatever was left in KEYGET ($4102) prior to this moment -- which &lt;em&gt;could&lt;/em&gt; be the last key entered. If I were to document this behavior in a manual, I would probably write something like, "&lt;em&gt;If the 6502 assembly program reads from KEYGET ($4102) before KEYCTRL ($4101) indicates that there is valid input available, the value read from KEYGET ($4102) is undefined.&lt;/em&gt;"&lt;/p&gt;

&lt;h2&gt;
  
  
  Echo
&lt;/h2&gt;

&lt;p&gt;Now I can write a program that takes input from the user and echoes it back to the screen until the user presses the Escape key. Here is the relevant assembly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;KEYCTRL = $4101
KEYGET = $4102
PRINT_BYTE = $4001
PRINT_CHAR = $4002
TEXTDELIM = $4003
STRING_OUT_LOW = $4004
STRING_OUT_HIGH = $4005

ESC = $1B

.CODE

prompt: .byte "Welcome to echo! Type away!", 13, "Press ESC when done...", 13, 13, 0

.proc reset
    lda #&amp;lt;prompt
    sta STRING_OUT_LOW
    lda #&amp;gt;prompt
    sta STRING_OUT_HIGH     ; Print prompt

    ldy TEXTDELIM
    ldx #$00
    stx TEXTDELIM           ; Save old text.delimiter

    read:
    bit KEYCTRL             ; Is key.available true?
    bpl read
    lda KEYGET              ; Read key
    cmp #ESC                ; Compare it to ESC press
    beq done
    sta PRINT_CHAR          ; Print character to screen
    jmp read

    done:
    sty TEXTDELIM           ; Restore old text.delimiter
    sty PRINT_CHAR          ; Adds an extra char. This should be char(13) normally.
    brk                     ; Note, we don't RTS out of reset
.endproc

.segment "VECTORS"
.word reset
.word reset
.word reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a screenshot of the Echo program in action: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidxtmcau5byu6yka4gan.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fidxtmcau5byu6yka4gan.png" alt="Output of the Echo program in action" width="800" height="595"&gt;&lt;/a&gt;Output of the Echo program&lt;/p&gt;

&lt;h2&gt;
  
  
  Character Encoding
&lt;/h2&gt;

&lt;p&gt;Without getting into too much detail on character encoding, suffice it to say that characters are encoded here using plain numbers.&lt;/p&gt;

&lt;p&gt;Notice how A = 2E in the upper right hand corner of the screenshot. This debug view into the emulation of the 6502 microprocessor shows us the value of the registers in hexadecimal representation.&lt;/p&gt;

&lt;p&gt;Since we're performing &lt;code&gt;lda KEYGET&lt;/code&gt; every time we have an available key, we're effectively loading the numerical representation of the last character typed into A. Let's look at an &lt;a href="https://www.asciitable.com/"&gt;ASCII chart&lt;/a&gt;, and see if this makes sense. We can see in the chart that hexadecimal 2E, is the numerical representation of the period character. This is, in fact, the last character I had typed in the above screenshot.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Gif is worth a Million Words
&lt;/h2&gt;

&lt;p&gt;It's more fun to watch this happen live. Here's a gif image of the program execution. Watch how A is always changing to reflect the character typed last. Watch how PC, which is the program counter, is regularly trapped in the loop to &lt;code&gt;bit KEYCTRL&lt;/code&gt; until input is available.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---yUSJ-Q3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://res.cloudinary.com/dnyhcbonw/image/upload/c_crop%2Cg_south%2Ch_1.0%2Cw_1.0%2Cy_0.8/c_scale%2Cw_0.75/v1710582907/devto/MiniMicro6502/bnudhtd4czjefn7tdez9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---yUSJ-Q3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://res.cloudinary.com/dnyhcbonw/image/upload/c_crop%2Cg_south%2Ch_1.0%2Cw_1.0%2Cy_0.8/c_scale%2Cw_0.75/v1710582907/devto/MiniMicro6502/bnudhtd4czjefn7tdez9.gif" alt="GIF Output" width="736" height="552"&gt;&lt;/a&gt;&lt;/p&gt;
Echo output: A and PC are of particular interest



&lt;p&gt;At the end of the program when BRK is executed, A is shown to be 1B in hexadecimal. This is because the last character read from KEYGET was in fact the Escape key which has a hexadecimal value of 1B (which is 27 in decimal). Nothing else overwrote the A register before the BRK caused the program to terminate, so it stays the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;I have a model to connect Mini Micro functionality to the assembly world. My two enemies now are complexity and performance, as I try to map the graphics in an efficient manner. If I can fend off these opponents, I'll have made major progress toward my goal of making a Pong clone in 6502 assembly for the Mini Micro.&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>minimicro</category>
      <category>6502</category>
      <category>assembly</category>
    </item>
    <item>
      <title>Mini Micro and the 6502: Adding retro to the neo-retro</title>
      <dc:creator>BibleClinger</dc:creator>
      <pubDate>Tue, 12 Mar 2024 12:53:24 +0000</pubDate>
      <link>https://dev.to/bibleclinger/mini-micro-and-the-6502-adding-retro-to-the-neo-retro-3mgh</link>
      <guid>https://dev.to/bibleclinger/mini-micro-and-the-6502-adding-retro-to-the-neo-retro-3mgh</guid>
      <description>&lt;p&gt;I couldn't help but feel something was missing in the &lt;a href="https://joestrout.itch.io/mini-micro"&gt;Mini Micro&lt;/a&gt; environment. After all, we are encouraged to poke around and go deeper in this &lt;a href="https://miniscript.org/"&gt;MiniScript&lt;/a&gt;-centered ecosystem, but there &lt;em&gt;are&lt;/em&gt; practical limits to a "neo-retro virtual computer." Naturally the "virtual" part of that description means there is no hardware to discover. There is no processor for which you can write assembly code in order to squeeze out extra performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Processor
&lt;/h2&gt;

&lt;p&gt;That gave me an idea. Why not give the Mini Micro a virtual CPU for which we could write assembly? It wouldn't provide us with any performance benefit, but it would definitely add some immersion, and it has the potential to be a cool, educational tool.&lt;/p&gt;

&lt;p&gt;I had done some dabbling with NES development. While some aspects of the NES are indeed a nightmare, the one thoroughly enjoyable aspect to it is writing assembly code for its 6502-based processor. The 6502 would be my processor of choice to add to the Mini Micro.&lt;/p&gt;

&lt;h2&gt;
  
  
  Necessary Ingredients
&lt;/h2&gt;

&lt;p&gt;In order to make this happen, we need some components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A 6502 assembler&lt;/li&gt;
&lt;li&gt;A MiniScript program that can read the assembled binary file and execute it&lt;/li&gt;
&lt;li&gt;A way for the assembly program to utilize Mini Micro's features&lt;/li&gt;
&lt;li&gt;A goal&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For #1, yes, I could write my own assembler in MiniScript within the Mini Micro environment, but I decided to choose &lt;a href="https://cc65.github.io/doc/ca65.html"&gt;ca65&lt;/a&gt;, at least for now. This assembler is robust, very versatile, and offers some very nice features, not the least of which is the ability to painstakingly describe your memory layout. More on this later!&lt;/p&gt;

&lt;p&gt;For #2, that is where the bulk of my project comes into play. My intent is to write more posts on this aspect of the journey as development (hopefully) continues.&lt;/p&gt;

&lt;p&gt;For #3, the answer is memory-mapped IO. We'll need a scheme. More on this later as well!&lt;/p&gt;

&lt;p&gt;For #4, I'd like to be able to make some sort of Pong clone for the Mini Micro, completely in 6502 assembly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory Mapping
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw489mbwesrqclst5cyuf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw489mbwesrqclst5cyuf.png" alt="The 1976 Second Edition 6502 manual showing an example of memory mapping convention" width="623" height="325"&gt;&lt;/a&gt;The 1976 Second Edition MCS6500 MICROCOMPUTER FAMILY&lt;br&gt;
PROGRAMMING MANUAL provides a convention for memory mapping&lt;/p&gt;

&lt;p&gt;The 6502 has a 16-bit address space, but it has no special way to communicate with other hardware (ie. sound, video, etc. etc..). As a result, in real systems, certain addresses were actually mapped directly to the hardware devices instead of being mapped to RAM.&lt;/p&gt;

&lt;p&gt;Did you want to make the sound chip on your system play a tone? Perhaps you'd need to write a value to address $5000 that represents the pitch of the tone you'd like to play. Did you want to change a pixel on the video output? Perhaps you'd need to write the color to $6000, the X coordinate to $6001, and the Y coordinate to $6002. This was different in every system, and it all depended on the hardware components and how they were mapped.&lt;/p&gt;

&lt;p&gt;Given that the Mini Micro has no real hardware, but that it has similar functionality (ie. sound and video) tied to native MiniScript objects, we can implement memory mapping to utilize this same functionality. We just need to pick a wise scheme that maps Mini Micro functionality to the assembly world in a smart and efficient manner.&lt;/p&gt;
&lt;h2&gt;
  
  
  Hello World: 6502 style
&lt;/h2&gt;

&lt;p&gt;Let's see how far I've gotten in about two weeks. Here's a 6502 assembly program that is far from complete, but actually assembles and runs in Mini Micro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.CODE

msg: .asciiz "Hello, world!"

reset:
ldy $4003   ; Store previous text.delimiter
ldx #$00
stx $4003   ; Disable text.delimiter by setting it to 0

loop:
lda msg, x
beq done
sta $4002   ; print single character of msg[x]
inx
jmp loop
done:
ldx #$0D
sty $4003   ; Enable previous text.delimiter by setting it to char(y)
stx $4002   ; Print the new line
brk

.segment "VECTORS"
.word reset
.word reset
.word reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here we have its output when executed on the Mini Micro:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foosy17x44xi1zuxcc1qx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foosy17x44xi1zuxcc1qx.png" alt='Output of 6502 "Hello World" on the Mini Micro' width="800" height="602"&gt;&lt;/a&gt;From 6502 assembly to the Mini Micro display: "Hello, world!"&lt;/p&gt;

&lt;p&gt;Whew! That's a lot of effort to get a simple message printed to the screen!&lt;/p&gt;

&lt;p&gt;But how does it work? How does reading (ie. lda, ldx, ldy) and writing (ie. sta, stx, sty) to memory affect text output?&lt;/p&gt;

&lt;p&gt;I mapped addresses $4001 to $4003 to Mini Micro's text functionality for this test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;$4001: Writing a byte to this address outputs it as a &lt;strong&gt;number&lt;/strong&gt; to the screen (ie. Writing 65 will print "65" to the screen)&lt;/li&gt;
&lt;li&gt;$4002: Writing a byte to this address outputs it as a &lt;strong&gt;character&lt;/strong&gt; to the screen (ie. Writing 65 will print "A" to the screen)&lt;/li&gt;
&lt;li&gt;$4003: Reading or writing a byte to this address gets or sets respectively the value of &lt;code&gt;text.delimiter&lt;/code&gt; -- which is the character in Mini Micro that gets printed after every call to &lt;code&gt;print&lt;/code&gt;. By default it is the same value as &lt;code&gt;char(13)&lt;/code&gt; in MiniScript (or '\r' in C).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Journey
&lt;/h2&gt;

&lt;p&gt;The important thing so far is that it works -- but is it sufficient for Pong?&lt;/p&gt;

&lt;p&gt;As you can see, in this run we've managed to emulate 223 cycles (BRK isn't emulated correctly just yet, so its cycle count isn't accurate) in 0.03506 seconds. That means that the virtual CPU is working at an average speed of ~6.360Khz -- wildly underperforming the real chip that could run between 1Mhz and 3Mhz in the real world. Hopefully we'll explore performance in later updates.&lt;/p&gt;

&lt;p&gt;Is it enough? Can we squeeze more performance out of it? Join me on the journey, and let's find out.&lt;/p&gt;

&lt;p&gt;Do you have any good 6502 memories? Do you have ideas for this project? Let me know!&lt;/p&gt;

</description>
      <category>miniscript</category>
      <category>minimicro</category>
      <category>6502</category>
      <category>assembly</category>
    </item>
  </channel>
</rss>
