MAGNAT2645 |
02-23-2024 06:39 |
Re: Pass an array to a function
PHP Code:
public Action Test(client, args) { 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(dir, name, sizeof name, type)) { if (type == FileType_File) { if (StrEqual(name[strlen(name) - 4], ".wav", false) || StrEqual(name[strlen(name) - 4], ".mp3", false)) { if (StrContains(name, Filtr, false) != -1) { FormatEx(path, sizeof(path), "%s/%s", sPath, name); 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 client, int 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(name, sizeof name, type)) { if (type == FileType_File) { if (StrEqual(name[strlen(name) - 4], ".wav", false) || StrEqual(name[strlen(name) - 4], ".mp3", false)) { if (StrContains(name, Filtr, false) != -1) { FormatEx(path, sizeof(path), "%s/%s", sPath, name); array.PushString(path); } } } } dir.Close(); } else LogError("Failed to open directory: %s", sPath); }
|