How variables work(The legend of Zelda for NES as example)

in #programming3 years ago

For every interactions with our PCs and Phones we change variables, from variables that say what wether a button was pressed(boolian or bool) to text variables(string or str) these varaibles are essential for our devices to have any use, so this post will explain how they work.

Varaibles in The Legend of Zelda

The first Zelda is one of the cleanest programmed games ever, with programms like Bizhawk you can easily find every number for every action in the game.

So how do the variables in Zelda work? It's actually pretty simple, lets take the sword for example. As you probably know, the sword in the first zelda game can level up. So the developers used a very easy trick to save game space. If the variable is set to zero(like it is when you first start up the game) you have no sword. If it's at 1 you have the level 1 one, if it's on 2 the level 2 one and so on. So a level system in agame s one way to use variables. 

The rubees in the game make it even easier though, as the state of the varaible(0-255 or a byte variable) is exactly the ammount of rubees you own. So if the variable is set to 57 you have 57 rubees.

How to implement variables yourself

There are many different types of variables, so I will just tell you the most commonly used and most effective ones(the words in the brackets tell you how the variables is written in code).

  1. Boolian(bool): a boolian can only story the values 1 and 0 or how most languages call it true and false.
  2. Byte: as allready mentioned a byte can story the numbers one up until 256
  3. integer(int): stores values from  –2147483648 to 2147483647 
  4. char: stores one character meaning : a-z,  A-Z, 0-9, !, §, $, ...
  5. string(str): stores text for example : "Hello World"

So therse are the basic variables in the coding world. Don't worry you don't have to learn the lenght of an integer. Just don't use it all the time as they can take up a lot of space and therefor make your file/programm bigger.

What to use them for

Anything. You can use them for any type of usecase as they are univeral and you just have to ask what state a variable has and it will perform the action you assign to this state. So theoretically you could also say that if a string has the word "banana" in it, it should shut down the programm or some senceless stuf like that.

Upper Imgage Source