AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved String array problem (https://forums.alliedmods.net/showthread.php?t=332181)

pan0s 04-28-2021 12:45

String array problem
 
Hi, I want to ask how to declare a 2D string array with initial values on SoucePawn?

on Java, it should be like that
Code:

String[][] g_sItems =
{
        {"item1",},
        {"item2","item3",}
};

on SoucePawn, I tried

Code:

char g_sItems[][][] =
{
        {"item1",},
        {"item2","item3",}
};

but the compiler gives me "error 018: initialization data exceeds declared size."

MAGNAT2645 04-28-2021 13:53

Re: String array problem
 
Code:

char g_sItems1[][] = {
    "ABC",
    "DEF",
    "123",
    "456"
};

char g_sItems2[][][] = {
    { "ABC", "DEF" },
    { "123", "456" },
    { "789", "QWE" }
};

public void OnPluginStart()
{
    for ( int i = 0; i < sizeof g_sItems1; i++ )
        PrintToServer( "%s", g_sItems1[i] );

    PrintToServer( "---" );

    for ( int i = 0, j; i < sizeof g_sItems2; i++ ) {
        for ( j = 0; j < sizeof g_sItems2[]; j++ )
            PrintToServer( "%s", g_sItems2[i][j] );
    }
}

Note that each row in 2D string array must have same number of strings (that's due to how compiler works).
If you want to have, for example, 2 strings in 1st row and 4 strings in 2nd row you can just put empty strings:
Code:

char g_sItems2[][][] = {
    { "ABC", "DEF", "",    ""    },
    { "123", "456", "GGG", "RRR" }
};


pan0s 04-28-2021 15:19

Re: String array problem
 
Quote:

Originally Posted by MAGNAT2645 (Post 2745316)
Code:

char g_sItems1[][] = {
    "ABC",
    "DEF",
    "123",
    "456"
};

char g_sItems2[][][] = {
    { "ABC", "DEF" },
    { "123", "456" },
    { "789", "QWE" }
};

public void OnPluginStart()
{
    for ( int i = 0; i < sizeof g_sItems1; i++ )
        PrintToServer( "%s", g_sItems1[i] );

    PrintToServer( "---" );

    for ( int i = 0, j; i < sizeof g_sItems2; i++ ) {
        for ( j = 0; j < sizeof g_sItems2[]; j++ )
            PrintToServer( "%s", g_sItems2[i][j] );
    }
}

Note that each row in 2D string array must have same number of strings (that's due to how compiler works).
If you want to have, for example, 2 strings in 1st row and 4 strings in 2nd row you can just put empty strings:
Code:

char g_sItems2[][][] = {
    { "ABC", "DEF", "",    ""    },
    { "123", "456", "GGG", "RRR" }
};


Well, I have this idea too. However, I rejected that idea in a second.
When the length of the max is extended, I have to add the empty string for each array to match the size. If I have 100 arrays of it, I have to do it 100 times...
In this stage, I have two better ideas.

Code:

// --First way is split the 2D string array to 1D string array, and pushing it into array list.
char g_sMelees[][] =
{                                              // Melee ID:
        "katana",                          // 0
        "fireaxe",                                  // 1
        "machete",                          // 2
        "flamethrower",                        // 3       
        "knife",                                    // 4
        "chainsaw",                        // 5
        "pitchfork",                        // 6
        "shovel",                                // 7
        "golfclub",                                // 8
        "electric_guitar",                  // 9
        "tonfa",                                    // 10
        "baseball_bat",                    // 11
        "cricket_bat",                      // 12
        "frying_pan",                      // 13
        "crowbar",                          // 14
};

ArrayList g_listItems[SHOP_SIZE];

enum
{      ...
        SHOP_MELEES                                        = 8,
        ...
        SHOP_SIZE                                        = 17,
};

public void OnPluginStart()
{
        for(int i=0; i<SHOP_SIZE; i++)
                g_listItems[i] = new ArrayList(ByteCountToCells(32));

        // melee weapons
        for(int i=0; i<sizeof(g_sMelees); i++) g_listItems[SHOP_MELEES].PushString(g_sMelees[i]);

        // --Second way, don't use the char array to declare, pushing the value to array list instead of it
        g_listItems[SHOP_MELEES].PushString("katana");
        g_listItems[SHOP_MELEES].PushString("fireaxe");
        //...
}



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

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