Hello,
I am trying to create a extension that may need to use the Microbit A/B buttons during startup depending on some settings and some runtime checks. The best way I found to do this is to ensure the setup code is ran before any user events get bound, as they can be used normally after the setup is done running, and should not fire during the startup.
Now the question is 2 part.
1: Can I force code to be run before the main code is ran, no matter if it’s blocks, javascript, python, etc? And if so, how?
2: I need to allow the user to be able to select which devices/modules are to be active. Is there a way to add custom settings in for example the project settings, and then read those out by the extension? Otherwise a block that sets these variable before or while the startup code is running.
Otherwise, if this is not possible. Can I at least force a startup block to be run before any events are bound like the input.onbuttonpress()? (Currently I can only get that to work in the javascript editor by manually moving the startup function to the top, but I need this to work in blocks)
1 Like
The first part seems maybe possible.
The custom.ts in this example (could be in an extension) holds up main.ts if the logo is held within 2s, and until button B is pressed, button A plays a tone
The main blocks create their own button handlers.
I don’t know if this is a good idea though!
1 Like
This is nice, but I had really hoped i got to know how to do the second part. Guess I will have to stick with the default configuration for the modules for now.
1 Like
Could you add a config block that runs the set up code and just require that the user run that block first? Something like “start with config…”?
And yes as Martinwork found, the main.ts and blocks files always run last, so putting your extension into a custom .ts file should make it run before any user code.
2 Likes
That is my current solution, but the problem is that i may need to use the a/b buttons during startup (for some configs) which means that either:
- I overwrite any a/b event the user already had defined.
2: The a/b user events fire whenever you press a/b during setup.
Defining the events before the config block also would not work as then the user events will overwrite the once I defined.
1 Like
You can use the game.pushScene() and game.pullScene() functions to send user defined button events away onto the scene stack while you run your own stuff and then pull them back once you are done. This is how blocks like the Ask For String block work.
@WoofWoof this is for micro:bit, not arcade! there’s no concept of scenes in micro:bit
1 Like
@Keventor you can pass values from pxt.json to C++ files via the yotta config, but you won’t have any fancy UI for doing so. users will have to edit their pxt.json files manually, which is generally not recommended for the layperson since messing up the JSON can break a project.
alternatively, they can define them using the userconfig like so:
namespace userconfig {
export const MY_VALUE = 1234;
}
which has the benefit of not having to edit JSON files, but does mean that they need to open the JavaScript editor.