Raised This Month: $ Target: $400
 0% 

Variables


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 05-09-2012 , 12:53   Variables
Reply With Quote #1

How to check using an easy way to check which of the 4 variables has the highest value?
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
<VeCo>
Veteran Member
Join Date: Jul 2009
Location: Bulgaria
Old 05-09-2012 , 12:59   Re: Variables
Reply With Quote #2

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 
__________________
<VeCo> is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 05-09-2012 , 13:13   Re: Variables
Reply With Quote #3

Quote:
Originally Posted by <VeCo> View Post
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 
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
<VeCo>
Veteran Member
Join Date: Jul 2009
Location: Bulgaria
Old 05-09-2012 , 13:32   Re: Variables
Reply With Quote #4

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.
__________________

Last edited by <VeCo>; 05-09-2012 at 13:40.
<VeCo> is offline
DarkGL
Senior Member
Join Date: Aug 2010
Location: Warsaw, Poland
Old 05-09-2012 , 14:01   Re: Variables
Reply With Quote #5

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 ] ) ) );
__________________
DarkGL is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-09-2012 , 15:08   Re: Variables
Reply With Quote #6

Quote:
Originally Posted by DarkGL View Post
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.
__________________
fysiks is offline
Tirant
Veteran Member
Join Date: Jul 2008
Location: Los Angeles, California
Old 05-09-2012 , 19:40   Re: Variables
Reply With Quote #7

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
__________________

PM me if you're interested in buying the Credits addition for Base Builder
Battlefield Rebirth [66% done]
Call of Duty: MW2 [100% done]
Base Builder [100% done]

Last edited by Tirant; 05-09-2012 at 19:45.
Tirant is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 05-10-2012 , 11:04   Re: Variables
Reply With Quote #8

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?
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-10-2012 , 11:37   Re: Variables
Reply With Quote #9

Quote:
Originally Posted by Napoleon_be View Post
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.
__________________
fysiks is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 05-10-2012 , 12:16   Re: Variables
Reply With Quote #10

Normally it's working, but now it isnt. If it chooses Cmd_KillVip(id), it works, but with the vote it ain't working...
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Reply



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 00:28.


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