# Extension that modifies the simulator

**URL:** <https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879>\
**Category:** micro:bit\
**Created:** [July 22, 2020, 5:10pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879 "2020-07-22T17:10:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![agc24](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/agc24/32/503_2.png) [@agc24](https://forum.makecode.com/u/agc24)\
**Post date:** [July 22, 2020, 5:10pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/1 "2020-07-22T17:10:41Z")

</div>

Hello,  
I was wondering if it is possible to write an extension that modifies the simulator? I use some extensions that allow students to program different sensors and I would like them to be able to test their programs by choosing different sensor values in the simulator (like the light sensor on the micro:bit) before they load the program on to the micro:bit. This is especially important since school is going to most likely be virtual this fall.  
Thanks!  
Alex

---

<div class="post-metadata">

**Author:** ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)\
**Post date:** [July 23, 2020, 12:26pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/2 "2020-07-23T12:26:39Z")

</div>

Hi Alex,

I’ve been investigating this concept for a while. Someone else may have a better answer, but I was considering using the “Experimental” Editor Extensions feature, which would require explicit effort for users to opt-in to using, and it would not be as visible as the built-in interactions, like the light sensor.

My basic idea is to allow users to enter a table of times and periods to simulate the sensor in the extension frame. For example, they may indicate the sensor shows 20 for 2 seconds, then 22 for 4 seconds, then 26 for 12 seconds, then 22 for 2 seconds. A total of 20 seconds of values are entered and they’d repeat every 20 seconds. Of course, if you’re just setting a specific value rather than a “schedule”, you could do so with a GUI instead.

Details about these types of extensions are here: [https://makecode.com/extensions/extensions](https://makecode.com/extensions/extensions)  
and there’s an example extension here: [https://github.com/samelhusseini/pxt-editor-extension-sample](https://github.com/samelhusseini/pxt-editor-extension-sample)

I hope someone else can share a better approach!

---

<div class="post-metadata">

**Author:** ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)\
**Post date:** [July 23, 2020, 1:56pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/3 "2020-07-23T13:56:04Z")

</div>

For security reasons, it is not currently possible to inject any javascript into the simulator. In a nutshel, it’s not trivial right now.

Here are a few ways I can think of making this work:

- fork [github.com/microsoft/pxt-microbit](http://github.com/microsoft/pxt-microbit) and implement additional behaviors in the pxt simulator itself. This is potentially complicated as our codebase is rather large. **Send us the pull request! 🙂**
- embed MakeCode as a widget in your page and communicate to the simulator via iframe messages. You will receive messages for serial, radio packets.

---

<div class="post-metadata">

**Author:** ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)\
**Post date:** [July 24, 2020, 12:42am UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/4 "2020-07-24T00:42:58Z")

</div>

Just to check…PRs to update simulation behavior for specific extensions _may_ be accepted? (That would be great!)

---

<div class="post-metadata">

**Author:** ![toysonlocation](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/toysonlocation/32/5932_2.png) [@toysonlocation](https://forum.makecode.com/u/toysonlocation)\
**Post date:** [March 11, 2022, 6:32pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/5 "2022-03-11T18:32:21Z")

</div>

How would I go about sending text between the sim and my own process in the host frame?

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [March 11, 2022, 6:34pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/6 "2022-03-11T18:34:04Z")

</div>

@toysonlocation can you give a little background on what you’re trying to do? Are you embedding the simulator in a website?

---

<div class="post-metadata">

**Author:** ![toysonlocation](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/toysonlocation/32/5932_2.png) [@toysonlocation](https://forum.makecode.com/u/toysonlocation)\
**Post date:** [March 14, 2022, 3:13pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/7 "2022-03-14T15:13:10Z")

</div>

Thanks for the reply. We want to let students develop for a robot car in a simulated environment rather than force them to have the real microbit on hand. We would like to simulate the car in its own virtual 2D env, and have that virtual env informed by output from the sim, and feed back to the sim from the virtual env via sensors. I have wrapped servo extensions with custom blocks to present the same API, and would hope to do the same with the sensors “somehow”. So, there are really three things I’m looking at:

1. real time text data to and from the sim
2. an external view to process the stream (from the sim) and send back signals (to the sim)
3. a way to wrap the return signals in the sensor components so the student won’t need to swap them out with “real” sensor components for the real microbit.

---

<div class="post-metadata">

**Author:** ![thegreatone](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/thegreatone/32/5414_2.png) [@thegreatone](https://forum.makecode.com/u/thegreatone)\
**Post date:** [March 14, 2022, 3:14pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/8 "2022-03-14T15:14:47Z")

</div>

I would recommend looking at the jacdac extension.

> **[GitHub - microsoft/pxt-jacdac: Jacdac client services for MakeCode - beta](https://github.com/microsoft/pxt-jacdac)**
>
> Jacdac client services for MakeCode - beta. Contribute to microsoft/pxt-jacdac development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![toysonlocation](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/toysonlocation/32/5932_2.png) [@toysonlocation](https://forum.makecode.com/u/toysonlocation)\
**Post date:** [March 24, 2022, 11:44pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/9 "2022-03-24T23:44:29Z")

</div>

Hi TGO, thanks for the link. Notifications from this board were going into my spam folder, or else I would have written sooner. The most important thing is that we can data from the sim, and write back to it via fake sensors. I’ll have a look at Jacdac!

---

<div class="post-metadata">

**Author:** ![Dvdplate](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@Dvdplate](https://forum.makecode.com/u/Dvdplate)\
**Post date:** [February 21, 2023, 10:55pm UTC](https://forum.makecode.com/t/extension-that-modifies-the-simulator/2879/10 "2023-02-21T22:55:31Z")

</div>

Hi everyone, id like to ask for a little more clarity on running external simulators.  
(my question is specifically in regard to pxt-microbit)

If you import the jacdac extension into pxt-microbit (for example), this will launch an iframe underneath the existing simulator, but this is only possible by making edits to pxt-microbit in pxtarget.json to allow a channel for a ‘message simulator’ and then specifying the URL to the iframe, as shown here with the jacdac extension:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/642456360107045c93a3833255ad2c8e45e92e3b.png)

The only part i think everyone, especially me, is confused about is how to send data back and forth to make the simulator and make code interact with each other, is there any documentation you could point us to or show us how to send messages to/from the sim like @peli said ‘You will receive messages for serial, radio packets.’

Any help would be appreciated.  
Thanks.
