Discuss Scratch

MeowShadow
Scratcher
82 posts

RPG Game Help!

So, I'm working on this game. I think it might be like a complex RPG, but that's beside the point. I'm working on it, and I have this script:
when green flag clicked
set [Something v] to [1]
Okay, I also have more blocks up there (that I'm too lazy to put because one of them is a rather complex “More Block,” and it has lists and stuff) but I have determined those are not what's causing the problem. The problem starts with this script:
when [space v] key pressed
set [Something v] to ((Something) + (1))
Then it sets “Something” to 36 instead of 2! I tried doing
set [Something v] to (((Something) + (1)) - (34))
Then it didn't get the proper answer (2) but instead, -1154. WTHECK?? How do you go from 1+1 - 34 to -1154?? I even tried with another variable, but it still has the same thing.
I've figured that the operator block is causing all this sticky mess. I've even used the
change [Something v] by (1)
But even that is 36!
Also, could someone tell me how to render stuff from a list? I want it on a text engine (which i already have) Is that possible? Or am I just asking for too much?

If you need more info, please just ask! This is really frustrating, so I'd like help as soon as I can have it, please. Have a nice day!
asivi
Scratcher
1000+ posts

RPG Game Help!

MeowShadow wrote:

So, I'm working on this game. I think it might be like a complex RPG, but that's beside the point. I'm working on it, and I have this script:
when green flag clicked
set [Something v] to [1]
Okay, I also have more blocks up there (that I'm too lazy to put because one of them is a rather complex “More Block,” and it has lists and stuff) but I have determined those are not what's causing the problem. The problem starts with this script:
when [space v] key pressed
set [Something v] to ((Something) + (1))
Then it sets “Something” to 36 instead of 2! I tried doing
set [Something v] to (((Something) + (1)) - (34))
Then it didn't get the proper answer (2) but instead, -1154. WTHECK?? How do you go from 1+1 - 34 to -1154?? I even tried with another variable, but it still has the same thing.
I've figured that the operator block is causing all this sticky mess. I've even used the
change [Something v] by (1)
But even that is 36!
Also, could someone tell me how to render stuff from a list? I want it on a text engine (which i already have) Is that possible? Or am I just asking for too much?

If you need more info, please just ask! This is really frustrating, so I'd like help as soon as I can have it, please. Have a nice day!
when [space v] key pressed
set [Something v] to ((Something) + (1))
wait until <not <key [space v] pressed?>>//check if this helps

MeowShadow
Scratcher
82 posts

RPG Game Help!

Nope, still not working. I think that operator block is cursed for me to never use it or something. I even switched it around to
when [space v] key pressed
wait until <not <key [space v] pressed?>>
set [Something v] to ((Something) + (1))
but still no cigar. Could it be a Scratch glitch for my computer or something?

Last edited by MeowShadow (March 28, 2017 05:12:08)

asivi
Scratcher
1000+ posts

RPG Game Help!

MeowShadow wrote:

Nope, still not working. I think that operator block is cursed for me to never use it or something. I even switched it around to
when [space v] key pressed
wait until <not <key [space v] pressed?>>
set [Something v] to ((Something) + (0))
but still no cigar. Could it be a Scratch glitch for my computer or something?
That means you have that script repeated somewhere else or others involving the space key interfering in some way.
MeowShadow
Scratcher
82 posts

RPG Game Help!

That is not repeated anywhere else. Nor is there anything else that responds to the space bar/any key. I checked and double checked. There's only four other sprites: one of them is a reference, one of them is the background, another is a character, and the last one is a banner. They are all programmed to move or show, not interact.
asivi
Scratcher
1000+ posts

RPG Game Help!

Check it again, and again and again…
asivi
Scratcher
1000+ posts

RPG Game Help!

…or let others check it for you.
MeowShadow
Scratcher
82 posts

RPG Game Help!

Do you think I should share it?
asivi
Scratcher
1000+ posts

RPG Game Help!

Do what you think is convenient.
MeowShadow
Scratcher
82 posts

RPG Game Help!

I don't mind. I just really need help. Here it is, if it helps: https://scratch.mit.edu/projects/151490691/
asivi
Scratcher
1000+ posts

RPG Game Help!

Do this:
create a new variable “clone count” set it to 0 at the start, put a block changing it by 1 under create clone of myself, so you will know how much clones has been created and take into account that every clone will respond to the space key pressed.

Last edited by asivi (March 28, 2017 06:10:58)

MeowShadow
Scratcher
82 posts

RPG Game Help!

Ah. There's 34 clones. I see it. I feel like an idiot for not remembering that from class xD
_nix
Scratcher
1000+ posts

RPG Game Help!

Yeah – clones will react to that “when space key pressed” block too, and so they'd all activate the “set something to something + 1” block.

I'm sure you can probably guess it yourself, but let's figure out the reason that “set something to something + 1 - 34” gets you -1154. First we'll show that in blocks:

set [something v] to (((something) + (1)) - (34))

Well, we can simplify this using the “change” block. It just increases a variable by the passed amount:

change [something v] by ((1) - (34))

If you've done negative numbers in math, you'll know that 1 minus 34 is -33. We can simplify our “change” block by replacing “1 - 34” with “-33”:

change [something v] by (-33)

And we already know that this block is being called once for every clone, so it's getting run more than once. Our variable is changed 35 times – once for the main sprite, and once for each clone.

We can simulate exactly what's happening using a loop and one sprite instead of a bunch of sprites:

when green flag clicked
set [something v] to (1) // The original value.
repeat (35) // The clone count, plus the original sprite.
change [something v] by (-33)
end
say (join [The value of something is: ] (something))

..And, sure enough, the value is -1154.

Fun fact – since we're repeating addition of -33, we can really represent that “repeat 35: add -33” as a multiplication: “35 * -33”; and then we can add that to our starting value of 1, which gets us “35 * -33 + 1”. If you put that into a calculator you'll get -1154 too!
_nix
Scratcher
1000+ posts

RPG Game Help!

Sorry for the double post but I felt a need to separate these two responses..

What you want is to change that variable just once – not 35 times! The easiest way to do that, then, is to make only the main sprite change the variable.

But how do we do that? Well, we'll need a way to tell whether an object (a sprite or a clone) is a clone. Then we can decide to run our action or not using an “if” block, like this:

when [space v] key pressed
if <not <is a clone? :: control>> then
change [variable v] by (1)
end

But there's no “is a clone?” block, so.. are we stuck?

Nope! That's because there's special variables that programmers call local variables. That's a fancy word for a variable that's stored in one sprite and each of its clones separately.

The easiest way to see that is something like this quick demo project I made. See how each clone has its own counter?

So each clone stores its variable separately from the original sprite (and other clones). Well, now all we need to do to see whether a clone is a variable or not is to use a local variable. In case you don't know how, just mark the variable as “for this sprite only” when you're creating it:



So we'll make a local variable called “is a clone?” and set it to “yes” right under in a “when I start as a clone” block. We'll also put a “set is a clone? to no” block under a “when flag clicked” block – only the main sprite runs “when flag clicked” blocks (since all clones are deleted by Scratch when we restart the project):

when green flag clicked // Only the main, original sprite will run this.
set [is a clone? v] to [no]

when I start as a clone // Of course, only clones will run this, right when they're created.
set [is a clone? v] to [yes]

And now we can use our “is a clone?” variable in place of the pretend “is a clone?” control block:

when [space v] key pressed
if <(is a clone?) = [no]> then
change [variable v] by (1)
end

..And it'll work!

By the way, you can also use the idea of local variables we made here for other actions, too – not just checking if the object is a clone! For example, you could make a way to reference clones separately using a “clone ID” system (for example, a broadcast that says “move clone 25 up” – well, only the clone with the local “clone ID” variable would move up).. but that's another topic

(edit: grammar, typos)

Last edited by _nix (March 28, 2017 12:00:00)

lazzerman004
Scratcher
27 posts

RPG Game Help!

when green flag clicked
forever
if <key [ v] pressed?> then
change [ something] by (1)
end
end

works wayyyyyy better than
when [ v] key pressed
trust me.

Last edited by lazzerman004 (March 28, 2017 12:51:33)

MeowShadow
Scratcher
82 posts

RPG Game Help!

Thanks so much @_nix! It really helped. You guys on the forums are very awesome!
MeowShadow
Scratcher
82 posts

RPG Game Help!

lazzerman004 wrote:

when green flag clicked
forever
if <key [ v] pressed?> then
change [ something] by (1)
end
end

works wayyyyyy better than
when [ v] key pressed
trust me.

Well, it MIGHT, except that's not what I'm looking for. I'm looking for the text to change when you press the space bar, and I'd rather have it in its own little script. Besides, if I did that, the numbers would be very big, as the “forever” loop is in there.

Powered by DjangoBB