Raised This Month: $ Target: $400
 0% 

[HELP] Switch between const size numbers


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 05-26-2014 , 09:16   [HELP] Switch between const size numbers
Reply With Quote #1

Hey all! Now, I want to ask if it is possible to switch between the size numbers of a constant. I tried with a loop, switching and cases, doeasn't work. I want to know, because I register a clcmds with a constant. They are doing pretty simillar stuff and that's why I want to add all of the functions for these cmds in one. I will give an example - the cmds are 10. Five of them give you HP, the other five give you Armor. But we will get simplier - there are two cmds. The one is giving HP, the other is giving Armor. And I'm registering the with loop like this:
PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "X"
#define VERSION "1.0"
#define AUTHOR "Flicker"


new const Stuff[][]=
{
    
""
    "say /hp"
,
    
"say /armor"
}

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    for(new 
1sizeof(Stuff); i++)
        
register_clcmd(Stuff[i], "TheOneFunction")
}


public 
TheOneFunction(id)
{
    
//Here I want to switch between each num of the const size. Like
    //in case 1 it will set_user_healt, in case the num of the size is 2 - it will set_user_armor
    //Is that possible?

Tried with this:
PHP Code:
public TheOneFunction(id
{
    for(new 
1sizeof(Stuff); i++)
    {
        switch(
i)
        {
            case 
1:
            case 
2:
        }
    }

Didn't work. Thanks in advance.
__________________

Last edited by Flick3rR; 05-26-2014 at 09:20.
Flick3rR is offline
Send a message via Skype™ to Flick3rR
DavidJr
Senior Member
Join Date: Apr 2012
Old 05-26-2014 , 09:49   Re: [HELP] Switch between const size numbers
Reply With Quote #2

When the user type /hp or /armor, the will get those 10 cases in one time.

Why don't you try with this?

PHP Code:
new szCmd[33];
read_argv(1szCmdsizeof szCmd);

if (
equal(szCmd"/hp"))
else if (
equal(szCmd"/armor")) 
__________________
What are you looking for here?
DavidJr is offline
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 05-26-2014 , 10:12   Re: [HELP] Switch between const size numbers
Reply With Quote #3

Because these 'say' commands are just an example. The command are many and different, but they are using thow natives- set_user_healt and sat_user_armor. And that's why I want to know how to mak that switch - that's what I am interested of.
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
DavidJr
Senior Member
Join Date: Apr 2012
Old 05-26-2014 , 10:33   Re: [HELP] Switch between const size numbers
Reply With Quote #4

I understand, but why do you still need case for constant variable? You can do it without swtich - case.

This is the example:
PHP Code:
new const Stuff[][] =
{
    
"",
    
"say /hp",
    
"say /armor"
}

new const 
healthPotion[] =
{
    
0,
    
10,
    
20,
    
30
}

new const 
armorPotion[] =
{
    
0,
    
10,
    
20,
    
30
}

public 
plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    for(new 
1sizeof(Stuff); i++) register_clcmd(Stuff[i], "TheOneFunction");
}


public 
TheOneFunction(iIndex)
{
    new 
szCmd[33];
    
read_argv(4szCmdsizeof szCmd);
    
    new 
iNum;
    
    if (
contain(szCmd"/hp"))
    {
        
replace(szCmdsizeof szCmd"/hp """);

        
iNum str_to_num(szCmd);
        new 
iHealth get_user_health(iIndex);
        
        
set_user_health(iIndexiHealth healthPotion[iNum]);
    }
    else if (
contain(szCmd"/ammo"))
    {
        
replace(szCmdsizeof szCmd"/ammo """);

        
iNum str_to_num(szCmd);
        new 
iArmor get_user_armor(iIndex);
        
        
set_user_armor(iIndexiArmor armorPotion[iNum]);
    }

__________________
What are you looking for here?

Last edited by DavidJr; 05-26-2014 at 11:43.
DavidJr is offline
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 05-26-2014 , 10:42   Re: [HELP] Switch between const size numbers
Reply With Quote #5

F*ck right Got it now, thanks. And you can agree, that I'm still searching an more optional way, if there is! For now, that's enough.
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 05-26-2014 , 11:15   Re: [HELP] Switch between const size numbers
Reply With Quote #6

Code:
read_argv(4, szCmd, sizeof szCmd);
There is only 1 argument with the say command.
The SayText message on the other hand contains 4 arguments, and the text is within the 4th argument if it's a say command that triggered the message.

sizeof is not suitable for strings alone. use sizeof X -1 or charsmax().

Code:
new iNum;       healthPotion[iNum]
Makes no sense at all.

iNum will cause index out of bounds unless limited.

You're far better of hardcoding it. This only increases the CPU usage and decreases readability, unless I'm misunderstanding something.
__________________

Last edited by Black Rose; 05-26-2014 at 11:19.
Black Rose is offline
Old 05-26-2014, 11:23
Flick3rR
This message has been deleted by Flick3rR. Reason: New stuff, I guess that's bullshit :D
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 05-26-2014 , 11:26   Re: [HELP] Switch between const size numbers
Reply With Quote #7

Okay, any suggestions to detect in one function which command is exactly used?
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
DavidJr
Senior Member
Join Date: Apr 2012
Old 05-26-2014 , 11:45   Re: [HELP] Switch between const size numbers
Reply With Quote #8

Quote:
Originally Posted by Black Rose View Post
Code:
read_argv(4, szCmd, sizeof szCmd);
There is only 1 argument with the say command.
The SayText message on the other hand contains 4 arguments, and the text is within the 4th argument if it's a say command that triggered the message.

sizeof is not suitable for strings alone. use sizeof X -1 or charsmax().
Ah, may I know know why must it be x - 1?

Quote:
Originally Posted by Black Rose View Post
Code:
new iNum;       healthPotion[iNum]
Makes no sense at all.

iNum will cause index out of bounds unless limited.

You're far better of hardcoding it. This only increases the CPU usage and decreases readability, unless I'm misunderstanding something.
Whoa, I was drunk. Sorry, I edited the code.
__________________
What are you looking for here?
DavidJr is offline
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 05-26-2014 , 12:26   Re: [HELP] Switch between const size numbers
Reply With Quote #9

And may you explain how exactly iNum gets 1 or 2 depending on str_to_num(szCmd) and why it gives me only HP whatever I type
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
DavidJr
Senior Member
Join Date: Apr 2012
Old 05-26-2014 , 12:44   Re: [HELP] Switch between const size numbers
Reply With Quote #10

Simply, I have made the filters. The filters are for the command hp or ap itself. For HP, I've filtered the iNum limit e.g: the const variable has 3 members, so I limit the iNum only three (0, 1, 2 and 3).

If you debug, e.g: Player types "/hp 2"

The szCmd result will be "/hp 2" (String).
The szCmd is converted to iNum using str_to_num(szCmd) and result will be "0" (String). (I don't know the value, just random example)

It will get the wrong result, so I replace the "/hp " to "" and the result will be "2" then I convert it using str_to_num(szCmd) and the result is going to be a correct integer.
__________________
What are you looking for here?

Last edited by DavidJr; 05-26-2014 at 12:50.
DavidJr is offline
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 09:43.


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