AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Variables (https://forums.alliedmods.net/showthread.php?t=184852)

Napoleon_be 05-09-2012 12:53

Variables
 
How to check using an easy way to check which of the 4 variables has the highest value?

<VeCo> 05-09-2012 12:59

Re: Variables
 
PHP Code:

new array[4]
 
new 
current_valuemax_value
for(current_value=0;current_value<4;current_value++) if(array[current_value] > array[max_value]) max_value current_value 


Napoleon_be 05-09-2012 13:13

Re: Variables
 
Quote:

Originally Posted by <VeCo> (Post 1705679)
PHP Code:

new array[4]
 
new 
current_valuemax_value
for(current_value=0;current_value<4;current_value++) if(array[current_value] > array[max_value]) max_value current_value 


can you do that with this code and explain me what it means, cuz i don't get it actually.
PHP Code:

public game_handler(idmenuitem) {
    if(
item == MENU_EXIT) {
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED
    
}
    
    if(
is_user_alive(id)) {
        switch(
item) {
            case 
0szNoScope++
            case 
1szNormal++
            case 
2szGravity++
            case 
3szVip++
        }
    }
}

public 
Cmd_EndVote(id) {
    if(
szNoScope 


<VeCo> 05-09-2012 13:32

Re: Variables
 
Make an array, if you want so much these "NoScope","Normal",etc. you can make an enum.

Like:

PHP Code:

enum
{
VOTE_NOSCOPE 0,
VOTE_NORMAL,
VOTE_GRAVITY,
VOTE_VIP,
MAX_VOTES
}
 
new 
vote_options[MAX_VOTES

And then you can use:

PHP Code:

vote_options[VOTE_NOSCOPE]++ 

Or better in your case you can remove this switch and use only this:

PHP Code:

vote_options[item]++ 


The loop will be:

PHP Code:

new imax_value
for(i=0;i<4;i++) if(vote_options[i] > vote_options[max_value]) max_value 

It starts the loop at slot 0 (variable i) of the array and then checks if it's value is bigger than that's from the slot number max_value. If it's bigger, then max_value is set to i and the check continues with i = slot 1. If the value of slot 1 is bigger than the value of max_value, which is now slot 0, then set max_value to the value slot 1. Then the loop goes to slot 2 - if the value of slot 2 is bigger than that of slot 1, set max_value to the value of slot 2. At the next step i is the value of slot 3 - if the value if slot 3 is bigger than that of slot 2, set max_value to 3... and so on...

So at the end you get the highest value of the options in the array, in the max_value variable and then you can use it like this:

PHP Code:

vote_options[max_value

-> max_value is the slot of the array with the highest value.

DarkGL 05-09-2012 14:01

Re: Variables
 
Code:

new array[4]
 
new current_value, max_value
for(current_value=0;current_value<4;current_value++) if(array[current_value] > array[max_value]) max_value = current_value

or
Code:

max( array[ 0 ] , max( array[ 1 ] , max( array[ 2 ] , array[ 3 ] ) ) );

fysiks 05-09-2012 15:08

Re: Variables
 
Quote:

Originally Posted by DarkGL (Post 1705736)
Code:

max( array[ 0 ] , max( array[ 1 ] , max( array[ 2 ] , array[ 3 ] ) ) );

That will only return the largest value which is not what is needed. You need to know which one has the largest value.

Tirant 05-09-2012 19:40

Re: Variables
 
If you didn't want to create an array you could try something like

new max = max(szNoScope, max(szNormal, max(szGravity, szVip)));
if (szNoScope == max) {
} else if (szNormal == max) {
} else if (szGravity == max) {
} else { // implied that szVip is max
}

Which I think would be the simplest for you to implement. The only problem with this method (and the array method) is that this will only tell you the first one that equals max, so to get more then one equaling max, just make them each their own if-statement.


As a side note, don't use hungarian notation in a type-less language if you don't understand how it works. It's really annoying when it's super important to understand your code.
http://en.wikipedia.org/wiki/Hungarian_notation

Napoleon_be 05-10-2012 11:04

Re: Variables
 
PHP Code:

public Cmd_EndVote(id) {
    new 
maxvote max(szNoScopemax(szNormalmax(szGravityszVip)))
    
    if (
szNoScope == maxvote) {
        
Cmd_NoScope(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    } else if (
szNormal == maxvote) {
        
Cmd_Normal(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    } else if (
szGravity == maxvote) {
        
Cmd_HigherGravity(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    } else { 
// implied that szVip is max
        
Cmd_KillVip(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    }


It's printing the message, but it ain't starting the game... Should i loop trough something?

fysiks 05-10-2012 11:37

Re: Variables
 
Quote:

Originally Posted by Napoleon_be (Post 1706196)
PHP Code:

public Cmd_EndVote(id) {
    new 
maxvote max(szNoScopemax(szNormalmax(szGravityszVip)))
    
    if (
szNoScope == maxvote) {
        
Cmd_NoScope(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    } else if (
szNormal == maxvote) {
        
Cmd_Normal(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    } else if (
szGravity == maxvote) {
        
Cmd_HigherGravity(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    } else { 
// implied that szVip is max
        
Cmd_KillVip(id)
        
ColorChat(idGREEN"[%s]^1 The vote has ended!"szPrefix)
    }


It's printing the message, but it ain't starting the game... Should i loop trough something?

Starting the game? If the message is working then this code is working. If something else is not working then you need to look at the code that actually isn't working.

Napoleon_be 05-10-2012 12:16

Re: Variables
 
Normally it's working, but now it isnt. If it chooses Cmd_KillVip(id), it works, but with the vote it ain't working...


All times are GMT -4. The time now is 00:28.

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