The variable self


#1

I don't necessarily know what the variable self does can someone help me?


#2

There's a topic for that already


#3

I am a bit confused too, well I don't even know what a variable is.


#4

The variable self basically means the object's... self. XD

When you want an object to do something when it has already done something, Self is what you need.

Say you want an object to grow when it touches something else. You would use the code "When (Self) touches (whatever you want it to touch before it grows) Grow by (percent)"

If you need more help, tag me :D


#5

I can help explain it, if you want :D


#6

Have you ever thought of being a teacher? You have good teaching skills...


#7

Yeah, that's what I want to be when I grow up. Thank you! :D


#8

Welcome. Seriously, dude, do it!!!!


#9

Check @bluedogmc-official's topic.


#10

#11

Please do~
I've figured out how to use the "wait" command and the "jump" thing but that's about it :sweat:


#12

I can tell you how to use it.


#13

Sure anyone can I'd love a lot of help ~ :heart:
Don't take that the wrong way, hehe


#14

Ok. What do you want explained? I'm in a car driving through Dallas at the moment, but I have internet.


#15

Variables are exactly what they sound like: they're "numbers" that are variable. Meaning, they can change, or "vary." But you control what a variable changes to. This is useful for randomizers and other things.


Example! Read this!

For example, say you want something to be randomly either blue or red every time you tap a button.

First, you take your object, let's say a circle. You want it to be either blue or red when you tap a button, so you want the color to be different and change, or vary. Hey, you need a variable!

You can call variables whatever you want, let's call this one "Random." Remember that!

So, this object should vary in color at the same time as the value of the variable varies. (That's a lot of "V"s!)

We want to make the color vary when the variable varies, so we'll put "When Random=1." (you can put any number here, but 1 is easy to remember.)

This means that something will happen when the value of our variable "Random" changes (varies!) to 1.

Doesn't that sound like what we wrote earlier? All it's missing is the part where it does something (in our case, changes color)!

So, we'll code it to change color to red because we already know when it will do that. We'll simply put "Set Color" and select red or use an HSB red.

There we go! Now it will change color to red when Random equals 1.

But we want it to do that with blue, too, right? So we'll go and add a new rule. Now we put "When Random="... but we need a different number. I'll go with 2. So,"When Random=2."

Then we'll put down "Set Color" and select your blue.

But wait... we need our button, because the variable isn't equal to 1 or 2, meaning the code won't happen!

Here's where the "random" bit comes in. You're going to get another object, let's say a circle, for your button.

Now you want to do "When Self is Tapped" because you want the first circle to be red or blue when the button is tapped.

Then you'll have it make our variable vary, meaning the color of the first circle will also vary thanks to our previous code. You can do this with the code "Set Variable "Random" random 1-2", which will make your variable randomly change, meaning- the color will change!


This is just one example of the use of variables in code. If you know the concept "when variable equals (number), (something) will happen" you can do lots of new code! :D


#16

@treefrogstudios Yes, you're right of course :smile: ...but it's more than that. The question not addressed is why do we need a (self) reference when you could just reference the object directly?

You don't need to use a (self) reference anytime "you want an object to grow when it touches something else". You could, but that's not the point. There are special cases where you have to use a (self) reference...

1) If you're using clones and want only the individual clone responding to the event to follow the coded action

2) Again with clones, if you wanted to refer to a property of the responding object. For example, if you wanted to use Set Size instead of Grow By these two blocks are equivalent

3) You want to make/use a custom block for multiple objects (that are not clones) that needs to refer to the object's properties (like if you wanted to include the Set Size above)


#17

Yeah, I know. I was sort of giving a basic explanation :P