AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Extract data from string (https://forums.alliedmods.net/showthread.php?t=293013)

gravi123 01-20-2017 19:52

Extract data from string
 
word1#word2#word3#

Is it possible to fill an array with words before each #
Loads on round start

Bugsy 01-20-2017 20:41

Re: Extract data from string
 
Of course there is a way. Explain exactly what you're trying to do because there may be a different way to go about this. If not, I can show you how to parse the values.

gravi123 01-20-2017 21:44

Re: Extract data from string
 
I was actually thinking of creating an array which contains weapons each player class should have, like

weapon_glock18#weapon_deagle#

And on round start to give_item

PRoSToTeM@ 01-20-2017 22:33

Re: Extract data from string
 
Why #? Why not comma or space?

fysiks 01-20-2017 23:06

Re: Extract data from string
 
Are you planning on reading this info from a cvar or a file? If not, and you are hard coding this, it doesn't make sense to store the info in a string.

If you are reading it from a cvar or a file, a comma is probably recommended because it's more common (and proper for any list in a written language) and will therefore be more easily understood.

gravi123 01-21-2017 05:13

Re: Extract data from string
 
Can be space, comma, doesn't really matter.

Lets go with the file then. I am preety sure I already know how to do this from a file, but don't know how to parse data if I have x weapons for each class.

Craxor 01-21-2017 06:07

Re: Extract data from string
 
Something like that?

PHP Code:

#include <amxmodx>
#include <fun>

new const WeaponClass[] [] =
{
    
"weapon_deagle"// 0
    
"weapon_awp",    // 1
    
"weapon_glock18" // 2
};

enum _:ClassType
{
    
CLASS_DEAGLE 0,
    
CLASS_AWP 1,
    
CLASS_GLOCK18 2
}

new 
gClassId[33];

public 
plugin_init( )
{
    
register_plugin"Weapon Class" "1.0" "Craxor" );

    
register_clcmd"say /give" "GiveWeapon" );

    
register_clcmd"say /awp" "awp" );
    
register_clcmd"say /deagle" "deagle" );
    
register_clcmd"say /glock" "glock" );
}

public 
awpid )
{
    
SetPlayerClassid ClassType:CLASS_AWP );    
}

public 
deagleid )
{
    
SetPlayerClassid ClassType:CLASS_DEAGLE );    
}

public 
glockid )
{
    
SetPlayerClassid ClassType:CLASS_GLOCK18 );
}

public 
GiveWeaponid )
{
    
give_itemid WeaponClassgClassId[id] ] );
}

SetPlayerClassindex ClassTypeType )
{
    
gClassIdindex ] = _:Type;    



gravi123 01-21-2017 11:35

Re: Extract data from string
 
I don't really see how that helps

I have managed to make this and it works for up to 2 weapons with this code. Is there a better way to parse data for X number of weapons or must i make more if conditions for more weapons

PHP Code:

    new file[256] = "addons/amxmodx/configs/plugin/weapons.ini";   
    new 
number_of_lines file_size(file1);
    new 
line[256];
    new 
number;
       
    for(new 
0number_of_linesi++)  {  
        
        
read_file(fileiline255number)
        
        if(
number && get_class == i) {
            
            
            new 
number_of_weapons replace_all(line255"#"" ");
        
            new 
weapon[2][32
        
            if(
number_of_weapons == 1) {            
                
parse(lineweapon[0][0], 31);
            }
            if(
number_of_weapons == 2) {
                
parse(lineweapon[0][0], 31weapon[1][0], 31);
            }
                    
            for(new 
j=0j<number_of_weaponsj++) {
                
give_item(id,weapon[j][0]);
            }
            
        }
    } 


HamletEagle 01-21-2017 13:02

Re: Extract data from string
 
Of course there is a better way. Look into strtok and also the new file natives. Don't use read_file/write_file, they are old and dreprecated.

fysiks 01-21-2017 13:13

Re: Extract data from string
 
Here is an example that answers your original question:

PHP Code:

    new szString[128] = "item1,item2,item3,item4,item5" // Make sure that this is long enough to hole your data.
    
new szItems[4][32// Change the first index to the maximum number of items that you expect to load.
    
new token ',' // Change this to any single character that will never be found in one of the item names.
    
new szThisItem[32]
    new 
iItemCount // Number of items found.
    
    
for(new 0sizeof(szItems); i++)
    {
        
strtok(szStringszThisItemcharsmax(szThisItem), szStringcharsmax(szString), token1)
        if( 
szThisItem[0] )
        {
            
copy(szItems[i], charsmax(szItems[]), szThisItem)
            
iItemCount++
        }
        else
        {
            break
        }
    } 



All times are GMT -4. The time now is 20:42.

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