AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Pass an array to a function (https://forums.alliedmods.net/showthread.php?t=346383)

Drimacus 02-22-2024 14:05

Pass an array to a function
 
Hello!
How do you pass an array with other variables?
The code below does not work. This is my vision.
PHP Code:

public Action Test(clientargs)
{
    
MyFunc(MyArray"sound/hit/""kevlar"); // How do you pass an array with other variables?
}

void MyFunc(const array[], char sPathFiltr)
{
    new 
Handle:dir;

    if ((
dir OpenDirectory(sPath)))
    {
        
char name[128];
        
char path[PLATFORM_MAX_PATH];
        new 
FileType:type;

        while (
ReadDirEntry(dirname128type))
        {
            if (
type == FileType_File)
            {
                if (
StrEqual(name[strlen(name) - 4], ".wav"false) || (StrEqual(name[strlen(name) - 4], ".mp3"false)))
                {
                    if (
StrContains(nameFiltrfalse) != -1)
                    {
                        
FormatEx(pathsizeof(path), "%s/%s"sPathname);
                        array.
PushString(path);
                    }
                }
            }
        }
        
CloseHandle(dir);
    }
    else
        
LogError("Failed to open directory: %s"sPath);



MAGNAT2645 02-23-2024 06:39

Re: Pass an array to a function
 
PHP Code:

public Action Test(clientargs)
{
    
ArrayList MyArray = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
    
MyFunc(MyArray"sound/hit/""kevlar");
    
// Do something with MyArray and then destroy it to avoid memory leaks
    
CloseHandle(MyArray);
}

void MyFunc(ArrayList array, const char[] sPath, const char[] Filtr)
{
    new 
Handle:dir;

    if ((
dir OpenDirectory(sPath)))
    {
        
char name[128];
        
char path[PLATFORM_MAX_PATH];
        new 
FileType:type;

        while (
ReadDirEntry(dirnamesizeof nametype))
        {
            if (
type == FileType_File)
            {
                if (
StrEqual(name[strlen(name) - 4], ".wav"false) || StrEqual(name[strlen(name) - 4], ".mp3"false))
                {
                    if (
StrContains(nameFiltrfalse) != -1)
                    {
                        
FormatEx(pathsizeof(path), "%s/%s"sPathname);
                        array.
PushString(path);
                    }
                }
            }
        }
        
CloseHandle(dir);
    }
    else
        
LogError("Failed to open directory: %s"sPath);


You also should use newer syntax (old syntax will be unsupported with newer SourcePawn compiler versions):
PHP Code:

public Action Test(int clientint args)
{
    
ArrayList MyArray = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
    
MyFunc(MyArray"sound/hit/""kevlar"); // How do you pass an array with other variables?
    // Do something with MyArray and then destroy/close it to avoid memory leaks
    
MyArray.Close(); // or "delete MyArray;" but that one also sets the variable to null which is extra unnecessary instruction here
    
return Plugin_Handled// this is important in command callbacks so you won't get "Unknown command" error
}

void MyFunc(ArrayList array, const char[] sPath, const char[] Filtr)
{
    
DirectoryListing dir;

    if ((
dir OpenDirectory(sPath)))
    {
        
char name[128], path[PLATFORM_MAX_PATH];
        
FileType type;

        while (
dir.GetNext(namesizeof nametype))
        {
            if (
type == FileType_File)
            {
                if (
StrEqual(name[strlen(name) - 4], ".wav"false) || StrEqual(name[strlen(name) - 4], ".mp3"false))
                {
                    if (
StrContains(nameFiltrfalse) != -1)
                    {
                        
FormatEx(pathsizeof(path), "%s/%s"sPathname);
                        array.
PushString(path);
                    }
                }
            }
        }
        
dir.Close();
    }
    else
        
LogError("Failed to open directory: %s"sPath);




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

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