How to use variables

howto
variables
donotlounge

#1

Hello! This is about the yellow blocks:
image
They change variables, usually things that are meant to change by increasing or decreasing or being set. They can be handy, like being used in art pads, tappers or games where you have increasing/decreasing/changing values.


Set Block

You know the “set” block? That’s used to set a variable. To use it, select it and add it to your code. For this instance, we’ll put it under “When Game Starts”.


You might be thinking, “what now?”. Well,let’s create a variable under the IPad text. Just name it Variable.

Now you have created the variable. Set it to any value. I’m leaving it at the default, ten.
But wait. What happens now? Well, let’s make the variable cause something to happen. Let’s use the blue Check Once If blocks. Set it so when Variable is equal to ten, it changes the text to “Congrats!” It should how look like this:

Press play now. It should say “Hooray”, or whatever you set it to.

Hope this helps. Now, you should be able to make something using a set block if you didn’t know what it did. That’s ok- we all learn something new!


Increase Block

Don’t know how to use the increase block? Let’s continue off the previous tutorial. With the code from the “hooray text”, change the Check Once If Block to when Variable is equal to 15. Then, add the code for when self is tapped. Add the increase block. Be sure to put in the variable!


Change it so it increases by 5 when tapped.

Now make sure before you play, have the text say Tap Me.

If you play now, it wo t change because we need a repeat forever block!
It should look like this:

Now if you play, it should change when you tap it.
Now that you know how to use the increase block, you can make many can even make an increase block increase by a negative number, so you can have games where you can have increasing/decreasing scores! Hope this tutorial helped you!


Other uses of a variable block

There are many uses of a variable block. For example, in an art Pad colors are definitely several times where there needs to be a changing variable. For example, doing a hsb or rgb art pad requires four variables, maybe three without a variable for width. Colors and sizes will constantly change. It’s easy to insert the variable, but you will need to copy and paste a variable for text that is constantly set to the width/size of the pen.
Tapper games also require at least one variable, some will use both variable blocks to set a starting value, and set it so when tapped, the value increases.
There’s no doubt many games require a variable. And now you know how they are used, you can try too! Thanks for reading this quick how-to.


For People With Regular

If you have a better category or custom tag for this, please change it. Also please don’t lounge This!

People who could give extra tips

@Petrichor
@CreationsOfaNoob
@CoderOfMagic
@CodeHelp
@pomtl
@Hopscotcher
I will add more as I remember


#2

If you were tagged, that means I think you can help with code.
I tagged pomtl because they do project stuff and this is a simple how-to with variables


#3

You can also use a variable to set a text to a number, basically you can turn a text into a timer or a score by setting the text’s text to always be that variable


#4

That is true, but this is a quick tutorial. I will add more later when I have time.


#5

That is basic


#6

I’m but I ran out of time when I have more time i’ll Add it.


#7

Some HSers are probably from the forum, then they discover Hopscotch. But that’s unlikely.


#8

Just something to note is that tapping the object will not change the text. This is because the condition is evaluated before you increase the variable.


#10

Hey @CoderOfMagic
Please invite people that can help with more info


#11

I fixed that. I forgot I need a repeat forever block


#12

To decrease a variable, put the negative sign!
Decrease “Hooray” by -1
Means to decrease your variable(hooray) by 1


#13

You could simply use a “When object is tapped” to increase your value by 5 each time you tap it.

Then you implement a “when =” block so it changes the text.

If you don’t want the variable to exceed 15 insert an “if <” or “>” statement inside then “when object is tapped” thingy and then put the increase block inside the if statement.


#14

Actually the variable is variable


#15

That is if you want to play with variables a bit more, you can do that.


#16

Hey, I was tagged in this. I don’t think I could add to anything the others have said. Sorry but keep up the good work.

UnicornRainbow xxx


#17

Awesome tutorial, well done! :clap: