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

Clone Entity


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 01-31-2020 , 18:09   Clone Entity
Reply With Quote #1

I would like to know if it's possible/how to clone an entity through SourceMod keeping all its previous properties and KeyValues.

Since it isn't possible to change the classname after an entity is created I need to copy an existing one, but creating it as another classname.
__________________
Marttt is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 02-05-2020 , 06:12   Re: Clone Entity
Reply With Quote #2

Dunno, how may it help you. For test reasons I reproduced props of one target to another target using tEntDev + such script:

PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>
#include <sdktools>

public void OnPluginStart()
{
    
RegAdminCmd    ("sm_reproduce",     Cmd_ReproduceProp,        ADMFLAG_ROOT,    "Clone properties on aim target based on the properties previously saved by sm_ted_select of tEntDev");
}

public 
Action Cmd_ReproduceProp(int clientint args)
{
    
int ent GetClientAimTarget(clientfalse);
    
    if (
ent 0) { // || !IsTank(ent)) {
        
PrintToChat(client"Not aimed or not a tank! Ent = %i"ent);
        return 
Plugin_Handled;
    }

    
PrintToChat(client"Reproducing properties on %i ..."ent);

    
KeyValues kv;
    
char sItem[16], sName[64], sValue[64];
    
int iValue;
    
float fValuevValue[3];
    
    
kv CreateKeyValues("tank");
    
    if (
FileToKeyValues(kv"kvtest.txt")) { // tEntDev report file (root of game folder)
        
PrintToChat(client"kvtest.txt is Loaded");
        
        
kv.Rewind();
        
kv.GotoFirstSubKey();
        
        do
        {
            
kv.GetSectionName(sItemsizeof(sItem)); // compare to full list
            
            
kv.GetString("Name"sNamesizeof(sName));
            
            if (
HasEntProp(entProp_SendsName)) {
            
                
PrintToConsole(client"Name: %s"sName);
                
                switch(
kv.GetNum("type")) {
                    case 
0: { // integer
                        
iValue kv.GetNum("value");
                        
SetEntProp(entProp_SendsNameiValue);
                        
PrintToConsole(client"%s = %i"sNameiValue);
                    }
                    case 
1: { // float
                        
fValue kv.GetFloat("value");
                        
SetEntPropFloat(entProp_SendsNamefValue);
                        
PrintToConsole(client"%s = %f"sNamefValue);
                    }
                    case 
2: { // vector
                        
kv.GetVector("value"vValue);
                        
SetEntPropVector(entProp_SendsNamevValue);
                        
PrintToConsole(client"%s = %f %f %f"sNamevValue[0], vValue[1], vValue[2]);
                    }
                    case 
3: { // ??
                    
}
                    case 
4: { // string
                        
kv.GetString("value"sValuesizeof(sValue), "error");
                        if (!
StrEqual(sValue"error")) {
                            
SetEntPropString(entProp_SendsNamesValue);
                            
PrintToConsole(client"%s = %s"sNamesValue);
                        }
                    }
                }
            }
        } while (
kv.GotoNextKey());
        
        
ChangeEdictState(ent0);
    }
    else {
        
PrintToChat(client"kvtest.txt file is not found!");
    }
    
PrintToChat(client"Finished");
    return 
Plugin_Handled;

sm_ted_select (on first aim target) + sm_reproduce (on second aim target)
__________________
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 02-06-2020 , 05:25   Re: Clone Entity
Reply With Quote #3

I will try later and give you some feedback, thanks Dragokas.
__________________
Marttt is offline
kadet.89
Veteran Member
Join Date: Nov 2012
Location: Serbia
Old 02-06-2020 , 05:34   Re: Clone Entity
Reply With Quote #4

It is possible to change entity classname. At least it works in my extensions via IServerTools::SetKeyValue before entity is spawned.
you can try this:
DispatchKeyValue(entity, "classname", "prop_something");
And I wouldn't recommend to do it for "weapon_*" entities, changing classnames for weapon entities leads to strange behavior in CS:S and probably in all other source games.

Last edited by kadet.89; 02-06-2020 at 05:34.
kadet.89 is offline
Send a message via Skype™ to kadet.89
Shadowysn
Senior Member
Join Date: Sep 2015
Location: Location:
Old 03-12-2020 , 07:39   Re: Clone Entity
Reply With Quote #5

Quote:
Originally Posted by kadet.89 View Post
It is possible to change entity classname. At least it works in my extensions via IServerTools::SetKeyValue before entity is spawned.
you can try this:
DispatchKeyValue(entity, "classname", "prop_something");
And I wouldn't recommend to do it for "weapon_*" entities, changing classnames for weapon entities leads to strange behavior in CS:S and probably in all other source games.
I think you should still be careful when changing classnames like that in any entity.

When I used this method to change the classnames of weapon_first_aid_kit_spawn's I created to just weapon_first_aid_kit in L4D2 to avoid crashing the game, it still eventually crashed due to no free edicts.
I think it was to do with garbage edicts being created from changing classnames, but I dunno, it's probably best to just leave the classname alone.
__________________
ragdoll spam, that is all

Steam profile, where I game, obviously.
Youtube channel, where I do Stick Death Maze animations and some other stuff.
no plugin requests, sorry


My Plugins:
-search list-
Modified Plugins:
1 | 2 | 3 | 4
Shadowysn is offline
kadet.89
Veteran Member
Join Date: Nov 2012
Location: Serbia
Old 03-12-2020 , 08:11   Re: Clone Entity
Reply With Quote #6

I've been using it a lot with prop_* and npc_* entities in CS:S and now use in CS:GO in CEntity framework, it's absolutely stable. But for weapon_* some tricks/corrections are required to avoid crashes. In CS:GO a weapon class id should be set to the correct one and a prefab for the new classname should be added. In CS:S map parser function should be hooked to replace the classname to the original one and then back in a postspawn hook and all animations are to be started "manually"
kadet.89 is offline
Send a message via Skype™ to kadet.89
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 03-14-2020 , 02:01   Re: Clone Entity
Reply With Quote #7

Which entity are you trying to clone? If it's simply weapons or something else simple there are not many variables you would need to duplicate (position, model, maybe a few other properties depending on what you're doing).

Changing the classname in OnEntityCreated might crash because the entity has not yet spawned or been initialized with variables so the game might apply some which don't exist on the new classname. I think it would error out instead, I don't see how it could produce a no free edicts error. Would be good to test and verify all entities spawned with the old/new classname and see if it's really leaking, I personally would like to know the result.
__________________
Silvers is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 03-14-2020 , 08:49   Re: Clone Entity
Reply With Quote #8

Hi Silvers, I'm trying to clone prop_dynamic/physics into a prop_car_alarm.

I read somewhere that if you change the classname of an entity, some events may not work properly because the entity was already spawned.

For example: if I change an entity to prop_car_alarm, the OnCarAlarmStart output doesn't fires.

At the moment the only way that I could achieve the firing events was by manually spawn a prop_car_alarm entity.

Maybe I'm doing something wrong, manually it works, but since I create all by hand some properties are lost this includes the attached events.

So I wanted to "clone" to get all properties from the previous entity and spawn it again.
__________________

Last edited by Marttt; 03-14-2020 at 09:01.
Marttt 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 02:31.


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