Discuss Scratch

Gamelovinggamer
Scratcher
86 posts

Creating extensions for Scratch 3.0

NitroCipher wrote:

Creating extensions for Scratch 3.0


There is now official documentation regarding extensions!, but feel free to use this post to get you started


You'll want to start off by creating your extension's class, and register the extension - In my case, this would be ‘NitroBlock’

class NitroBlock { //In both instances, NitroBlock will be the name in both instances
}
Scratch.extensions.register(new NitroBlock());

Next, we will be constructing block and menu definitions - We will continue to use ‘NitroBlock’ through this tutorial
getInfo() {
    return {
        "id": "NitroBlock",
        "name": "NitroBlock",
        "blocks": [
        ],
        "menus": { //we will get back to this in a later tutorial
        }
    };
}

We are going to take a look at how blocks are constructed

For those of you that are familiar with extensions for Scratch 2.0, we will start off with this: - If not, you can ignore this
['r', 'letters %n through %n of %s', 'substringy', '2', '5', 'hello world']
//breakdown below:
['r' = block type, 'letters %n through %n of %s' = block text, 'substringy' = block ID/opcode]

{
    "opcode": "substringy", //This will be the ID code for the block
    "blockType": "reporter", //This can either be Boolean, reporter, command, or hat
    "text": "letters [num1] through [num2] of [string]", //This is the block text, and how it will display in the Scratch interface
    "arguments": { //Arguments are the input fields in the block. In the block text, place arguments in square brackets with the corresponding ID 
        "num1": { //This is the ID for your argument
            "type": "number", //This can be either Boolean, number, or string
            "defaultValue": "2" //This is the default text that will appear in the input field, you can leave this blank if you wish
        },
        "num2": {
            "type": "number",
            "defaultValue": "5"
        },
        "string": {
            "type": "string",
            "defaultValue": "hello world"
        }
    }
},

We will put this newly constructed code into the blocks object above - My code will now look like this
class NitroBlock {
    getInfo() {
        return {
            "id": "NitroBlock",
            "name": "NitroBlock",
            "blocks": [{
                    "opcode": "substringy",
                    "blockType": "reporter",
                    "text": "letters [num1] through [num2] of [string]",
                    "arguments": {
                        "num1": {
                            "type": "number",
                            "defaultValue": "2"
                        },
                        "num2": {
                            "type": "number",
                            "defaultValue": "5"
                        },
                        "string": {
                            "type": "string",
                            "defaultValue": "hello world"
                        }
                    }
                },
            }],
        "menus": { //we will get back to this in a later tutorial
        }
    };
}
Scratch.extensions.register(new NitroBlock());

Next we come to the most important part, the code that actually runs the blocks! - I am keeping this short and simple for tutorial's sake.
//Make sure you name this function with with the proper ID for the block you defined above
substringy({num1, num2, string}) { //these names will match the argument names you used earlier, and will be used as the variables in your code
    //this code can be anything you want
    return string.substring(num1 - 1, num2);  //for reporters and Boolean blocks the important thing is to use 'return' to get the value back into Scratch.
}

Place this new code below your getInfo() function
class NitroBlock {
    getInfo() {
        return {
            "id": "NitroBlock",
            "name": "NitroBlock",
            "blocks": [{
                    "opcode": "substringy",
                    "blockType": "reporter",
                    "text": "letters [num1] through [num2] of [string]",
                    "arguments": {
                        "num1": {
                            "type": "number",
                            "defaultValue": "2"
                        },
                        "num2": {
                            "type": "number",
                            "defaultValue": "5"
                        },
                        "string": {
                            "type": "string",
                            "defaultValue": "hello world"
                        }
                    }
                },
            }],
        "menus": { //we will get back to this in a later tutorial
        }
    };
    substringy({num1, num2, string}) {
        return string.substring(num1 - 1, num2);
    };
}

Save this code to your computer as a js file, in my case it is NitroBlock_3.js

Congratz, you now have your extension code created!!
You can use this tutorial to add it your own personal copy of scratch
Or, you can host the extension with gh-pages, and go here to test it
(provide your own url, don't use mine)

Here is the archived copy of my original post

is that supposed to be making a block? btw i dont understand, if you posted a project with that block and someone without the extension played it, what would happen?

you can never be too old for applesauce
-Gamelovinggamer

Hi! I'm Gamelovinggamer! (aka Barrett) A sorta-nerdy, friendly person. My pronouns are he/him or they/them. I'm either online once a day or 1-4 days a week. I am a HUGE Internet and Mega Man enthusiast! My hobbies are playing Roblox, doing Scratch, editing my website which you can find by clicking here, playing Mega Man, making YT videos which you can find by clicking here My favorite Mega Man games are 1,2,3,7,10, X1, and MMPU (Mega Man: Powered Up)

Links to my Social Medias: Scratch Account l Youtube and those are my social medias.

define -_-
This is Jeremy, my forum guardian. He is sleepy, yet helpful. I will say when Jeremy answers your questions. Jeremy likes to sleep in coding blocks so one day he might pop up in your project.
Rushthe10
Scratcher
1 post

Creating extensions for Scratch 3.0


when [sprite 1] clicked


<I receive [message 1]>


when red sign clicked
Acrylylll
Scratcher
10 posts

Creating extensions for Scratch 3.0

Rushthe10 wrote:


when [sprite 1] clicked


<I receive [message 1]>


when red sign clicked
these blocks are in the banned blocks list.
A-MARIO-PLAYER
Scratcher
1000+ posts

Creating extensions for Scratch 3.0

Acrylylll wrote:

(#823)

Rushthe10 wrote:


when [sprite 1] clicked


<I receive [message 1]>


when red sign clicked
these blocks are in the banned blocks list.
please be more constructive and actually quote or link to TOLORS.

moved to @8to16 - this account is an archive





Amp hall of fame
Any posts I made before 2024 are bad posts.
1000th post (no edit)
ban scratch cause you can hack it
i only know 5 digits of pi
quote spam to the extreme
slatch.mit.edu
dying of cringe scratchblocks
Workaround: Summon it





buahauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhauhau
the laugh is so evil your tiny screen can't comprehend it
THE_diamond_2021cat
Scratcher
24 posts

Creating extensions for Scratch 3.0

rest for (idk why im still here tbh) beats

꧁•⊹٭DiamondCat٭⊹•꧂
ExpungleCat2023
Scratcher
13 posts

Creating extensions for Scratch 3.0

SuperlabProductions wrote:

Ooga booga

define [quote]abc[/quote]

My name is Maxx. I was the loyal “pet” cat to Expunged, but I ran away. Want to join the Expunged Empire? Sign up here: https://scratch-mit-edu.ezproxy.canberra.edu.au/projects/822961685
add [Expunged] to [Peeps i ran away from v]
Owlgriffin
Scratcher
15 posts

Creating extensions for Scratch 3.0

Hello!

I'm currently working on porting a ScratchX extension to Scratch 3 and I'm having some issues.
This extension uses websockets to connect to an smartphone running an app, but I don't know how to import the websocket library in the extension, I keep getting “object not clonable” errors.
Did anyone here implement websockets on a Scratch3 extension succesfully?

I really wish I could help, but I can’t
when green flag clicked
if <question I can’t do> then
say [sorry!] for (2) secs
end

The quote will not work but the comment was by

Last edited by Owlgriffin (Aug. 14, 2024 00:37:56)


Owlgriffin

Hi! This is just my signature
12233344445555566666677777778888888999999999
Saifan2014
Scratcher
100+ posts

Creating extensions for Scratch 3.0

NitroCipher wrote:

Creating extensions for Scratch 3.0


There is now official documentation regarding extensions!, but feel free to use this post to get you started


You'll want to start off by creating your extension's class, and register the extension - In my case, this would be ‘NitroBlock’

class NitroBlock { //In both instances, NitroBlock will be the name in both instances
}
Scratch.extensions.register(new NitroBlock());

Next, we will be constructing block and menu definitions - We will continue to use ‘NitroBlock’ through this tutorial
getInfo() {
    return {
        "id": "NitroBlock",
        "name": "NitroBlock",
        "blocks": [
        ],
        "menus": { //we will get back to this in a later tutorial
        }
    };
}

We are going to take a look at how blocks are constructed

For those of you that are familiar with extensions for Scratch 2.0, we will start off with this: - If not, you can ignore this
['r', 'letters %n through %n of %s', 'substringy', '2', '5', 'hello world']
//breakdown below:
['r' = block type, 'letters %n through %n of %s' = block text, 'substringy' = block ID/opcode]

{
    "opcode": "substringy", //This will be the ID code for the block
    "blockType": "reporter", //This can either be Boolean, reporter, command, or hat
    "text": "letters [num1] through [num2] of [string]", //This is the block text, and how it will display in the Scratch interface
    "arguments": { //Arguments are the input fields in the block. In the block text, place arguments in square brackets with the corresponding ID 
        "num1": { //This is the ID for your argument
            "type": "number", //This can be either Boolean, number, or string
            "defaultValue": "2" //This is the default text that will appear in the input field, you can leave this blank if you wish
        },
        "num2": {
            "type": "number",
            "defaultValue": "5"
        },
        "string": {
            "type": "string",
            "defaultValue": "hello world"
        }
    }
},

We will put this newly constructed code into the blocks object above - My code will now look like this
class NitroBlock {
    getInfo() {
        return {
            "id": "NitroBlock",
            "name": "NitroBlock",
            "blocks": [{
                    "opcode": "substringy",
                    "blockType": "reporter",
                    "text": "letters [num1] through [num2] of [string]",
                    "arguments": {
                        "num1": {
                            "type": "number",
                            "defaultValue": "2"
                        },
                        "num2": {
                            "type": "number",
                            "defaultValue": "5"
                        },
                        "string": {
                            "type": "string",
                            "defaultValue": "hello world"
                        }
                    }
                },
            }],
        "menus": { //we will get back to this in a later tutorial
        }
    };
}
Scratch.extensions.register(new NitroBlock());

Next we come to the most important part, the code that actually runs the blocks! - I am keeping this short and simple for tutorial's sake.
//Make sure you name this function with with the proper ID for the block you defined above
substringy({num1, num2, string}) { //these names will match the argument names you used earlier, and will be used as the variables in your code
    //this code can be anything you want
    return string.substring(num1 - 1, num2);  //for reporters and Boolean blocks the important thing is to use 'return' to get the value back into Scratch.
}

Place this new code below your getInfo() function
class NitroBlock {
    getInfo() {
        return {
            "id": "NitroBlock",
            "name": "NitroBlock",
            "blocks": [{
                    "opcode": "substringy",
                    "blockType": "reporter",
                    "text": "letters [num1] through [num2] of [string]",
                    "arguments": {
                        "num1": {
                            "type": "number",
                            "defaultValue": "2"
                        },
                        "num2": {
                            "type": "number",
                            "defaultValue": "5"
                        },
                        "string": {
                            "type": "string",
                            "defaultValue": "hello world"
                        }
                    }
                },
            }],
        "menus": { //we will get back to this in a later tutorial
        }
    };
    substringy({num1, num2, string}) {
        return string.substring(num1 - 1, num2);
    };
}

Save this code to your computer as a js file, in my case it is NitroBlock_3.js

Congratz, you now have your extension code created!!
You can use this tutorial to add it your own personal copy of scratch
Or, you can host the extension with gh-pages, and go here to test it
(provide your own url, don't use mine)

Here is the archived copy of my original post
My browser / operating system: Linux, Chrome 127.0.0.0, No Flash version detected

ADYAN_MAHEER
Scratcher
2 posts

Creating extensions for Scratch 3.0

class NitroBlock {
getInfo() {
return {
“id”: “NitroBlock”,
“name”: “NitroBlock”,
“blocks”: [{
“opcode”: “substringy”,
“blockType”: “reporter”,
“text”: "letters through of “,
”arguments“: {
”num1“: {
”type“: ”number“,
”defaultValue“: ”2“
},
”num2“: {
”type“: ”number“,
”defaultValue“: ”5“
},
”string“: {
”type“: ”string“,
”defaultValue“: ”hello world“
}
}
},
}],
”menus": { //we will get back to this in a later tutorial
}
};
substringy({num1, num2, string}) {
return string.substring(num1 - 1, num2);
};
}

Powered by DjangoBB