This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
blockly's profile picture

A Sneak Peek At Blockly Lua

Started by blockly, 31 October 2013 - 03:16 PM
blockly #1
Posted 31 October 2013 - 04:16 PM
I've been working on an extension to Blockly to make it possible to generate ComputerCraft programs by dragging and dropping blocks (similar to Scratch and App Inventor for Android). Give it a try at "http://blockly-lua.appspot.com/static/apps/code/index.html". For an idea of what can be done, here's a program I wrote to dig a spiral staircase. Feel free to play with it. (Only you will see the changes, unless you save your modified programming by clicking on the link icon in the upper right corner and share the resulting URL.)

Planned additions:
  • More of the ComputerCraft API.
  • Support for OpenPeripheral.
  • Pastebin integration.
I could use community input on what features should be highest priority. It would also be great for someone to chip in with documentation or code contributions. Feel free to PM me or post here.
amtra5 #2
Posted 31 October 2013 - 04:31 PM
Very nice! Also, could you put the source on Github, as I would also like to work on this project ;)/>
blockly #3
Posted 31 October 2013 - 04:43 PM
Very nice! Also, could you put the source on Github, as I would also like to work on this project ;)/>

Already done! Here's the URL: https://github.com/espertus/blockly-lua
blockly #4
Posted 31 October 2013 - 10:16 PM
Here's a mining program created in Blockly Lua. Before running it, blocks that the player is NOT interested in should be placed in slots 12-16 of the turtle's inventory. The program digs straight down. At each step, the turtle looks around at adjacent blocks, digging any that differ from the uninteresting blocks, expanding recursively, so it follows any veins. Here's the Blockly link: "http://blockly-lua.appspot.com/static/apps/code/index.html#bv3ys8".

The code can be found in the Lua tab at the above link or at "http://pastebin.com/Ey0im3XJ".
theoriginalbit #5
Posted 31 October 2013 - 10:26 PM
One question… in your code where you're searching for a "sensor" peripheral… why do you look for "peripheral" instead of "sensor"?
amtra5 #6
Posted 01 November 2013 - 01:23 AM
How would you add blocks? I've followed the tutorial on the wiki with no avail :(/>
Zudo #7
Posted 01 November 2013 - 02:08 AM
I thought this would be bad, but it is actually quite good! Definitely sharing with my friends, who want to learn but don't know how :P/>
blockly #8
Posted 01 November 2013 - 12:01 PM
One question… in your code where you're searching for a "sensor" peripheral… why do you look for "peripheral" instead of "sensor"?

Good to see you here. You know I'm a fan of yours.

I forgot that I'd left in that code. The reason for it is that I'd always interacted with a sensor by attaching it as a peripheral to a turtle and accessing it through the peripheral interface. How would you recommend that I access it?
blockly #9
Posted 01 November 2013 - 12:02 PM
How would you add blocks? I've followed the tutorial on the wiki with no avail :(/>

I'll start work on a Lua-specific tutorial. If you tell me what you were trying to do and where it got confusing, that would be helpful.
blockly #10
Posted 01 November 2013 - 02:30 PM
I've started on a tutorial: https://github.com/e...w-to-Add-Blocks
amtra5 #11
Posted 01 November 2013 - 05:30 PM
How would you add blocks? I've followed the tutorial on the wiki with no avail :(/>/>

I'll start work on a Lua-specific tutorial. If you tell me what you were trying to do and where it got confusing, that would be helpful.
I had a bit of spare time and I was going to finish the OS api for you ;)/>
blockly #12
Posted 01 November 2013 - 07:33 PM
I had a bit of spare time and I was going to finish the OS api for you ;)/>

Super! I've been cleaning up the code and working on the documentation, so you may be able to get started. Don't be shy about asking me questions.
Zudo #13
Posted 02 November 2013 - 12:31 AM
Error: Not Found

The requested URL /static/apps/code/index.html was not found on this server.


amtra5 #14
Posted 02 November 2013 - 01:44 AM
Also, you may want to somehow disable the default blockly colour blocks from being able to interact with CC logic blocks.

Edit: What would I add to template.soy so that it recognises my block?
theoriginalbit #15
Posted 02 November 2013 - 02:54 AM
I forgot that I'd left in that code. The reason for it is that I'd always interacted with a sensor by attaching it as a peripheral to a turtle and accessing it through the peripheral interface. How would you recommend that I access it?
Like this
blockly #16
Posted 02 November 2013 - 02:49 PM
Error: Not Found The requested URL /static/apps/code/index.html was not found on this server.

Yikes! Thanks for letting me know. Fixed.
blockly #17
Posted 02 November 2013 - 02:57 PM
Also, you may want to somehow disable the default blockly colour blocks from being able to interact with CC logic blocks.

Great idea!

Edit: What would I add to template.soy so that it recognises my block?

Copy one of the existing definitions between lines 94 and 116, changing the name to that of your block. Make sure you place it in the right category.

Update: I removed template.soy and generated/en.js. Instead, copy an existing block definition from between lines 106 and 128 of index.html.

The reason some blocks have VALUE children is so they come with an inner number/text block.

Then run the command at the top of the file to re-generate en.js. (I'm going to see if I can eliminate that step.)

I'll document in full later but wanted to give you a reply ASAP. I hope it's understandable. If not, ask again.

Thanks for helping out.
blockly #18
Posted 03 November 2013 - 12:29 PM
I've completed (the initial version of) How to Add a Block.
blockly #19
Posted 04 November 2013 - 11:21 PM
Thanks to theoriginal bit, all of the turtle functions have been added.
Skydyn #20
Posted 05 November 2013 - 02:30 AM
Neat. I tried making an IDE type thing sorta like this for a different language, but gave up because I didn't find it particularly useful. Typing is just easier. Do you think it'll have an impact on beginners?

Also, have you ever used ALICE?
blockly #21
Posted 05 November 2013 - 12:39 PM
Neat. I tried making an IDE type thing sorta like this for a different language, but gave up because I didn't find it particularly useful. Typing is just easier.

Agreed. It's not what I'd want to use for programming myself.

Do you think it'll have an impact on beginners?

Other block-based languages, such as Scratch, Alice, and App Inventor (which I worked on) have had a big impact on beginners. One of the unique things about Blockly is that it generates code in a conventional language, which we hope eases the transition to textual languages.

Whether Blockly appeals to the Minecraft community, I don't know. I hope it will. If you can help spread the word or improve it, that would be great!

Also, have you ever used ALICE?

I'm not sure I've actually used it, although of course it's had a big impact on CS education, my field. Blockly has more of an MIT influence (StarLogo, Scratch, and App Inventor, which was developed at Google by mostly MIT people).
elfin8er #22
Posted 05 November 2013 - 03:30 PM
Wow, this is really neat! I see you mentioned Scratch. I used to use Scratch all the time. It'd be nice of it could have more of a scratch feeling to it. Other than that, keep up the good work!
blockly #23
Posted 05 November 2013 - 07:46 PM
It is ugly, but we now have Pastebin integration. I'm hoping that someone who knows CSS will be sufficiently disgusted by the appearance that they'll volunteer to improve it. :)/>
blockly #24
Posted 07 November 2013 - 09:53 PM
I'd like some input on how to show the blocks. The list of 17 blocks in the Turtle category is very long (barely all visible on a 24" monitor). Take a look.

I can think of three ways of making the list more manageable:
  1. Combining similar blocks with a dropdown menu. For example "get item count in slot" and "get free space in slot" would be on the same block, with a dropdown menu to choose between them. The downside is the user might not know that the non-visible choice is there. You can see this in the right thumbnail below.
  2. Adding another level of hierarchy. You can play with this on this test server. I left the most popular blocks at the top level. Disadvantages are it takes more clicks to get to the desired block and users might not notice the sub-categories. You can see this in the left thumbnail below.
  3. Dividing Turtle into four categories: "Turtle Movement", "Turtle Mining and Fighting", "Turtle Inventory", and "Turtle Fuel". (I'm not sure whether to list categories alphabetically or most to least important.)
What do people think?
theoriginalbit #25
Posted 07 November 2013 - 10:06 PM
Well our discussions on have been leaning more towards #1, but if you don't want to do that then of the other choices definitely #3.
blockly #26
Posted 08 November 2013 - 12:11 AM
Well our discussions on have been leaning more towards #1, but if you don't want to do that then of the other choices definitely #3.

I'm just not sure which way to go. It sounds like you prefer 1 to 3?

Here are some ideas for blocks to combine:
  • get [item count/free space] in slot #
  • [drop/place/suck] item [in front/up/down]
  • either
    • [move/compare items] in selected slot to slot #
    • compare [front/up/down/slot #] to selected slot
  • [dig/attack/detect?] [in front/up/down]
Some of these combinations are questionable, and there still would be more than ten blocks, so I'm just not sure which way is best.
theoriginalbit #27
Posted 08 November 2013 - 01:12 AM
Well ~10 blocks is better that 35


  • get [item count/free space] in slot #
  • [drop/place/suck] item [in front/up/down]
  • either
    • [move/compare items] in selected slot to slot #
    • compare [front/up/down/slot #] to selected slot
  • [dig/attack/detect?] [in front/up/down]
those combinations could work with the change of [dig/attack/detect/compare] since its [move/compareTo] not [move/compare]
Edited on 08 November 2013 - 12:13 AM
blockly #28
Posted 12 November 2013 - 02:13 PM
get [item count/free space] in slot #

I ended up only making this first change. I felt that the others, where the primary verb was in the dropdown menu, were more likely to confuse than help the user. There's no reason that seeing "[dig] [in front]" should make an inexperienced user expect that block to also contain detect, attack, etc.
blockly #29
Posted 12 November 2013 - 02:55 PM
Here's another program, which makes a wall using the pattern specified by a list. For example, the list
{ {1, 2, 1, 2, 1}, {2, 1, 2, 1, 2} }
makes a wall 5 blocks wide, 2 blocks high, in a checkerboard pattern, with half the blocks from slot 1 and half from slot 2.




Links:
blockly #30
Posted 15 November 2013 - 09:16 PM
I've pushed a bunch more updates to Blockly Lua, including peripherals blocks. The Original Bit has been a big help.
theoriginalbit #31
Posted 15 November 2013 - 10:56 PM
One thing I did notice with this latest code is this

print(table.concat({'Spiralled down ', #path, ' steps.'}))
definitely not needed, and not very efficient, the following code would produce the same output

print("Spiralled down "..#path.." steps.")
the only time table.concat really needs to come into play is when there is a table that wants to be output. Anything being concatenated to a string with .. is tostring'd except nil and booleans, which of course if you wanted to be on the safe side you could do

print("Spiralled down "..tostring(#path).." steps.")
Also as an extra point, print is a vararg function meaning that this would also work

print("Spiralled down ", tostring(#path), " steps.")

I've pushed a bunch more updates to Blockly Lua, including peripherals blocks. The Original Bit has been a big help.
Not to be picky, but it is TheOriginalBIT, theoriginalbit, or BIT. :)/>
blockly #32
Posted 16 November 2013 - 12:43 AM
One thing I did notice with this latest code is this

print(table.concat({'Spiralled down ', #path, ' steps.'}))
definitely not needed, and not very efficient, the following code would produce the same output

print("Spiralled down "..#path.." steps.")
the only time table.concat really needs to come into play is when there is a table that wants to be output. Anything being concatenated to a string with .. is tostring'd except nil and booleans, which of course if you wanted to be on the safe side you could do

print("Spiralled down "..tostring(#path).." steps.")
Also as an extra point, print is a vararg function meaning that this would also work

print("Spiralled down ", tostring(#path), " steps.")

Thank you. I'll improve the code, unless you'd like to first. :)/>

It's been tough to decide whether to improve existing code or add more.

I've pushed a bunch more updates to Blockly Lua, including peripherals blocks. The Original Bit has been a big help.
Not to be picky, but it is TheOriginalBIT, theoriginalbit, or BIT. :)/>

Thank you. I won't make that mistake again. I'll make others. :unsure:/>
blockly #33
Posted 19 November 2013 - 05:13 PM
I pushed a new version with lots more of the API. I did a refactoring, so it should be easier to add new blocks, although my documentation is now out-of-date.
blockly #34
Posted 03 December 2013 - 01:30 PM
I pushed a new version to the server with bug fixes and lots more blocks.