Raised This Month: $51 Target: $400
 12% 

Solved Extract data from string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gravi123
Member
Join Date: Feb 2016
Old 01-20-2017 , 19:52   Extract data from string
Reply With Quote #1

word1#word2#word3#

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

Last edited by gravi123; 01-24-2017 at 08:13. Reason: Solved
gravi123 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-20-2017 , 20:41   Re: Extract data from string
Reply With Quote #2

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.
__________________
Bugsy is offline
gravi123
Member
Join Date: Feb 2016
Old 01-20-2017 , 21:44   Re: Extract data from string
Reply With Quote #3

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
gravi123 is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 01-20-2017 , 22:33   Re: Extract data from string
Reply With Quote #4

Why #? Why not comma or space?
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-20-2017 , 23:06   Re: Extract data from string
Reply With Quote #5

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.
__________________
fysiks is offline
gravi123
Member
Join Date: Feb 2016
Old 01-21-2017 , 05:13   Re: Extract data from string
Reply With Quote #6

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.

Last edited by gravi123; 01-21-2017 at 05:15.
gravi123 is offline
Craxor
Veteran Member
Join Date: Jan 2016
Location: Romania
Old 01-21-2017 , 06:07   Re: Extract data from string
Reply With Quote #7

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;    

__________________
Project: Among Us

Last edited by Craxor; 01-21-2017 at 06:31.
Craxor is offline
Send a message via ICQ to Craxor
gravi123
Member
Join Date: Feb 2016
Old 01-21-2017 , 11:35   Re: Extract data from string
Reply With Quote #8

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]);
            }
            
        }
    } 
gravi123 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-21-2017 , 13:02   Re: Extract data from string
Reply With Quote #9

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

Last edited by HamletEagle; 01-21-2017 at 13:03.
HamletEagle is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-21-2017 , 13:13   Re: Extract data from string
Reply With Quote #10

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

Last edited by fysiks; 01-21-2017 at 13:16. Reason: Added comments.
fysiks 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 02:41.


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