Access to __name__ in block code? #2420
-
|
Is there anyway to access the I am using pybricks blocks for an FLL team for 4 graders. It has been working great, but now we need to combine everyone's program with a top-level menu. I foolishly assumed that we could just import all the mission files, and then use the menu to select which one to run. While this would infact be trivial if we were using code, I can't figure out how to do it with blocks. I have several groups working on the code, and combining the code into a single file is not really a viable solution. Inevitably somebody's code is going to get messed up/deleted. It is much safer if they all work in their own file. The Ideal situation: Another potential solution that could work is if I can pin the top-level program so that hitting play always downloads the pinned program instead of the currently visible program. Right now the only solution I have involves the kids constantly switching between the top level file and the task file they are currently editing. This is extra confusing to the younger kids because the top level file can't be blocks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
We ran into the same problem in my team, and the solution we found was kinda hacky. Basically, use a number in the hub storage to detect if the program was being run individually or being imported in the menu. Something like this: In the menu program: hub.system.storage(0, write=(1).to_bytes(1, "little"))
from program_1 import program_1
# Rest of the programs
hub.system.storage(0, write=(0).to_bytes(1, "little"))And in each individual programs for the launches (for example, if int.from_bytes(hub.system.storage(0, read=1), "little") == 0:
# If it reads 0, the program is being launched directly.
# If it reads 1, it's being imported in the menu.
program_1()With function IIRC, we also used a "dummy" hub object to write and read the storage, but I'm not sure if that was necessary. |
Beta Was this translation helpful? Give feedback.
-
You can technically do that like this. The main program will be a Python listing of the file, but all the programs can use blocks as illustrated in that example. (I can see this is confusing. In the mean time, we're working on alternatives to simplify this, no menu program required.)
I don't know exactly what you would like to build, but yes you could make an
|
Beta Was this translation helpful? Give feedback.



You can technically do that like this. The main program will be a Python listing of the file, but all the programs can use blocks as illustrated in that example.
(I can see this is confusing. In the mean time, we're working on alternatives to simplify this, no menu program required.)
I don't know exactly what you would like to build, but yes you could make an
is_mainblock available to all your programs, like this: