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

Pass an array to a function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drimacus
Member
Join Date: Jun 2011
Old 02-22-2024 , 14:05   Pass an array to a function
Reply With Quote #1

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);

Drimacus is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 02-23-2024 , 06:39   Re: Pass an array to a function
Reply With Quote #2

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);

__________________

Last edited by MAGNAT2645; 02-23-2024 at 06:51.
MAGNAT2645 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 14:55.


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