Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Pass variable as parameter to function
- JordanJLyons
-
4 posts
Pass variable as parameter to function
I'm making a game that uses d6 dice to set a number of variables. I'm trying to make a die block that takes one of my variables as a parameter, and then sets it to a random number between 1 and 6.
It seems like I should be able to put a line in my block that says "set to (pick random (1) to (6))“ but when I pick that line from ”variables,' it only allows an existing variable, and doesn't let me drag the parameter into that line.
This is what I would want it to look like:
With the result being that the variable batterx1 has been passed into the block as the parameter, and set to a random number between 1 and 6.
What am I not getting?
You can see the project here: https://scratch.mit.edu/projects/370598146
It seems like I should be able to put a line in my block that says "set to (pick random (1) to (6))“ but when I pick that line from ”variables,' it only allows an existing variable, and doesn't let me drag the parameter into that line.
This is what I would want it to look like:
With the result being that the variable batterx1 has been passed into the block as the parameter, and set to a random number between 1 and 6.
What am I not getting?
You can see the project here: https://scratch.mit.edu/projects/370598146
Last edited by JordanJLyons (Feb. 24, 2020 19:39:45)
- badatprogrammingibe
-
500+ posts
Pass variable as parameter to function
You sadly cannot pass variables as parameters into blocks; scratch only supports pass by value not pass by reference.
- mario19811
-
15 posts
Pass variable as parameter to function
Try this:
or
or
Last edited by mario19811 (Feb. 25, 2020 17:02:14)
- JordanJLyons
-
4 posts
Pass variable as parameter to function
maybe try:
Thank you! On my interface, I can't set say “set (parameter) to …. ” I can only find blocks that allow me to set the variables I have created.
The define block is also pink instead of dark purple. Am I choosing the wrong block or just have a newer interface or something?
- nyankatpro
-
500+ posts
Pass variable as parameter to function
The scratchblocks extension for the forums still uses the old scratch 2.0 blocks, custom blocks were changed to be reddish-pink in 3.0. As for the script, I wasn't that confident it would actually work, so maybe try this:
or better yet, skip the custom block altogether:
Last edited by nyankatpro (Feb. 24, 2020 21:06:10)
- JordanJLyons
-
4 posts
Pass variable as parameter to function
maybe try:
Setting the parameter seems to maybe be functionality that can be used in the Scratch Blocks on the forum but not in the actual application?
- codeman1044
-
1000+ posts
Pass variable as parameter to function
Yes. This is one of the more confusing aspects of the scratchblocks feature of the forums, as certain features seem like they should exist but don't. Setting the parameter seems to maybe be functionality that can be used in the Scratch Blocks on the forum but not in the actual application?
Sadly, there is no clean and clever way to do what you're asking for on scratch, although Snap! has this feature (Snap! is for more advanced users though, so maybe it isn't the best choice to recommend a newer user to scratch.)
The most organized way to do this would probably be one of two things:
1) Use a bunch of if thens to set it properly, each variable paired with an if then:
The second method is more advanced but would behave more like what you're looking for. I have a tutorial/example here. I tried to make it user-friendly and simple, but please let me know if there's anything confusing about the script.
- Discussion Forums
- » Help with Scripts
-
» Pass variable as parameter to function