Discuss Scratch
- Discussion Forums
- » Suggestions
- » A "Clamp" Block
- M0nk3yMan714
-
Scratcher
28 posts
A "Clamp" Block
I was doing a project recently, and I thought that it would be nice to have a clamp block. This block would have a slot for a number, a slot for the minimum, and a slot for the maximum. If the number is greater than the maximum, return the maximum. If the number is less than the minimum, return the minimum. This would go in the operators section, and it would make code a lot cleaner than, say, writing out an entire if statement chain for that.
Instead of:
if(x > maximum) { return maximum }
else if(x < minimum) { return minimum }
else { return x };
It would be:
clamp(x, maximum, minimum);
(Sorry, I don't know how to make the scratch blocks show up in a forum post - I'm new to this!)
Instead of:
if(x > maximum) { return maximum }
else if(x < minimum) { return minimum }
else { return x };
It would be:
clamp(x, maximum, minimum);
(Sorry, I don't know how to make the scratch blocks show up in a forum post - I'm new to this!)
- M0nk3yMan714
-
Scratcher
28 posts
A "Clamp" Block
Basically, if the number you put in the block is greater than a specified max value, it would simply give you the maximum value. If you put in a number less than a specified minimum value, then it will give you the minimum value. I'm a Javascript/C# programmer, so sorry about the code.
- MultigenderMess-_
-
Scratcher
100+ posts
A "Clamp" Block
Basically, if the number you put in the block is greater than a specified max value, it would simply give you the maximum value. If you put in a number less than a specified minimum value, then it will give you the minimum value. I'm a Javascript/C# programmer, so sorry about the code...What??
No support, if I can't understand half of this I'm sure a 8 year old wouldn't understand it either
- M0nk3yMan714
-
Scratcher
28 posts
A "Clamp" Block
Okay. Sorry.
Basically (2.0)
The block restrains the value entered to a certain specified range.
If it is outside of the range because it is too large, then just spits out the maximum value that the range allows.
If it is outside of the range because it is too small, then it just spits out the minimum value that the range allows.
Example:
would spit out 5, because 10 is bigger than the maximum specified (5)
Sorry for the confusion
Basically (2.0)
The block restrains the value entered to a certain specified range.
If it is outside of the range because it is too large, then just spits out the maximum value that the range allows.
If it is outside of the range because it is too small, then it just spits out the minimum value that the range allows.
Example:
((10) clamp (5) maximum, (-5) minimum)
would spit out 5, because 10 is bigger than the maximum specified (5)
Sorry for the confusion
Last edited by M0nk3yMan714 (March 16, 2023 19:11:14)
- Basilikos
-
Scratcher
1000+ posts
A "Clamp" Block
You can make a custom block for it :
and do the whole thing. Since the name of the function is not intuitive and since it's easy to build up I don't think Scratch would benefit from this.
The concept's easy too, so encountering it in other programming languages won't be a challenge.
define clamp () ()
and do the whole thing. Since the name of the function is not intuitive and since it's easy to build up I don't think Scratch would benefit from this.
The concept's easy too, so encountering it in other programming languages won't be a challenge.
- M0nk3yMan714
-
Scratcher
28 posts
A "Clamp" Block
Well, yes. I got this idea from Unity (C#). I just think it would be useful, as scratch does not allow you to make a custom block that returns a value.
(No blocks with () edges or <> edges)
(No blocks with () edges or <> edges)
- Basilikos
-
Scratcher
1000+ posts
A "Clamp" Block
By the way that is just an “”approximation“” using a block instead of a variable block, but you can set a variable to the argument later.
- M0nk3yMan714
-
Scratcher
28 posts
A "Clamp" Block
Oh… true. I still think it would be useful, as it saves a lot of annoying coding. I kind of forgot about being able to use variables for this. Still, I think this would be cool. Thank you, though.


- Basilikos
-
Scratcher
1000+ posts
A "Clamp" Block
Oh… true. I still think it would be useful, as it saves a lot of annoying coding. I kind of forgot about being able to use variables for this. Still, I think this would be cool. Thank you, though.If you really want it you could code it yourself, backpack the custom block with its functionalities and use it in later projects.
- medians
-
Scratcher
1000+ posts
A "Clamp" Block
Please explain what half of this meansIt would basically just be this, I believe:

The first part just checks which is the bigger number pretty much.
- M0nk3yMan714
-
Scratcher
28 posts
A "Clamp" Block
Okay, I made my own version using custom blocks.
define clamp (num) (min) (max)
set [return] to [0]
if <(num) > (max)> then
set [return] to (max)
else
if <(num) < (min)> then
set [return] to (min)
else
set [return] to (num)
end
end
Last edited by M0nk3yMan714 (March 17, 2023 16:33:25)
- Basilikos
-
Scratcher
1000+ posts
A "Clamp" Block
Okay, I made my own version using custom blocks.Now that I think of it, you could use a join block to add a specific ID to the return variable, and put every “”clamped numbers“” into a list, to avoid making an absurdly high amount of variables.define clamp (num) (min) (max)
set [return] to [0]
if <(num) > (max)> then
set [return] to (max)
else
if <(num) < (min)> then
set [return] to (min)
else
set [return] to (num)
end
end
- Sonicfan2115
-
Scratcher
20 posts
A "Clamp" Block
I used this in one of my turbo warp scripts:
we need the
(clamp (direction) from(60) to (120))for gravity.
we need the
(clamp () from() to ())block now or else I will
create clone of [myself]
delete this clone
- Discussion Forums
- » Suggestions
-
» A "Clamp" Block




