I have two questions where the value block is a variable by itself and I want to add the string "end" to the end of the function.

31 views
Skip to first unread message

Akina

unread,
Jun 18, 2024, 5:25:30 PM (11 days ago) Jun 18
to Blockly
I am currently working on a tool to generate Lua code in Blockly to simplify the code of a library.
However, two problems have arisen.
1. When I connect a value block to an If, it doesn't go into a conditional expression.
I want to connect a value block with an If so that it enters the conditional expression of the if,
(e.g. if <value block generated code> then)
When I connect it with if or while, the code is generated as a variable.
What should I do?
(Some sentences may be incorrect due to the use of translation.)
Html Code
        Blockly.Blocks['getWidth'] = {
            init: function () {
                this.appendDummyInput()
                    .appendField("Text");
                this.setInputsInline(true);
                this.setOutput(true, null); this.
                this.setColour(225); this.
                this.setTooltip(""); this.
                this.setHelpUrl(""); this.setHelpUrl(""); }
            }
        }; }
JavaScript Code
        lua.luaGenerator.forBlock['getWidth'] = function (block, generator) {
            // TODO: Assemble lua into code variable.
            var code = 'getWidth()'; //<- Execute and a number will be returned as return value
            // TODO: Change ORDER_NONE to the correct strength.
            return code;
        };
2. How to add end to the tail of a function
If you place an event block (although it looks like an event block, it is just a function inside) When I place an event block (which is just a function) in a workspace, I want it to look like the following.
Currently I am having trouble with the end not being added to the end.
function EventName()
// Code for the connected block
end
--------
HTML Code
        Blockly.Blocks['event'] = {
            init: function () {
                this.appendDummyInput()
                    .appendField("text");
                this.setInputsInline(true);
                this.setNextStatement(true, null); this.
                this.setColour(0); this.
                this.setTooltip(""); this.
                this.setHelpUrl(""); this.setHelpUrl(""); }
            }
        }; }
Javascript Code
        Blockly.Blocks['event'] = {
            init: function () {
                this.appendDummyInput()
                    .appendField("text");
                this.setInputsInline(true);
                this.setNextStatement(true, null); this.
                this.setColour(0); this.
                this.setTooltip(""); this.
                this.setHelpUrl(""); this.setHelpUrl(""); }
            }
        };

Maribeth Moffatt

unread,
Jun 18, 2024, 6:18:01 PM (11 days ago) Jun 18
to Blockly
Hello,

First, I would encourage you to read our docs about code generation if you haven't already read them. The main idea to wrap your head around is that we're just generating strings from blocks at this stage; we're not executing them. Generally, blocks only define code that is relevant to that exact block, and it can take into account any child blocks that are attached to it. It's rare that a block cares about its parent blocks during code generation.

What this means for your getWidth block is that your block should probably just return the code `getWidth()` like you have. The "if" block will then handle concatenating that code into the `if <value input block's code here> then <statement block's code here>` code block. The custom block doesn't need to check if it's in an if/while loop because the code generated for the custom block itself doesn't change regardless.

Do also note that since your width block has an output connection, the code generation needs to return an array containing not just the code, but also an operator precedence strength. Right now your generator function does not return an array, and this may cause errors.

For your second question, you haven't included your block-code generator function so it's difficult to debug. But there is an example in the docs about generating code for statement blocks. You can get the code for the blocks connected to the statement input, then include that code in your final code string returned by the event block.

If you need additional help, we need more information from you.
For the 1st question, please include the actual code generated when using your block and what you expect it to be instead. For example, create a workspace that contains an `if` block connected to your custom block. Generate code for that workspace and show us the result. Then tell us how you want it to be different.
For the 2nd question, please include the block-code generator function as well as the above information.

I hope that helps! With additional info I'd be happy to clarify further. Best,
Maribeth

Reply all
Reply to author
Forward
0 new messages