AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [SOLVED] find_ent_by_owner() on client_disconnect() (https://forums.alliedmods.net/showthread.php?t=163147)

yokomo 07-27-2011 06:54

[SOLVED] find_ent_by_owner() on client_disconnect()
 
Hi,
I got a problem when remove entity created by owner when owner disconnect from server. You can see an example code that i used in server. I got this error log:
Code:

L 07/27/2011 - 17:17:46: [ENGINE] Invalid player 12 (not in-game)
L 07/27/2011 - 17:17:46: [AMXX] Displaying debug trace (plugin "TestCreateEntity.amxx")
L 07/27/2011 - 17:17:46: [AMXX] Run time error 10: native error (native "find_ent_by_owner")

And this is the code:
PHP Code:

public client_disconnect(id)
{
    
//I want remove all ent created by owner when he disconnect from server.
    
RemoveUserEntity(id)
}

public 
EventRoundStart()
{
    
RemoveAllBananaEntity()
}

//Cl_cmd
public CreateBananaEnt(id)
{
    if(!
is_user_alive(id)) return PLUGIN_HANDLED
    
    
if(pev(idpev_flags) & FL_ONGROUND)
    {
        
CreateTripEntity(id)
    }

        return 
PLUGIN_HANDLED
}

CreateTripEntity(id)
{
    static 
Float:origin[3], ent
    pev
(idpev_originorigin)
    
    
ent engfunc(EngFunc_CreateNamedEntityengfunc(EngFunc_AllocString"info_target"))
    if(
pev_valid(ent))
    {
        
set_pev(entpev_classname"banana_ent")
        
set_pev(entpev_originorigin)
        
engfunc(EngFunc_SetModelentMODEL_BANANA)
        
engfunc(EngFunc_SetSizeentFloat:{-3.0, -3.0, -3.0}, Float:{3.03.03.0})
        
set_pev(entpev_solid1)
        
set_pev(entpev_movetype6)
        
set_pev(entpev_sequence1)
        
set_pev(entpev_ownerid)
        
set_rendering(entkRenderFxGlowShell303030kRenderTransAlpha10)
        
emit_sound(entCHAN_VOICESOUND_BANANA1.0ATTN_NORM0PITCH_NORM)
        
client_print(idprint_center"Banana entity has been created")
    }
}

//Remove all ent created by owner
RemoveUserEntity(id)
{
    new 
ent find_ent_by_owner(-1"banana_ent"id0)
    while(
ent 0)
    {
        if(
pev_valid(ent)) remove_entity(ent)
        
ent find_ent_by_owner(-1"banana_ent"id0)
    }
}

//Remove all banana ent found on map 
RemoveAllBananaEntity()
{    
    new 
ent find_ent_by_class(-1"banana_ent")
    while(
ent 0)
    {
        
remove_entity(ent)
        
ent find_ent_by_class(-1"banana_ent")
    }


All i want to do is remove all entity created by owner when he left the server.

Exolent[jNr] 07-27-2011 10:59

Re: [Help] find_ent_by_owner() on client_disconnect()
 
If you are getting that error, then you will have to manually check for ownership using the find_ent_by_class() and entity_get_edict(..., EV_INT_owner).

yokomo 07-27-2011 12:03

Re: [Help] find_ent_by_owner() on client_disconnect()
 
Quote:

Originally Posted by Exolent[jNr] (Post 1519682)
If you are getting that error, then you will have to manually check for ownership using the find_ent_by_class() and entity_get_edict(..., EV_INT_owner).

Can you make an example how to check it?

***Edited***

I've looking in OT's nademode code and found how the way he remove owner's entity it look like this:
PHP Code:

RemovePlayerEntity(id)
{
    static 
ent
    ent 
= -1
    
    
while ((ent find_ent_by_class(entTRIP_CLASSNAME)))
    {
        if(
pev(entpev_owner) != id)
            continue
        
        if(
pev_valid(ent)) remove_entity(ent)
    }


Tested no error log yet.

Exolent[jNr] 07-27-2011 12:16

Re: [Help] find_ent_by_owner() on client_disconnect()
 
Code:
new ent = find_ent_by_class -1 while ent > 0     if valid ent and id = ent owner         remove ent     ent = find_ent_by_class ent

Hunter-Digital 07-27-2011 12:24

Re: [Help] find_ent_by_owner() on client_disconnect()
 
Hmm, wouldn't this:
Code:

    while(ent > 0)
    {
       
remove_entity(ent)
       
ent = find_ent_by_class(-1, "banana_ent")
    }

...make an infinite loop ? since find_ent_by_class() would always return the same entity over and over...

Anyway... you should use ent instead of -1 there and asign ent to -1 when you create it... but I'm gonna add the owner check aswell:
Code:

new ent = -1
// "id" is player's entity id

while((ent = find_ent_by_class(ent, "banana_ent")) != 0)
{
    if(entity_get_int(ent, EV_ENT_owner) == id)
        remove_entity(ent)
}


Exolent[jNr] 07-27-2011 12:32

Re: [Help] find_ent_by_owner() on client_disconnect()
 
Quote:

Originally Posted by Hunter-Digital (Post 1519736)
...make an infinnite loop ? since find_ent_by_class() would always return the same entity over and over...

It can't return the same entity after it is removed.

Hunter-Digital 07-27-2011 12:39

Re: [Help] find_ent_by_owner() on client_disconnect()
 
Quote:

Originally Posted by Exolent[jNr] (Post 1519741)
It can't return the same entity after it is removed.

Oh right =) but if it didn't remove that entity the loop would've failed.

Still, it would be faster to input the entity back to the function to limit the search range... just pointing it out.

EDIT: Also, AFAIK, it would also be useless to check for valid entity since that function only returns valid entities... reference to yokomo's last edit.

yokomo 07-27-2011 14:56

Re: [Help] find_ent_by_owner() on client_disconnect()
 
Ok after 2 hours in server using this code:
PHP Code:

RemovePlayerEntity(id

    static 
ent 
    ent 
= -
     
    
while ((ent find_ent_by_class(entCLASSNAME))) 
    { 
        if(
pev(entpev_owner) != id
            continue 
         
        if(
pev_valid(ent)) remove_entity(ent
    } 


No error log found yet :) thanks guys.

ConnorMcLeod 07-27-2011 14:58

Re: [Help] find_ent_by_owner() on client_disconnect()
 
You could try to hook FM_ClientDisconnect PRE and then send find_ent_by_owner, but if you need only a specific classname, then the code you actually have is ok.


All times are GMT -4. The time now is 01:09.

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