AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Switch between const size numbers (https://forums.alliedmods.net/showthread.php?t=241029)

Flick3rR 05-26-2014 09:16

[HELP] Switch between const size numbers
 
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. :)

DavidJr 05-26-2014 09:49

Re: [HELP] Switch between const size numbers
 
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")) 


Flick3rR 05-26-2014 10:12

Re: [HELP] Switch between const size numbers
 
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. :)

DavidJr 05-26-2014 10:33

Re: [HELP] Switch between const size numbers
 
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]);
    }



Flick3rR 05-26-2014 10:42

Re: [HELP] Switch between const size numbers
 
F*ck right :D 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. :)

Black Rose 05-26-2014 11:15

Re: [HELP] Switch between const size numbers
 
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.

Flick3rR 05-26-2014 11:26

Re: [HELP] Switch between const size numbers
 
Okay, any suggestions to detect in one function which command is exactly used?

DavidJr 05-26-2014 11:45

Re: [HELP] Switch between const size numbers
 
Quote:

Originally Posted by Black Rose (Post 2142686)
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 (Post 2142686)
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.

Flick3rR 05-26-2014 12:26

Re: [HELP] Switch between const size numbers
 
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 :D

DavidJr 05-26-2014 12:44

Re: [HELP] Switch between const size numbers
 
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.


All times are GMT -4. The time now is 09:43.

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