Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » When/How do you use variables?
- Warriorcatseriesfan
-
Scratcher
16 posts
When/How do you use variables?
I'm planning to make a warriors game. When I play the other warriors game that other people make, I usually see variables made. Also, I really don't know when to use them. I'm kinda confused with variables. Can you help me?
- deck26
-
Scratcher
1000+ posts
When/How do you use variables?
If you have a game with multiple levels you might use a level variable to keep track of the the current level so blocks like this could work
If the player scores points you need a variable to keep track of that. Any value that might change and isn't covered by built-in variables like costume#, x-position, size will need to be stored by you and you'll need a variable or list to store the values. Otherwise how can your scripts refer to the values and do different things as the project progresses? That can work if everything always happens the same and the order is fixed but most projects are not like that.
when I receive [new level v]
change [level v] by [1]
if <(level) = [3]> then
some code here for level 3
else
if <(level) = [4]> then
some code here for level 4
end
end
If the player scores points you need a variable to keep track of that. Any value that might change and isn't covered by built-in variables like costume#, x-position, size will need to be stored by you and you'll need a variable or list to store the values. Otherwise how can your scripts refer to the values and do different things as the project progresses? That can work if everything always happens the same and the order is fixed but most projects are not like that.
- sonicace
-
Scratcher
29 posts
When/How do you use variables?
Variables are a way to store data that is then usually picked up by another script.
More advanced people will use list storage as it's more efficient but when you are starting out you should probably use variables.
There ate multiple uses for variables, including (but not limited to):
More advanced people will use list storage as it's more efficient but when you are starting out you should probably use variables.
There ate multiple uses for variables, including (but not limited to):
- Storing a number (useful if you are needing to use math)
Holding numbers as it's easy to check if something is true or false using variables
Holding a string of text or numbers that will be used multiple times, as a variable can replace a long string of words/numbers
Moving data across sprites. (such as X, Y and DIR)
Making multiplayer games (cloud var)
Last edited by sonicace (Sept. 29, 2020 12:35:54)
- SparshG
-
Scratcher
500+ posts
When/How do you use variables?
Variables are simply the things that can vary…
1. You want to give health to a player?
2. You want to make a ball fall?
Lets speed up ball while falling to simulate gravity
Let me do the same with a variable
I got a tutorial project on variables here if you want to see.. https://scratch.mit.edu/projects/150991939/
1. You want to give health to a player?
when green flag clicked
set [health v] to [10]
when I receive [lower health v]
change [health v] by (-1)
2. You want to make a ball fall?
glide (1) secs to x: (0) y: (-100) //not realistic at all
Lets speed up ball while falling to simulate gravity
change y by (-1)
change y by (-2)
change y by (-3)
change y by (-4)
change y by (-5) // ok its really inefficient way...
Let me do the same with a variable
set [speed v] to [0]
repeat (20)
change y by (speed)
change [speed v] by (-1)
end
I got a tutorial project on variables here if you want to see.. https://scratch.mit.edu/projects/150991939/
- Warriorcatseriesfan
-
Scratcher
16 posts
When/How do you use variables?
OMG! Tysm! So you use it like that? Sorry, but pls one more favor. How do you change the mode of your age? Or maybe how do you make someone talk, but when you come back, he/she says to do some kind of task?
- Discussion Forums
- » Help with Scripts
-
» When/How do you use variables?