Raised This Month: $ Target: $400
 0% 

Constant in if statements?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Erox902
Veteran Member
Join Date: Jun 2009
Location: Never Never Land
Old 08-04-2011 , 09:46   Constant in if statements?
Reply With Quote #1

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
?
Erox902 is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 08-04-2011 , 09:50   Re: Constant in if statements?
Reply With Quote #2

loop if array size is big
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-04-2011 , 11:08   Re: Constant in if statements?
Reply With Quote #3

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 
__________________

Last edited by Bugsy; 08-04-2011 at 11:39.
Bugsy is offline
Erox902
Veteran Member
Join Date: Jun 2009
Location: Never Never Land
Old 08-04-2011 , 12:52   Re: Constant in if statements?
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post
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.
Erox902 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-04-2011 , 13:02   Re: Constant in if statements?
Reply With Quote #5

The above way is a good way to go about it. What range of numbers are you working with?
__________________
Bugsy is offline
Erox902
Veteran Member
Join Date: Jun 2009
Location: Never Never Land
Old 08-04-2011 , 13:22   Re: Constant in if statements?
Reply With Quote #6

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.
Erox902 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-04-2011 , 15:04   Re: Constant in if statements?
Reply With Quote #7

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

__________________

Last edited by fysiks; 08-04-2011 at 17:11. Reason: Added bool tag to function.
fysiks is offline
Hunter-Digital
Veteran Member
Join Date: Aug 2006
Location: In the Game [ro]
Old 08-04-2011 , 15:12   Re: Constant in if statements?
Reply With Quote #8

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)
__________________
Hunter-Digital is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-04-2011 , 15:25   Re: Constant in if statements?
Reply With Quote #9

Quote:
Originally Posted by Hunter-Digital View Post
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.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-04-2011 , 16:07   Re: Constant in if statements?
Reply With Quote #10

Explain specifically what you are doing so we can provide an exact solution.
__________________
Bugsy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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