As per title, some plugins spawn a lot of weapon time to time, so I plan to remove excess weapon_* but I didn't see weapon get removed, I not sure if my code is wrong
Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#define MAX_WEAPONS 18 // Maximum allowed weapon entities
#define CHECK_INTERVAL 5.0 // Interval in seconds to check for weapon entities
Handle g_Timer = INVALID_HANDLE;
public Plugin myinfo =
{
name = "[L4D2] Limit Weapons Ground",
author = "Anime4000",
description = "Limit weapons on the ground",
version = "1.0.0",
url = "https://steamcommunity.com/id/anime4000/"
}
public void OnPluginStart()
{
// Start the timer if it's not already running
if (g_Timer == INVALID_HANDLE)
{
g_Timer = CreateTimer(CHECK_INTERVAL, Timer_CheckWeapons, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}
}
public void OnMapStart()
{
// Start the timer if it's not already running
if (g_Timer == INVALID_HANDLE)
{
g_Timer = CreateTimer(CHECK_INTERVAL, Timer_CheckWeapons, _, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action Timer_CheckWeapons(Handle timer, any data)
{
CheckAndManageWeapons();
return Plugin_Continue;
}
public void CheckAndManageWeapons()
{
// Array to store the entity indices of weapons
int weaponEntities[MAX_WEAPONS + 1];
int weaponCount = 0;
// Iterate through all entities
for (int entity = 1; entity <= GetMaxEntities(); entity++)
{
if (IsValidEntity(entity) && IsWeaponEntity(entity))
{
weaponEntities[weaponCount++] = entity;
// If we have found more weapons than the maximum allowed, remove the extras
if (weaponCount > MAX_WEAPONS)
{
PrintToConsoleAll("RemoveEntity Weapon %d (%d)", entity, weaponCount - 1);
AcceptEntityInput(entity, "Kill", -1, -1, -1);
}
}
}
}
public bool IsWeaponEntity(int entity)
{
// Check if the entity is a valid weapon by checking the classname prefix
char classname[64];
GetEdictClassname(entity, classname, sizeof(classname));
// Check if the classname starts with "weapon_"
if (strncmp(classname, "weapon_", 7) == 0)
{
return true;
}
return false;
}
What is different between RemoveEntity(int); or AcceptEntityInput(int, "kill);
some said using AcceptEntityInput is better way, but I didn't see any weapons on the ground get removed
__________________