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

Problem with entity replace(remove entity sometims)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Pawemol12
Member
Join Date: Jun 2012
Location: Poland - Wojnowice
Old 07-30-2012 , 14:42   Problem with entity replace(remove entity sometims)
Reply With Quote #1

Hi,

In Left 4 Dead 2 are 2 good weapons. But one of them is difficult to find. That i try replace this weapon, so as to be equally, you know if an odd number of one weapons will always be more. My way works, but sometimes removes random weapon, so that it is not at all in the game. I do not know why. Here's the code and my function:

min_ilosc is the min count of m60 in the map(it is more important then grenade launcher)
Code:
static ReplaceWeapon(min_ilosc)
{
    new granatniki; //count of grenade launchers
    new m60; //count of m60 rifles
    for ( new index = 1; index <= GetMaxEntities(); index++) // Now i'm counting my weapons
    {
        if (IsValidEntity(index))
        {
            if (IsValidEdict(index))
            {
                new String:weapon_name[64];
                GetEdictClassname(index, weapon_name, sizeof(weapon_name));    
                if (StrEqual(weapon_name, "weapon_grenade_launcher_spawn"))
                {
                    granatniki++;
                }
                else if (StrEqual(weapon_name, "weapon_rifle_m60_spawn"))
                {
                    m60++;
                }                    
            }
        }    
    }                
    for ( new index = 1; index <= GetMaxEntities(); index++) // Now I replace weapons, so that it was equally
    {
        if (IsValidEntity(index))
        {
            if (IsValidEdict(index))
            {
                new String:weapon_name[64];
                GetEdictClassname(index, weapon_name, sizeof(weapon_name));    
                if (StrEqual(weapon_name, "weapon_grenade_launcher_spawn"))
                {
                    if (m60 < granatniki || m60 < min_ilosc)
                    {
                        new Float:Location[3];
                        Entity_GetAbsOrigin(index, Location);
                        new Float:Angles[3];
                        GetEntPropVector(index, Prop_Send, "m_angRotation", Angles);
                        RemoveEdict(index);    
                        new replacement = CreateEntityByName("weapon_rifle_m60_spawn");
                        TeleportEntity(replacement, Location, Angles, NULL_VECTOR);                        
                        DispatchKeyValue(replacement, "count", "1");                        
                        DispatchSpawn(replacement);
                        m60++;
                        granatniki--;
                    }
                    else if (StrEqual(weapon_name, "weapon_rifle_m60_spawn"))
                    {
                        if (m60 > granatniki)
                        {
                            new Float:Location[3];
                            Entity_GetAbsOrigin(index, Location);
                            new Float:Angles[3];
                            GetEntPropVector(index, Prop_Send, "m_angRotation", Angles);
                            RemoveEdict(index);    
                            new replacement = CreateEntityByName("weapon_grenade_launcher_spawn");
                            TeleportEntity(replacement, Location, Angles, NULL_VECTOR);                        
                            DispatchKeyValue(replacement, "count", "1");                        
                            DispatchSpawn(replacement);
                            granatniki++;
                            m60--;
                        }
                    }
                }    
            }
        }
    }    
}

Last edited by Pawemol12; 07-30-2012 at 14:43.
Pawemol12 is offline
MasterOfTheXP
Veteran Member
Join Date: Aug 2011
Location: Cloudbank
Old 07-30-2012 , 14:48   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #2

Why not just use Stripper:Source to replace entities?
__________________
Plugins / My Steam / TF2 Sandbox (plugin beta testing!)
MasterOfTheXP is offline
Pawemol12
Member
Join Date: Jun 2012
Location: Poland - Wojnowice
Old 07-30-2012 , 14:55   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #3

It doesnt support l4d2.
Pawemol12 is offline
McFlurry
Veteran Member
Join Date: Mar 2010
Location: RemoveEdict(0);
Old 07-30-2012 , 21:09   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #4

Stripper: Source works on all Source Engine versions.
__________________
McFlurry is offline
Send a message via Skype™ to McFlurry
Pawemol12
Member
Join Date: Jun 2012
Location: Poland - Wojnowice
Old 07-31-2012 , 05:34   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #5

Hmm, yes, but i wont turn so that it was equally Grenade Launcher and M60, can be the difference of one. But i dont know why first entity is remove, and new dont create

Edit:

I note when m60 remove, sometimes grenade launcher dont create

Last edited by Pawemol12; 07-31-2012 at 06:22.
Pawemol12 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 07-31-2012 , 06:45   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #6

you should redo that code anyway, right now it's doing ~20.000 or more native calls.
I recommend using FindEntityByClassname instead.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0

Last edited by berni; 07-31-2012 at 06:46.
berni is offline
Pawemol12
Member
Join Date: Jun 2012
Location: Poland - Wojnowice
Old 07-31-2012 , 06:49   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #7

Hmm, i dont know how to count entities by while loop and FindEntityByClassname. Can you help?

But my code good count the entities, but when replace sometimes it remove entity and dont create new.

Last edited by Pawemol12; 07-31-2012 at 06:52.
Pawemol12 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 07-31-2012 , 07:07   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #8

Quote:
Originally Posted by Pawemol12 View Post
Hmm, i dont know how to count entities by while loop and FindEntityByClassname. Can you help?
you find an example on the same page in the comments (look at the last comment).

And instead of RemoveEdict(index); you should use AcceptEntityInput(index, "Kill");

Quote:
Originally Posted by Pawemol12 View Post
But my code good count the entities, but when replace sometimes it remove entity and dont create new.
if it's good then don't expect support, I personally puked when I looked at the code
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Pawemol12
Member
Join Date: Jun 2012
Location: Poland - Wojnowice
Old 07-31-2012 , 07:17   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #9

Yesterday I tried a while loop, but it still turned the same And on this page does not count as a while loop.

Now my code look like:
Code:
new ilosc[38]; //jest 38 broni, a na index 0 nic nie daje
for ( new index = 65; index <= GetMaxEntities(); index++)    
{
    if (IsValidEntity(index))
    {
        if (IsValidEdict(index))
        {
            new String:weapon_name[64];
            GetEdictClassname(index, weapon_name, sizeof(weapon_name));
            if (StrEqual(weapon_name, "weapon_rifle_m60_spawn") || StrEqual(weapon_name, "weapon_grenade_launcher_spawn"))
            {
                new id = GetEntProp(index, Prop_Send, "m_weaponID", 4);
                ilosc[id]++;
            }
        }
    }
}    
new index = -1;
new prev = 0;
while ((index = FindEntityByClassname(index, "weapon_rifle_m60_spawn")) != -1 && ilosc[GRENADE_LAUNCHER] + 1 < ilosc[M60])
{
    if (prev);
    {    
        if (IsValidEntity(prev))
        {
            new Float:Location[3];
            Entity_GetAbsOrigin(prev, Location);
            new Float:Angles[3];
            GetEntPropVector(prev, Prop_Send, "m_angRotation", Angles);
            Entity_Kill(prev);
            new index2 = CreateEntityByName("weapon_grenade_launcher_spawn");
            Entity_SetModel(index2, "models/w_models/weapons/w_grenade_launcher.mdl");
            SetEntProp(index2, Prop_Send, "m_weaponID", GRENADE_LAUNCHER);
            TeleportEntity(index2, Location, Angles, NULL_VECTOR);
            DispatchKeyValue(index2, "count", "1");
            DispatchSpawn(index2);                                
            ilosc[GRENADE_LAUNCHER]++;
            ilosc[M60]--;
        }    
    }
    prev = index;
}
And still new entities remove (dont create new entity) Maybe I must do a timer to delay create. Idk, becouse sometimes entity is create, but not always.

Btw. In map start entity normal create. But when restart round it doesnt create, but remove old entity.

Last edited by Pawemol12; 07-31-2012 at 09:42.
Pawemol12 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 07-31-2012 , 15:00   Re: Problem with entity replace(remove entity sometims)
Reply With Quote #10

Since you tried so hard, I decided to rewrite your code a bit,
now it should be a bit easier to debug any problems.

with removing a random weapon, do you mean the weapon you tried to spawn did not get created ?
It could be Left4Dead2 itself, I think this game has some weird logic that on certain map it only allows a certain amount of weapons if I remember correctly.

PHP Code:
stock GetEntityCountByClassName(const String:className[])
{
    new 
count=0;

    new 
entity = -1;
    while ((
entity FindEntityByClassname(entityclassName)) != INVALID_ENT_REFERENCE) {
        
count++;
    }

    return 
count;
}

stock ReplaceWeaponByAnotherWeapon(const String:classNameToFind[], const String:classNameToCreate[], const String:model[], weaponIdmax)
{
    new 
x=0;

    new 
entity = -1;
    while ((
entity FindEntityByClassname(entityclassNameToFind)) != INVALID_ENT_REFERENCE) {

        if (
>= max) {
            return;
        }

        new 
Float:location[3];
        new 
Float:angles[3];

        
Entity_GetAbsOrigin(entitylocation);
        
GetEntPropVector(entityProp_Send"m_angRotation"angles);

        
// Mark the entity to kill, but this will be delayed to the next frame, so it's still valid
        
Entity_Kill(entity);

        new 
newEntity CreateEntityByName(classNameToCreate);
        
Entity_SetModel(newEntitymodel);
        
SetEntProp(newEntityProp_Send"m_weaponID"weaponId);
        
TeleportEntity(newEntitylocationanglesNULL_VECTOR);
        
DispatchKeyValue(newEntity"count""1");
        
DispatchSpawn(newEntity);

        
x++;
    }
}

yourFunction() {
    new 
countM60                GetEntityCountByClassName("weapon_rifle_m60_spawn");
    new 
countGrenadeLauncher    GetEntityCountByClassName("weapon_grenade_launcher_spawn");

    if (
countM60 countGrenadeLauncher) {
        
ReplaceWeaponByAnotherWeapon(
            
"weapon_rifle_m60_spawn",
            
"weapon_grenade_launcher_spawn",
            
"models/w_models/weapons/w_grenade_launcher.mdl",
            
GRENADE_LAUNCHER
            RoundFloat
((countM60 countGrenadeLauncher) / 2))
        );
    }
    else if (
countM60 countM60) {
        
ReplaceWeaponByAnotherWeapon(
            
"weapon_grenade_launcher_spawn",
            
"weapon_rifle_m60_spawn",
            
"models/w_models/weapons/w_m60.mdl",
            
M60,
            
RoundFloat((countGrenadeLauncher countM60) / 2))
        );
    }

__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0

Last edited by berni; 07-31-2012 at 15:01.
berni is offline
Reply



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 09:09.


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