AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Entity not being created (https://forums.alliedmods.net/showthread.php?t=331713)

SpirT 04-03-2021 12:48

Entity not being created
 
Hey

I am trying to create an entity on the floor where a player dies (with a custom model) and detect whether someone touches it.

I have the following code:
PHP Code:

#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "SpirT"
#define PLUGIN_VERSION "1.0"

#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <multicolors>

char entitymdl[] = "models/weapons/spirtmodels/entityspawn.mdl";

#pragma newdecls required

public Plugin myinfo 
{
    
name "[SpirT] Spawn entity on player death",
    
author PLUGIN_AUTHOR,
    
description "",
    
version PLUGIN_VERSION,
    
url ""
};

public 
void OnPluginStart()
{
    
HookEvent("player_death"OnPlayerDeath);
}

public 
Action OnPlayerDeath(Event event, const char[] namebool dontBroadCast)
{
    
int client GetClientOfUserId(GetEventInt(event"userid"));
    
int attacker GetClientOfUserId(GetEventInt(event"attacker"));
    
    if(
client == attacker || attacker == || !attacker)
    {
        return 
Plugin_Continue;
    }
    
    
PrintToChatAll("Detected attacker %N and victim %N"attackerclient);
    
SpawnEntity(client);
    return 
Plugin_Handled;
}

void SpawnEntity(int client)
{
    
int coin;
    
    if((
coin CreateEntityByName("prop_dynamic")) != -1)
    {
        
PrintToChatAll("Entity is valid");
        
float position[3];
        
GetClientEyePosition(clientposition);
        
TeleportEntity(coinpositionNULL_VECTORNULL_VECTOR);
        
PrintToChatAll("Entity teleported to victim position %f"position);
        
        
DispatchKeyValue(coin"model"entitymdl);
        
PrintToChatAll("Coin model was set to %s"entitymdl);
        
DispatchKeyValue(coin"massScale""8.0");
        
DispatchKeyValue(coin"physicsmode""2");
        
DispatchSpawn(coin);
        
        
SetEntProp(coinProp_Send"m_usSolidFlags"8);
        
SetEntProp(coinProp_Send"m_CollisionGroup"11);
        
        
SDKHook(coinSDKHook_StartTouchOnTouchEntity);
    }
}

public 
Action OnTouchEntity(int entityint client)
{
    
PrintToChatAll("Touch hook started");
    
char model[128];
    
model[0] = '\0';
    
    
GetEntPropString(entityProp_Data"m_ModelName"modelsizeof(model));
    
    if (
client && client <= MaxClients && IsPlayerAlive(client) && StrEqual(modelentitymdl))
    {
        
StartDeleting(entityclient);
    }
}

void StartDeleting(int entityint client)
{
    if(
IsValidEntity(entity))
    {
        
AcceptEntityInput(entity"Kill");
        
PrintToChatAll("%N just picked up the item"client);
        
SDKUnhook(entitySDKHook_StartTouchOnTouchCoin);
    }


So, with this source that I have provided, it sends the message as creating the entity and deleting it at the same time. So, how can I keep the entity on the player position and only delete it once someone touches it?

Regards,
SpirT.

Marttt 04-03-2021 14:09

Re: Entity not being created
 
Your code looks ok.

I think is missing a debug to detect what is touching the entity, probably with that info you can understand what is happening

Otherwise, try waiting for a frame (or create a timer to test) before spawning the entity.
Maybe it is touching the death player itself (even with the IsPlayerAlive(client))

Also doesn't make much sense comparing the model in the OnTouchEntity check

SpirT 04-03-2021 14:26

Re: Entity not being created
 
Quote:

Originally Posted by Marttt (Post 2742870)
Your code looks ok.

I think is missing a debug to detect what is touching the entity, probably with that info you can understand what is happening

Otherwise, try waiting for a frame (or create a timer to test) before spawning the entity.
Maybe it is touching the death player itself (even with the IsPlayerAlive(client))

Also doesn't make much sense comparing the model in the OnTouchEntity check

Thanks for your reply. I already tried to do some debugging. I also tried to kill the opponent in a far distance, but it still says that I have touched it!

With the timer do you mean creating it before deleting it?

Marttt 04-03-2021 14:48

Re: Entity not being created
 
by Timer I mean hooking "SDKHook(coin, SDKHook_StartTouch, OnTouchEntity);" inside the timer.

andi67 04-03-2021 16:00

Re: Entity not being created
 
Quote:

void SpawnEntity(int client)
{
int coin;

if((coin = CreateEntityByName("prop_dynamic")) != -1)
{
PrintToChatAll("Entity is valid");
float position[3];
GetClientEyePosition(client, position);
TeleportEntity(coin, position, NULL_VECTOR, NULL_VECTOR);
PrintToChatAll("Entity teleported to victim position %f", position);

DispatchKeyValue(coin, "model", entitymdl);
PrintToChatAll("Coin model was set to %s", entitymdl);
DispatchKeyValue(coin, "massScale", "8.0");
DispatchKeyValue(coin, "physicsmode", "2");
DispatchSpawn(coin);

SetEntProp(coin, Prop_Send, "m_usSolidFlags", 8);
SetEntProp(coin, Prop_Send, "m_CollisionGroup", 11);
CreateTimer(1.0, CreateCoin,coin,TIMER_FLAG_NO_MAPCHANGE);
// SDKHook(coin, SDKHook_StartTouch, OnTouchEntity);
}
}

public Action CreateCoin(Handle timer, int coin)
{
SDKHook(coin, SDKHook_StartTouch, OnTouchEntity);
}

Something like this........

MasterMind420 04-03-2021 16:02

Re: Entity not being created
 
Put a delay in between player death and creating it, im guessing its hitting the dead player. Try a RequestFrame delay if that doesn't work then use a timer at 0.1 sec...that should work, if it does not then go to 0.2 etc. Or just do what Andi67 suggests.

Bacardi 04-03-2021 16:41

Re: Entity not being created
 
...depends, do custom model have physic model, so touch would activate ?
You can look from Hammer map editor, via model browser, check "render collision model" you should see red wireframes.
Or from Model Viewer, check physics model.

What I remember.
*edit Try different model ? ex: "models\weapons\w_eq_smokegrenade_dropped.mdl "

SpirT 04-04-2021 05:35

Re: Entity not being created
 
Quote:

Originally Posted by Marttt (Post 2742875)
by Timer I mean hooking "SDKHook(coin, SDKHook_StartTouch, OnTouchEntity);" inside the timer.

I tried that and it worked. Also, the model path was wrong, but even though it worked!

I'll give a try to andi67 source.

SpirT 04-05-2021 04:12

Re: Entity not being created
 
Andi67's code worked perfectly, I changed also the timer from 1.0 to 0.5 and it works great! I just have another question. With these settings, the model spawns @ player death position, but I'd like it to fall to the floor, like if we would spawn any weapon in the air. Is that possible? (CS:GO)

andi67 04-05-2021 05:15

Re: Entity not being created
 
Change prop_dynamic to prop_physics.....


All times are GMT -4. The time now is 17:47.

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