At the risk of sounding like a dumbass, what exactly could this library be used for,
@duck ?
Click to expand...
Flags allow you to store pieces of information inside a single number. Using them, you can provide many different attributes to things. For example, Clockwork commands. What if I want a command that can't be used during certain situations, such as if they're knocked out, in a vehicle, or dead? What if I want it so that they can't use it if in a vehicle, but can use it while knocked out or dead? What if I want it so you can use it while knocked out, but not dead? It's not so simple to do this with tables. Sure, you can have a command that creates fields called COMMAND.canUseIfDead, COMMAND.canUseIfInVehicle, etc, but that gets complicated and messy. With flags, you can compact it all to a single line.
First, you must define your flags. These numbers are useless to you, and you'll never have to deal with them again after defining them. I recommend you start from 1 and go up from there.
Code:
Clockwork.bitFlags:Define("Command", {
CMD_KNOCKEDOUT = 1,
CMD_FALLENOVER = 2,
CMD_VEHICLE = 3,
CMD_DEAD = 4
});
After that, you're all set to use flags.
Code:
COMMAND.flags = Clockwork.bitFlags:Combine(CMD_DEAD, CMD_VEHICLE)
If you want a command that you can only use while dead or in a vehicle, using the PlayerCanUseCommand hook, you check if they're dead and see if the command has the CMD_DEAD flag.
Code:
local canUse = Clockwork.bitFlags:Has(commandTable.flags, CMD_DEAD)
If they're in a vehicle, you check if the command has the CMD_VEHICLE flag.
Code:
local canUse = Clockwork.bitFlags:Has(commandTable.flags, CMD_VEHICLE)
This library uses bit flags differently. Normally bit flags are like this:
2
4
8
16
32
64
etc...
but I've simplified it so that you can use them much easier, by just defining a table with consecutive numbers. The command library actually already uses bit flags, but in the way I just showed you right now where the numbers double (they're actually powers of 2). You can see that here and here .
Bit flags basically allow you to combine numbers into one unique number that you can apply and extract many different attributes to and from.