Raised This Month: $ Target: $400
 0% 

[L4D2] Remove excess weapon_* on the ground


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Author Message
ilham92-cc-sakura
Senior Member
Join Date: Oct 2008
Location: /var/www/index.html
Old 07-08-2024 , 12:36   [L4D2] Remove excess weapon_* on the ground
Reply With Quote #1

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
__________________

Last edited by ilham92-cc-sakura; 07-08-2024 at 12:37. Reason: identation
ilham92-cc-sakura is offline
 


Thread Tools
Display Modes

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 00:55.


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