Thank you very much for your answer, I learned a lot in the past few days, but I can't solve one problem. I made a simple plugin, where you can spawn an entity and then toggle entities display, everything works fine but I found a bug and I can't find the solution.
Problem: After entity is being spawned and I am trying to disable entities display for myself
(Using SetTransmit SDK Hook), entity disappears but glow remains.
How to reproduce the bug?
Plugin source:
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#pragma semicolon 1
#pragma newdecls required
#define MODEL_CHICKEN "models/chicken/chicken.mdl"
bool g_clientEntityDisplay[MAXPLAYERS + 1] = { true, ... };
int g_clientEntityRef[MAXPLAYERS + 1] = { INVALID_ENT_REFERENCE, ... };
public Plugin myinfo =
{
name = "Entities Test",
author = "Pavelas",
description = "Spawn a Chicken, add Glow Effect, use Transmit",
version = "1.0.0"
}
public void OnPluginStart()
{
RegConsoleCmd("sm_spawn", Command_Spawn);
RegConsoleCmd("sm_destroy", Command_Destroy);
RegConsoleCmd("sm_toggle", Command_Toggle);
}
public void OnMapStart()
{
PrecacheModel(MODEL_CHICKEN, true);
}
public void OnPluginEnd()
{
for (int i = 0; i < sizeof(g_clientEntityRef); i++) {
DestroyEntity(i);
}
}
public Action Command_Spawn(int client, int args)
{
DestroyEntity(client);
CreateEntity(client);
return Plugin_Handled;
}
public Action Command_Destroy(int client, int args)
{
if (g_clientEntityRef[client] == INVALID_ENT_REFERENCE) {
PrintToChat(client, "[ENTITIES]: You do not have any spawned entities.");
return Plugin_Handled;
}
DestroyEntity(client);
return Plugin_Handled;
}
public Action Command_Toggle(int client, int args)
{
g_clientEntityDisplay[client] = !g_clientEntityDisplay[client];
PrintToChat(client, "[ENTITIES]: Display entities option changed: %s", g_clientEntityDisplay[client] ? "Display" : "Hide");
return Plugin_Handled;
}
public Action Event_OnSetTransmit(int entity, int client)
{
if (g_clientEntityDisplay[client]) {
return Plugin_Continue;
}
return Plugin_Handled;
}
void CreateEntity(int client)
{
if (!IsPlayerAlive(client)) {
return;
}
// Get client aim origin in order to spawn an entity there
float origin[3];
GetAimOrigin(client, origin);
// Trying to create an entity
int entity = CreateEntityByName("prop_dynamic_glow");
if (!IsValidEntity(entity)) {
return;
}
// Set transmit entity
if (!SDKHookEx(entity, SDKHook_SetTransmit, Event_OnSetTransmit)) {
return;
}
// Entity properties
DispatchKeyValue(entity, "model", MODEL_CHICKEN); // Add chicken model path
DispatchKeyValue(entity, "solid", "6");
DispatchKeyValue(entity, "glowstyle", "0");
DispatchKeyValue(entity, "glowdist", "10000.0");
DispatchKeyValue(entity, "glowcolor", "255 0 0");
DispatchKeyValue(entity, "glowenabled", "1");
DispatchSpawn(entity);
TeleportEntity(entity, origin, NULL_VECTOR, NULL_VECTOR);
// Attach this entity to the client
g_clientEntityRef[client] = EntIndexToEntRef(entity);
}
void DestroyEntity(int client)
{
if (g_clientEntityRef[client] == INVALID_ENT_REFERENCE) {
return;
}
int entity = EntRefToEntIndex(g_clientEntityRef[client]);
if (!IsValidEntity(entity)) {
PrintToChat(client, "[ENTITIES]: Impossible to destroy invalid entity: %d", entity);
return;
}
// Unhook set transmit
SDKUnhook(entity, SDKHook_SetTransmit, Event_OnSetTransmit);
// Destroy the entity
AcceptEntityInput(entity, "Kill");
// Detach entity from the client
g_clientEntityRef[client] = INVALID_ENT_REFERENCE;
}
public bool TraceEntityFilterPlayer(int entity, int contentsMask)
{
return entity > MaxClients;
}
stock void GetAimOrigin(int client, float origin[3])
{
float pos[3], angles[3];
GetClientEyePosition(client, pos);
GetClientEyeAngles(client, angles);
Handle trace = TR_TraceRayFilterEx(pos, angles, MASK_SHOT, RayType_Infinite, TraceEntityFilterPlayer);
if (TR_DidHit(trace)) {
TR_GetEndPosition(origin, trace);
}
CloseHandle(trace);
}