Raised This Month: $12 Target: $400
 3% 

[CS:GO] OnEntityCreated + SDKSpawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 10-05-2020 , 15:12   [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #1

PHP Code:

public void OnEntityCreated(int entity, const char[] classname)
{
    
SDKHook(entitySDKHook_SpawnSDK_OnEntitySpawn);
}

public 
Action SDK_OnEntitySpawn(int entity)
{
    if (
entity < -1)
    {
        
entity EntRefToEntIndex(entity);
        if (
entity == INVALID_ENT_REFERENCE)
        {
            return 
Plugin_Continue;
        }
    }

    
// here for func_door, door_rotating and movelinear I do this: 
t    // there are some validations, I removed them for this example
    
HookSingleEntityOutput(entity"OnFullyOpen"Output_OnDoorStateChange);
}

public 
void Output_OnDoorStateChange(const char[] outputint callerint activatorfloat delay)
{
    
// here print something for testing purpose

So for func_door, Output_OnDoorStateChange prints the message in most cases, but sometimes it doesn't print anything.
I think there's a problem with entity validation, maybe it's not valid here? Maybe the entity is a reference and if (entity == INVALID_ENT_REFERENCE) return true?

How can I hook entity spawn for every entity? OnEntityCreated doesn't work with func_brushes, I did some testing with it, and I had to do an iteration on round_start.

Also I replaced HookSingleEntityOutput with HookEntityOutput, I hope it will work for my entities and will not trigger different ones (for func_button, HookEntityOutput - OnPressed triggers another entity as well).
__________________
Ilusion9 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-05-2020 , 15:34   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #2

I have noticed, when map start, game flush all entities which disappear right after.

So, OnEntityCreated, take entity reference and past it to next frame (or use timer 0.0).
On the next frame, look, do you get entity index from entity reference.

If you get index, then hook entity.
And entity also have correct data, because at OnEntityCreated it is... empty


*edit
I mean OnEntityCreated. Not OnEntitySpawn


*edit
Use HookSingleEntityOutput on every round_start (or round_freeze_end).

HookEntityOutput again will work all teh time from OnPluginStart.

PHP Code:
#include <sdktools>

public void OnPluginStart()
{
    
HookEntityOutput("prop_door_rotating""OnFullyOpen"OnFullyOpen);
    
    
HookEvent("round_freeze_end"round_freeze_end);
}

public 
void OnFullyOpen(const char[] outputint callerint activatorfloat delay)
{
    
PrintToServer("OnFullyOpen caller %i, activator %i"calleractivator);
}

public 
void round_freeze_end(Event event, const char[] namebool dontBroadcast)
{
    
char targetname[MAX_NAME_LENGTH];
    
int entity = -1;
    
    while((
entity FindEntityByClassname(entity"prop_door_rotating")) != INVALID_ENT_REFERENCE)
    {
        
GetEntPropString(entityProp_Data"m_iName"targetnamesizeof(targetname));
        
PrintToServer("%s"targetname);
        
        
// do action once and unhook
        
HookSingleEntityOutput(entity"OnFullyClosed"OnFullyClosedtrue);
    }
}

public 
void OnFullyClosed(const char[] outputint callerint activatorfloat delay)
{
    
PrintToServer("OnFullyClosed caller %i, activator %i"calleractivator);


Last edited by Bacardi; 10-05-2020 at 16:32.
Bacardi is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 10-05-2020 , 18:45   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #3

HookEntityOutputs triggers another entities as well, for example some instance_* entity for func_door::OnFullyClosed etc. (I just deleted the error log file). I'm just hoping that will work for all func_doors.

Looping in round_start it's something, but I need to hook entity spawn in order to change some settings of entities. And there are point_template cases where entities are spawned after round start (after some traps are activated).
__________________
Ilusion9 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-05-2020 , 19:19   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #4

...well try check entity classname at OnEntityCreated.
If entity classname is right, get entity reference code: int ref = EntIndexToEntRef(entity)

Pass reference code into RequestFrame(nextframe, ref)

On nextframe callback, convert reference code back to entity index:
int entity = EntRefToEntIndex(ref)

If entity != INVALID_ENT_REFERENCE, your entity exist and you can do your stuff.



-Not need OnEntitySpawn at all.
-INVALID_ENT_REFERENCE is -1
-entity index and entity reference code are like player index and userid
__________________
Do not Private Message @me

Last edited by Bacardi; 10-05-2020 at 19:20.
Bacardi is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 10-05-2020 , 20:29   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #5

Well I usually use SpawnPost in the SDKHook, also noticed that sometimes we need an extra frame to things work properly (usually for getting properties)

Here is a snippet that I usually use

PHP Code:
public void OnEntityCreated(int entity, const char[] sClassname)
{
    
SDKHook(entitySDKHook_SpawnPostSpawnPost);
}

/****************************************************************************************************/

public void SpawnPost(int entity)
{
    if (!
IsValidEntity(entity))
        return;

    
// 1 frame later required to get some properties
    
RequestFrame(OnNextFrameEntIndexToEntRef(entity));
}

/****************************************************************************************************/

void OnNextFrame(int entityRef)
{
    
int entity EntRefToEntIndex(entityRef);

    if (
entity == INVALID_ENT_REFERENCE)
        return;
        
    
// Do something

And of course, filter the classname on OnEntityCreated if possible.
__________________

Last edited by Marttt; 10-05-2020 at 20:37.
Marttt is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-06-2020 , 13:53   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #6

++ not forget, that some entities are changes their class name during transition from OnEntityCreated to SpawnPost stage.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 10-06-2020 , 13:59   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #7

Quote:
Originally Posted by Dragokas View Post
++ not forget, that some entities are changes their class name during transition from OnEntityCreated to SpawnPost stage.
Is one of them "Weapon_item_spawn" ??
Or does it get destroyed ?
__________________
Do not Private Message @me
Bacardi is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 10-06-2020 , 14:40   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #8

Quote:
Originally Posted by Dragokas View Post
++ not forget, that some entities are changes their class name during transition from OnEntityCreated to SpawnPost stage.
for real?
I use SDK_Spawn for all entities and check their classnames there, should that be the problem?
__________________
Ilusion9 is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 10-06-2020 , 14:49   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #9

Sorry, don't remember which one. Something with "_spawn" surely was the case.
Not hard to just print everything in logfile, and check.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 10-06-2020 , 16:29   Re: [CS:GO] OnEntityCreated + SDKSpawn
Reply With Quote #10

Quote:
Originally Posted by Dragokas View Post
++ not forget, that some entities are changes their class name during transition from OnEntityCreated to SpawnPost stage.
I didn't know that! Thanks for pointing this, gonna check if I found some, in case you remember someday, please post here ^^
__________________
Marttt is offline
Reply


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 05:30.


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