AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [CS:GO] OnEntityCreated + SDKSpawn (https://forums.alliedmods.net/showthread.php?t=327727)

Ilusion9 10-05-2020 16:12

[CS:GO] OnEntityCreated + SDKSpawn
 
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).

Bacardi 10-05-2020 16:34

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
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 :P


*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);



Ilusion9 10-05-2020 19:45

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
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).

Bacardi 10-05-2020 20:19

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
...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

Marttt 10-05-2020 21:29

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
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.

Dragokas 10-06-2020 14:53

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
++ not forget, that some entities are changes their class name during transition from OnEntityCreated to SpawnPost stage.

Bacardi 10-06-2020 14:59

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
Quote:

Originally Posted by Dragokas (Post 2720450)
++ 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 ?

Ilusion9 10-06-2020 15:40

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
Quote:

Originally Posted by Dragokas (Post 2720450)
++ 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?

Dragokas 10-06-2020 15:49

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
Sorry, don't remember which one. Something with "_spawn" surely was the case.
Not hard to just print everything in logfile, and check.

Marttt 10-06-2020 17:29

Re: [CS:GO] OnEntityCreated + SDKSpawn
 
Quote:

Originally Posted by Dragokas (Post 2720450)
++ 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 ^^


All times are GMT -4. The time now is 00:38.

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