Discuss Scratch

Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

Hello! I am having quite a bit of trouble using variables…

I'm trying to make a game where you can choose what your character looks like (which are all separate sprites). Later on in the game, I want to have that said character that was chosen show up in conversations (like RPG style), but I cannot get this to work. I am using a variable that will set itself to a given number if a certain sprite is clicked (like character one is numbered 1, two is numbered 2, ect). I have it where it's supposed to use the variable's number later on, and pop up a face corresponding with that number.

However it doesn't seem to be working. Instead, it acts as though the variable's value just disappears and resets itself back to 0 after a small time frame.

I have tried doing this:
if <(character) = [1]> then 
broadcast [character 1 face]


else
if <(character) = [2]> then
broadcast [character 2 face]
end
end
But it doesn't seem to work at all (not even the first part works).

What am I doing wrong? Is there another code or way I could do in order to do this?
VintageAura
Scratcher
100+ posts

Using Variables+If/Else Block with More than 1 If

fixes the problem, not the code
switch costume to (hairnum)
Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

VintageAura wrote:

fixes the problem, not the code
switch costume to (hairnum)

That is what I refer to as code. From what I understand, that's making a sprite switch to a variable? I don't understand how that works. I also have more than one sprite that could be used.
Darter15
Scratcher
64 posts

Using Variables+If/Else Block with More than 1 If

I used to have this problem all the time. You need to add a “forever” loop
DerpyHead0
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

for if else with more than 1 if, you can usually just:
if <> then
end
if <> then
end
or
if <> then
else
if <> then
else
end
end

for what you want to do here, you probably shouldn't do this:
if <(thing)=[1]> then
do (1) :: grey
end
if <(thing)=[2]> then
do (2) :: grey
end
...
and do this:
do (thing) :: grey




anyways, for the variable resetting to 0, you should probably share the project so we can figure that out.
Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

I see. The problem is is that I have more than one possible character, meaning that more than one sprite could potentially be shown in a certain spot. I do actually have a forever loop on it (and tried it without it), but I'm not sure if that block of code being put in with another block (that controls the dialogue box and characters that will pop up) is affecting it in a way.

My project, however, is here (only 1 and 2 are “working”) if you want to take a look at the code. Thanks to all of you! I appreciate it very much!

Last edited by Asterlie (Aug. 16, 2018 14:30:19)

mstone326
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

One issue you have it setting cat character to grey and you have another script that says set cat character to white. However, I see that you put the orange variable reporter into the set cat character as seen below. That won't work, actually white is set to 0. So you are setting cat character to 0 and not “white”.



set [cat character v] to (white)

You need to type the cat character in the block as seen below. Now, cat charater is set to white. Whereas above you are setting it to 0.

set [cat character v] to [white]
Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

mstone326 wrote:

One issue you have it setting cat character to grey and you have another script that says set cat character to white. However, I see that you put the orange variable reporter into the set cat character as seen below. That won't work, actually white is set to 0. So you are setting cat character to 0 and not “white”.



set [cat character v] to (white)

You need to type the cat character in the block as seen below. Now, cat charater is set to white. Whereas above you are setting it to 0.

set [cat character v] to [white]

So I need to replace the original block with the new one you suggested? So I basically just need to type out the variable rather than drag the orange block? I did this and it didn't seem to work. The value of “cat character” is still “resetting” itself somehow. It actually changes the value of the variable, but it sets itself back to zero (what it was doing before). Do I need to do this a different way? Is there an issue with the receiving part of the code? I'm starting to wonder if that's part of the problem, though I'm clueless on how to fix it.

I have the receiving half in the “Dialogue” sprite (where it will broadcast a message back to the character cats if the value matches), but I feel as though it is sloppy or there could be a better way of doing that.

Sorry for bombarding you with questions and thank you so much for your help!
deck26
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

I don't think you set the variables white or grey at all so they are both 0. If so there is no difference between any of the 3 blocks below, they all do the same thing

set [variable v] to [0]
set [variable v] to (white)
set [variable v] to (grey)
If you mean the variables to actually have a numeric value which you can then use to give Cat character a number you need to decide what these variables should be set to. Should white be set to 1 and grey to 2 for example.

If you want Cat character to have a colour name (eg white) rather than a number you need to set it in the way @mstone326 said.

How many characters are going to be set in one run of the project? Can you have two with the same colour?

Last edited by deck26 (Aug. 16, 2018 17:57:48)

mstone326
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

If you look at your dialog scripts you have a series of if statements, if cat character = gray, broadcast gray face. if cat character = white, broadcast white face.

Problem is all of those statements are true. Because cat character and gray are both 0. So that is true, same with white.

In your current setup, when you click the cat you want have it set cat character to the word as I said above. Then in your if statements construct them using that information.

if <(cat character) = [gray]> then

broadcast [gray face v]
else
if <(cat character) = [white]> then
broadcast [white face v]
else
end
Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

@deck26 @mstone326

I was planning on having a total of 9 colors (all labeled differently, of course). What should I do to set the variable so that it can later recognize it?

I have each of the 5 current sprites set up like this (all of course, having different values, not all grey):
when this sprite clicked
set [character v] to [grey]

It seems this doesn't actually set the variables?

Do I need to type it out here as well?
if <(character) = [grey]> then 
broadcast [grey face v]


else
if <(character) = [white]> then
broadcast [white face v]
end
end
Would setting a list work in its place?

Super sorry if there's an obvious answer and I'm just not getting it! I'm not the best at this kind of stuff (as you may have guessed).

Last edited by Asterlie (Aug. 16, 2018 17:47:58)

deck26
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

I'm still a bit confused about how this is supposed to work.

You ask for a number 1 to 9. This is detected by the various ‘when key pressed’ hat blocks. The prompt then vanishes but there is nothing preventing the user pressing another number.

Are you then planning on prompting again for another number? If so using hat blocks which will hide anything you've already shown - eg pressing 2 will hide cat 1 - so that's not a good method.

Without a better feel for what you're aiming for I'm struggling to advise. The method you should use will depend on what you're aiming for.
Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

deck26 wrote:

I'm still a bit confused about how this is supposed to work.

You ask for a number 1 to 9. This is detected by the various ‘when key pressed’ hat blocks. The prompt then vanishes but there is nothing preventing the user pressing another number.

Are you then planning on prompting again for another number? If so using hat blocks which will hide anything you've already shown - eg pressing 2 will hide cat 1 - so that's not a good method.

Without a better feel for what you're aiming for I'm struggling to advise. The method you should use will depend on what you're aiming for.

I plan to only have those numbers used once, they are just there for choosing a color. I honestly don't know how to make it where pressing those numbers after don't bring up other characters (I haven't gotten around to it, either). Sorry for being unclear
mstone326
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

I am with @Deck26 on this. I am also confused on what the end result should be. Let me see if I can sum this up and let us know if this is correct?

Allow the user to choose their cat, the sprites that are large.
After the user has chosen their cat, the selection period is over for the duration of the game
then the appropriate small cat will show that you can control
Then, when the dialog screen starts the cat you chose at the beginning should be the cat that appears in the dialog

Does that about sum it up?
Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

Yes, that is what I'm trying to do.
mstone326
Scratcher
1000+ posts

Using Variables+If/Else Block with More than 1 If

First step is to get set the time that the user can select the cat, so when the project starts and the cat has been selected the number keys should be disabled.

ALso, look at how I streamlined to use the any key instead of every key.

I stripped out all the extra stuff. So I only did gray and white cat for now. Let's get them working properly then you can follow that format for the extra cats you add. I would do this differently but I didn't want to rewrite what you did. I wanted to stay in your current format.

Edit: added a few other things and a way to eliminate all those if then else blocks. It is on a comment note on the project page and inside on the gray cat.

https://scratch.mit.edu/projects/239294975

Last edited by mstone326 (Aug. 16, 2018 23:32:17)

Asterlie
Scratcher
10 posts

Using Variables+If/Else Block with More than 1 If

@mstone326

Thank you VERY much for doing this! I will be sure to use those bits with the other characters as well. I also wanted to apologize if I seemed difficult with this whole thing, I am unfamiliar with some of these things, though I am willing to learn. Sorry for replying a bit late, had some errands to run.

Powered by DjangoBB