AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Constant in if statements? (https://forums.alliedmods.net/showthread.php?t=163862)

Erox902 08-04-2011 09:46

Constant in if statements?
 
Let's say I have a constant like this:

PHP Code:

new const NUMBERS[][] =
{
     
10,
     
15,
     
30


How can I use them in a "if statement" like this

PHP Code:

if( gVariable == "any of the numbers inside const NUMBERS" 

?
Or do I have to do
PHP Code:

if( gVariable == 10 || 15 || 30 

Or
PHP Code:

if( gVariable == NUMBERS[0] || NUMBERS[1] || NUMBERS[2

?

jim_yang 08-04-2011 09:50

Re: Constant in if statements?
 
loop if array size is big

Bugsy 08-04-2011 11:08

Re: Constant in if statements?
 
Could use bit field, not sure what exactly youre doing but its just an idea. You are limited to using a min of 0 and max of 31 but you can extend it with a little more code. Let me know if you have any questions.
PHP Code:

const NUMBERS << 10 << 15 |<< 30;

var = 
<< << 15;
//true if at least one exists
if ( var & NUMBERS )
     
//true since 1<<15 exists

var = << 15;
// true if only 1 << 15 exists, you can | additional flags
if ( ( var & NUMBERS) == << 15 )
      
//true since only 1<<15 exists
 
var = << 10 << 15 << 20;
//true if all exist
if ( ( var & NUMBERS ) == NUMBERS )
    
//true since all exist 


Erox902 08-04-2011 12:52

Re: Constant in if statements?
 
Quote:

Originally Posted by Bugsy (Post 1525588)
Could use bit field, not sure what exactly youre doing but its just an idea. You are limited to using a min of 0 and max of 31 but you can extend it with a little more code. Let me know if you have any questions.

Acually bit wise seems kind of hard. What I'd like to do is just check if the variable is any of the numbers inside the constant. And if it returns true send a fuction.

Bugsy 08-04-2011 13:02

Re: Constant in if statements?
 
The above way is a good way to go about it. What range of numbers are you working with?

Erox902 08-04-2011 13:22

Re: Constant in if statements?
 
Well acually this was just an example, I was mostly curious if it was possible to do this...
So yeah basically any range of numbers.

fysiks 08-04-2011 15:04

Re: Constant in if statements?
 
Then use a loop checking each value of the array.

PHP Code:

stock bool:num_is_in_array(iNum)
{
    for( new 
0sizeof(array); i++ )
    {
        if( 
iNum == array[i] )
        {
            return 
true
        
}
    }
    return 
false



Hunter-Digital 08-04-2011 15:12

Re: Constant in if statements?
 
Hmm, wouldn't a string character search function work in this case too ? I dunno but I can't find it, I know there's one that searches for a single character inside a string... and you'll just use the constant array as the string and the character as your variable number (must be converted to char number tough, number 1 is char 38, something like that)

fysiks 08-04-2011 15:25

Re: Constant in if statements?
 
Quote:

Originally Posted by Hunter-Digital (Post 1525750)
Hmm, wouldn't a string character search function work in this case too ? I dunno but I can't find it, I know there's one that searches for a single character inside a string... and you'll just use the constant array as the string and the character as your variable number (must be converted to char number tough, number 1 is char 38, something like that)

Unless it's a native their's no reason to consider it. :)

Bugsy 08-04-2011 16:07

Re: Constant in if statements?
 
Explain specifically what you are doing so we can provide an exact solution.


All times are GMT -4. The time now is 03:19.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.