Discuss Scratch

JordanJLyons
New Scratcher
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:

definerolldiesetdietopickrandom1to6whenclickedrollbatterx1

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
Scratcher
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
Scratcher
15 posts

Pass variable as parameter to function

Try this:
whenthisspriteclickedsaypickrandom1to6for2secs

or

whenclickedforeverifmousedown?thensaypickrandom1to6for2secs

Last edited by mario19811 (Feb. 25, 2020 17:02:14)

nyankatpro
Scratcher
500+ posts

Pass variable as parameter to function

maybe try:
definerolldiesetdietopickrandom1to6
JordanJLyons
New Scratcher
4 posts

Pass variable as parameter to function

nyankatpro wrote:

maybe try:
definerolldiesetdietopickrandom1to6

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
Scratcher
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:
definerolldiesetresulttopickrandom1to6. . .rolldiesetvariabletoresult. . .
or better yet, skip the custom block altogether:
setvariabletopickrandom1to6

Last edited by nyankatpro (Feb. 24, 2020 21:06:10)

JordanJLyons
New Scratcher
4 posts

Pass variable as parameter to function

nyankatpro wrote:

maybe try:
definerolldiesetdietopickrandom1to6

definerolldiesetdietopickrandom1to6
JordanJLyons
New Scratcher
4 posts

Pass variable as parameter to function

JordanJLyons wrote:

nyankatpro wrote:

maybe try:
definerolldiesetdietopickrandom1to6

definerolldiesetdietopickrandom1to6

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
Scratcher
1000+ posts

Pass variable as parameter to function

JordanJLyons wrote:

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?
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.
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:
batterx1batterx2batterx3rollbatterdefinerolldieifdie=batterx1thensetbatterx1topickrandom1to6elseifdie=batterx2thensetbatterx2topickrandom1to6elseif...then. . .etc etc
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.

Powered by DjangoBB