Terraria Community Forums

Xylia
Xylia
Now, I know enough about programming to know that two bytes (FF) is equal to 255, three bytes (FFF) is equal to 4095 and four bytes (FFFF) is equal to 65535, but I could not for the life of me figure out why some games had hard caps of 511. Obviously, 511 is 255+256. This never made sense to me... (cont)
Xylia
Xylia
.... because why use 4 bytes to store something you could store in 3 bytes instead and get a much larger number? Then it hit me... I bet they are using a two-byte variable + a boolean (which only needs 1 bit to store). (cont)
Xylia
Xylia
So... basically what you got is the byte that can go from 0 to 255 plus a boolean (true/false). "Is the number >255? If so, add 255 to the value of the variable and use that number". This allows you to double the capacity of a two-byte variable.
Daikonradish
Daikonradish
Another thing worth considering that 255's a max, how did some NES/Famicom racers have the player vehicles go above 255 kilos an hour?
Xylia
Xylia
Because NES games can actually assign variables higher than 255. Crystalis capped XP at 65535, for example. The reason that many stats and variables ARE capped at 255 is to save memory. If they didn't think you needed more than 255, then they designed the game around a 255 cap in that particular variable. But, something like XP, you're obviously gonna want more than 255.
Xylia
Xylia
Also, Crystalis has a level cap of 16. This means they only needed 1 byte for your Level Number (1-16). Rather than allocating two bytes that would allow for Lv255, they said "nah, we don't need to go that high, let's just stop at 16 so we only need 1 byte for that".
Xylia
Xylia
Another small tidbit that a lot of people miss, is that you need to allocate this stuff when you are writing code. You need to tell the computer to set aside a location in memory to be used for that, and part of this includes how much data to set aside. At least, in earlier machines. I think modern computers do this stuff automatically.
Back
Top Bottom