How do I store several tags in a variable?

From OHRRPGCE-Wiki
Jump to navigation Jump to search
Revise.png
This article does not meet the standard of quality we would like to have. Feel free to revise it and make it better by using the edit link at the top of the page. This tag should be removed when an editor feels it is of high quality.

The reason this article has been marked is: script globals are 32 bit now, not 16

Your game has a total of 998 tags that it can use to store simple true/false values. For most people, this should be plenty. If your game is ridiculously complex, though, there's a slim chance you may run out of tags. Alternatively, you might want to do something with tags that can only be done with variables--for instance, you might want to be able to read their values from a saved game without loading it. In any case, it's possible to store several tags in the form of a single variable.

If you're not aware, a bit is the smallest possible amount of information that a computer can store. The famous zeros and ones that computers use for everything are bits. One and zero can also be thought of as on and off, true and false, yes and no, good and evil, Honoka and Nagisa, day and night, fuschia and not fuschia, or any other type of situation where there are exactly two possible values you want to keep track of. If that sounds familiar, it's because that's exactly how tags work. A tag is nothing more than a bit that the OHRRPGCE keeps track of for you.

Since variables are just information on your computer, they're made out of bits, too. Specifically, most variables in OHRRPGCE are 16-bit signed integers, so they're each made out of 16 bits. So if you have a way to mess with those bits one at a time, you can use a variable as 16 tags.

# return whether one particular bit is true or false
script, get variable bit, value=1, which=1, begin
  variable (bit)
  # the 16th bit is the minus sign, so it has to be treated differently
  if (which == 16) then, begin
    bit := -32768
    # the computer thinks of -32768 as 1000000000000000b
  end
  else, begin
    # this turns the numbers 1, 2, 3, 4... into the numbers 1, 2, 4, 8...
    # why these numbers?  because if you convert them to the bits the computer
    # uses to represent them, you get 0000000000000001b, 0000000000000010b,
    # 0000000000000100b, 0000000000001000b...
    # in other words, it gives us a number where all the bits are turned off
    # except the one we're interested in, which is true
    bit := 2 ^ (which -- 1)
  end
  
  # return true if there are any bits that are turned on in both value and bit
  # since bit has only one bit turned on, this is the same as returning true
  # if that bit is turned on
  return ((value ,and, bit) <> 0)
end

# change the value to have the bit in the requested state, and return the result
script, set variable bit, value=1, which=1, state=true, begin
  variable (bit)
  # the 16th bit is the minus sign, so it has to be treated differently
  if (which == 16) then, begin
    bit := -32768
    # the computer thinks of -32768 as 1000000000000000b
  end
  else, begin
    # this turns the numbers 1, 2, 3, 4... into the numbers 1, 2, 4, 8...
    # why these numbers?  because if you convert them to the bits the computer
    # uses to represent them, you get 0000000000000001b, 0000000000000010b,
    # 0000000000000100b, 0000000000001000b...
    # in other words, it gives us a number where all the bits are turned off
    # except the one we're interested in, which is true
    bit := 2 ^ (which -- 1)
  end
  
  if (state) then, begin
    # if we're trying to turn the bit on, then start a new number.  go through it
    # one bit at a time, and if either value or bit has that bit turned on, turn it
    # on in the new number, too, and return that.
    return ((value ,or, bit))
  end
  else, begin
    # this is more complicated.  -1 looks like 1111111111111111b to the computer, so
    # (-1 -- bit) equals the opposite of bit: all the bits are on, except that the one
    # that's turned on in bit is turned off.  next, go through every bit, and if a bit
    # is turned on in BOTH value AND (-1 -- bit), then turn that bit on.  this will
    # turn on everything that's turned on in value, except that the bit that's turned
    # off in (-1 -- bit) and on in bit will be turned off.
    return ((value ,and, (-1 -- bit)))
  end
end

That's all really complicated, but you don't necessarily need to know how it works in order to use it, which is done like so:

variable (a)

a := 0
# 0000000000000000b

a := set variable bit (a, 5, true)
# 0000000000010000b

if (get variable bit (a, 5)) then, begin
  show text box (1)
  wait for text box
end
#true

a := -1
# 1111111111111111b

a := set variable bit (a, 5, false)
# 1111111111111111b

if (get variable bit (a, 5)) then, begin
  show text box (1)
  wait for text box
end
#false