20.03.2012, 18:02
Introduction
In this tutorial, I will show you how to use switch statement for strings.
Getting the necessary things
All you need to have is this library only. Credits to ****** for creating the useful library. Make sure to include the library on your script.
How will it work
Well, it hashes the string into numbers which can be easily compared afterwards. Sounds easy eh.
How to script it
It's as easy as comparing numeric values.
Let me show you an example code with guidance.
Very easy right. You can use this on several places even though you have a command processor and it's the fastest string comparisons.
If you have any question, please post it below.
In this tutorial, I will show you how to use switch statement for strings.
Getting the necessary things
All you need to have is this library only. Credits to ****** for creating the useful library. Make sure to include the library on your script.
How will it work
Well, it hashes the string into numbers which can be easily compared afterwards. Sounds easy eh.
How to script it
It's as easy as comparing numeric values.
pawn Code:
StrHash(
strz[], // String to hash.
bool:sensitive = true, // Case Sensitivity.
e_HASH_TYPE:type = bernstein // The type of hash to use as default to bernstein.
);
pawn Code:
switch(StrHash(cmd)) // Hashing the string into numberic value and switiching it
{
// _I - This is the macro to generate a compile time constant hash as stated on the y_stringhash official thread.
case _I<help>: // If command is help
{
// Code to execute on here
}
case _I<commands>: // If command is commands
{
// Code to execute on here
}
case _I<kill>: // If command is kill
{
// Code to execute on here
}
}
If you have any question, please post it below.