Variables how do they work

Noob question. I’ve been having a lot of fun making gamemodes but i having wrapped my noggin around variables. Like variable a or b. I’ve gotten the hang of a large portion of things but i don’t understand how variables work. If its not too complicated to explain could someone explain a bit of it to me?

Variables can store any data (except for strings) for later use. Global Variables are variables that belong to the game itself, while player variables belong to the specified players.

For example, you could keep the position of a shop in a global variable, so the shop would be in the same position for every player in the match no matter what.

But you could store the stats of a player in several player variables, like how much “money” they have to spend on upgrades in the shop, and what stats they have after buying upgrades.

3 Likes

I see someone’s already posted an explanation but more info is always helpful so I’ll post what I was already working on.

Variables are a way of storing values in a named container so that you can view/modify those stored values later. Typically they have a name and a value associated with them. In the Overwatch Workshop you can pick any letter from ‘a-z’ as a name for your variable.

As for the value, In the workshop there’s a lot of different types of stuff you can put in variables; numbers (real numbers from -9999 to positive 9999 -100000 to positive 100000), position points on the map in x-y-z coordinates (x is left/right position, y is the height for the player, and z could be considered forward/backward position), “Strings” (what programmers call words or text), heroes (values for ana, tracer, winston, genji…), player names (references to players who have joined the game), and arrays (ordered lists of things that can include any of the previous types mentioned).

In addition, in Overwatch there are two types of variables; global variables (a-z variables are not associated with any particular player), and player variables (separate a-z variables that are associated with each individual player).

Let me know if you have any questions because I know that I’ve thrown a lot at you.

  • Edit: You can’t put strings in variables at this time. And corrected the actual number range you can store in variables
2 Likes

Correction, as of today you can’t put Strings in variables.

1 Like

I don’t believe this is true, I have gotten variables to at least several million before… not for any practical reason though.

Ah my mistake. I didn’t have access to a computer with Overwatch when I wrote that and I thought I remembered being limited to that value when I tried entering number constants for an action down. Looks like numbers for actions are actually limited from 100 000 to -100 000. (When I add 1 to a global variable set to 100 000 it rolls over to -100 000)

2 Likes

A simple way to understand variables (as my programming teacher once explained it), is to pretend that each variable is a box.

You can put something in a box if you need it later. For example, if you want to remember a number, a hero, a position, or anything else.

Each box can only contain one thing. So if you put Hanzo in box A, trying to put the number 45 in box A will first remove Hanzo from it. (Arrays can store multiple values, but that’s slightly more advanced and harder to work with in the workshop).

So what you do is put something in the box, then the content of the box is saved, and you can at any time later take a look in the box and retrieve what’s inside it.

As a practical example, imagine you’re counting how many times a player has jumped. Every time a player jumps, you add +1 to a player variable. Then you’ll be able to for example check the variable:
“if the player variable is larger than 10…”
and then perform actions:
“kill the player and set the player variable back to 0”.

Thank you guys you really helped

2 Likes