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

[CS:S] Parent Entity or Sprite to Weapon


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
abgar
Senior Member
Join Date: Apr 2015
Old 05-27-2016 , 21:41   [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #1

Hi guys,

I found this piece of code for attaching a sprite to a player.

My question is, can i parent this to the actual weapon instead? And if so, how do i do this? (Note - the weapon will always be a knife).

Cheers

PHP Code:
stock AttachSprite(clientString:sprite[])
{
    if(!
IsPlayerAlive(client)) return -1;
    
decl String:iTarget[16];
    
Format(iTarget16"client%d"client);
    
DispatchKeyValue(client"targetname"iTarget);
    
decl Float:Origin[3];
    
GetClientEyePosition(client,Origin);
    
Origin[2] += 25.0;
    new 
Ent CreateEntityByName("env_sprite");
    if(!
Ent) return -1;
    
DispatchKeyValue(Ent"model"sprite);
    
DispatchKeyValue(Ent"classname""env_sprite");
    
DispatchKeyValue(Ent"spawnflags""1");
    
DispatchKeyValue(Ent"scale""0.1");
    
DispatchKeyValue(Ent"rendermode""1");
    
DispatchKeyValue(Ent"rendercolor""255 255 255");
    
DispatchSpawn(Ent);
    
TeleportEntity(EntOriginNULL_VECTORNULL_VECTOR);
    
SetVariantString(iTarget);
    
AcceptEntityInput(Ent"SetParent"EntEnt0);
    return 
Ent;

abgar is offline
sdz
Senior Member
Join Date: Feb 2012
Old 05-27-2016 , 22:53   Re: [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #2

Get the edict* index of the weapon.

You'll also want to edit that stock and change references from clients to edicts, since well it should've been done that way from the get-go. Clients ARE edicts.

Last edited by sdz; 05-27-2016 at 22:57.
sdz is offline
abgar
Senior Member
Join Date: Apr 2015
Old 05-28-2016 , 02:02   Re: [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #3

Thanks for that.

I've never really dealt with edicts, entities etc - so just trying to figure out my way through this.

After a bit of trial and error, and searching the API, I've tried this:
PHP Code:
new weaponent Client_GetActiveWeapon(client);
    
decl String:weaponedict[32];
    
GetEdictClassname(weaponent,weaponedictsizeof(weaponedict)); 
So, the total code i've used looks this, which compiles without errors / warnings.

PHP Code:
new weaponent Client_GetActiveWeapon(client);
    
decl String:weaponedict[32];
    
GetEdictClassname(weaponent,weaponedictsizeof(weaponedict));

    new 
Ent CreateEntityByName("env_sprite");
    if(!
Ent) return -1;
        
    
DispatchKeyValue(Ent"targetname"weaponedict);
    
DispatchKeyValue(Ent"model"sprite);
    
DispatchKeyValue(Ent"classname""env_sprite");
    
DispatchKeyValue(Ent"spawnflags""1");
    
DispatchKeyValue(Ent"scale""0.1");
    
DispatchKeyValue(Ent"rendermode""1");
    
DispatchKeyValue(Ent"rendercolor""255 255 255");
    
DispatchSpawn(Ent);
    
SetVariantString(weaponedict);
    
AcceptEntityInput(Ent"SetParent"EntEnt0);
    return 
Ent
However nothing actually shows once this is called - so im assuming im doing something incorrectly?

Also, not quite sure what you mean by this sorry? How would i adapt to suit?

Quote:
You'll also want to edit that stock and change references from clients to edicts, since well it should've been done that way from the get-go. Clients ARE edicts.

Last edited by abgar; 05-28-2016 at 02:04.
abgar is offline
sdz
Senior Member
Join Date: Feb 2012
Old 05-28-2016 , 11:56   Re: [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #4

Quote:
Originally Posted by abgar View Post
Thanks for that.
snip
Also, not quite sure what you mean by this sorry? How would i adapt to suit?
As you may know, Edicts are networked Entities. Essentially, there can be 4096(?) Entities, where there can be 2048 Edicts until the server crashes. So essentially server entities number 1 to MaxClients are reserved for players themselves. Entity index 0 is worldspawn, AKA the map for the most part. https://developer.valvesoftware.com/wiki/Entity_limit

This is why we loop from 1 to MaxClients when we want to scan for all active players like so:
This bit of information is also in the "Clients" include file, or located in the API.
Code:
MAXPLAYERS is not the same as MaxClients. MAXPLAYERS is a hardcoded value as an upper limit.  MaxClients changes based on the server. Both GetMaxClients() and MaxClients are only available once the map is loaded, and should not be used in OnPluginStart().                     
#define MAXPLAYERS        65   /**< Maximum number of players SourceMod supports */ 
#define MAX_NAME_LENGTH 32    /**< Maximum buffer required to store a client name */ 
public const MaxClients;    /**< Maximum number of players the server supports (dynamic) */
PHP Code:
for(new 1MaxClientsi++)
{
     if(
IsClientConnected(i) && IsClientInGame(i))
     {
          
//Do stuff
     
}

You'll need to activate the entity. It's kinda weird, and using valve hammer while working with entities is the best way to go sometimes.
Code:
AcceptEntityInput(sprite, "ToggleSprite");
AcceptEntityInput(sprite, "ShowSprite");
To give you an example of how in SM you need to turn on the entities, here's a block from my plugin ShootFX, I've also commented some stuff to make it a little easier to understand
PHP Code:
public createFire(Float:Origin[3], ownerIndex)
{
    new 
fire CreateEntityByName("env_fire");
    
DispatchKeyValue(fire"damagescale""0.0");
    
DispatchKeyValue(fire"firesize""64");
    
DispatchKeyValue(fire"firetype""0");
    
DispatchKeyValue(fire"fireattack""2");
    
DispatchKeyValue(fire"health""15");
    
DispatchKeyValue(fire"ignitionpoint""1");
    
DispatchSpawn(fire);
    
TeleportEntity(fireendNULL_VECTORNULL_VECTOR);
    
AcceptEntityInput(fire"Enable"); //Enable it since by default it's disabled
    
AcceptEntityInput(fire"StartFire"); //Turn that bitch on
    
SetEntPropEnt(fireProp_Send"m_hOwnerEntity"ownerIndex); //Certain games will make this the killer if it kills a player or entity

Feel free to use that if you ever need it.

Last edited by sdz; 05-28-2016 at 12:06.
sdz is offline
abgar
Senior Member
Join Date: Apr 2015
Old 05-30-2016 , 01:52   Re: [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #5

Thanks for that information, however unfortunately im still not sure how to attach the sprite to the weapon, instead of the client. (or an enity to a weapon, as i could use either a sprite or an entity).

The original code i posted definitely does work without having to turn on the sprite, or activate it - The SpawnFlags, "1" for env_sprite means start activated.

It's just the parenting that sprite to the client's weapon, instead of the client itself. Are you able to provide an example of how i can do that?

Thanks so much again
abgar is offline
sdz
Senior Member
Join Date: Feb 2012
Old 05-30-2016 , 13:07   Re: [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #6

Quote:
Originally Posted by abgar View Post
Thanks for that information, however unfortunately im still not sure how to attach the sprite to the weapon, instead of the client. (or an enity to a weapon, as i could use either a sprite or an entity).

The original code i posted definitely does work without having to turn on the sprite, or activate it - The SpawnFlags, "1" for env_sprite means start activated.

It's just the parenting that sprite to the client's weapon, instead of the client itself. Are you able to provide an example of how i can do that?

Thanks so much again

Well you'd need to get the entity index of the client's weapon, then hook OnWeaponSwitch.
There's a couple routes you could take with this:
Then once you have the weapon index, you just do the above except replacing client with the weaponindex.
sdz is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 05-30-2016 , 13:12   Re: [CS:S] Parent Entity or Sprite to Weapon
Reply With Quote #7

Quote:
Originally Posted by EasSidezz View Post
Just a note on this, I never actually added an easy way to parent stuff to the weapon, just an easy way to get the weapon shoot position, however if you look at the code I parent an info_target to the weapon to get that position so you can easily rip that out and use it.
Mitchell 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 01:15.


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