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

How to switch to weapon without delays after GivePlayerItem command?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
altex
Veteran Member
Join Date: May 2009
Location: Russia
Old 10-05-2012 , 09:12   How to switch to weapon without delays after GivePlayerItem command?
Reply With Quote #1

Is there a way to instantly (without delays after previous attacks) switch the weapon been gived with GivePlayerItem and make it possible to fire?
__________________

Last edited by altex; 10-05-2012 at 09:41.
altex is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 10-05-2012 , 13:18   Re: How to switch to weapon without delays after GivePlayerItem command?
Reply With Quote #3

Try doing SetEntPropEnt(iClient, Prop_Send, "m_hActiveWeapon", iEnt) (where iEnt is the index of the weapon) to switch to it. You could then do SetEntPropFloat(iEnt, Prop_Send, "m_flNextPrimaryAttack", GetGameTime(), true) which should allow the weapon to shoot right away.
bl4nk is offline
altex
Veteran Member
Join Date: May 2009
Location: Russia
Old 10-05-2012 , 17:14   Re: How to switch to weapon without delays after GivePlayerItem command?
Reply With Quote #4

Quote:
Originally Posted by bl4nk View Post
Try doing SetEntPropEnt(iClient, Prop_Send, "m_hActiveWeapon", iEnt) (where iEnt is the index of the weapon) to switch to it. You could then do SetEntPropFloat(iEnt, Prop_Send, "m_flNextPrimaryAttack", GetGameTime(), true) which should allow the weapon to shoot right away.
Thank you for the reply.

But it does not work '(

I use the following test plugin
Code:
#pragma semicolon 1

#include <sdktools>

public Plugin:myinfo = {
    name        = "test",
    author      = "test",
    description = "test",
    version     = "test",
    url         = "test"
};

new g_index     = 0;
new g_index2    = 0;
new String:g_weapons[][] = {
    "weapon_glock",
    "weapon_p250",
    "weapon_fiveseven",
    "weapon_deagle",
    "weapon_elite",
    "weapon_hkp2000",
    "weapon_tec9",

    "weapon_nova",
    "weapon_xm1014",
    "weapon_sawedoff",

    "weapon_m249",
    "weapon_negev",
    "weapon_mag7",

    "weapon_mp7",
    "weapon_ump45",
    "weapon_p90",
    "weapon_bizon",
    "weapon_mp9",
    "weapon_mac10",

    "weapon_famas",
    "weapon_m4a1",
    "weapon_aug",
    "weapon_galilar",
    "weapon_ak47",
    "weapon_sg556",

    "weapon_ssg08",
    "weapon_awp",
    "weapon_scar20",
    "weapon_g3sg1"
};

SwitchWeapon(client) {
    if (g_index+1>= sizeof(g_weapons)) {
        g_index = 0;
    } else {
        g_index++;
    }

    for (new i = 0, ent; i < 2; i++) {
        ent = GetPlayerWeaponSlot(client, i);
        if (ent > 0) {
            RemovePlayerItem(client, ent);
            RemoveEdict(ent);
        }
    }

    new newWeapon = GivePlayerItem(client, g_weapons[g_index]);
    SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", newWeapon);
    SetEntPropFloat(newWeapon, Prop_Send, "m_flNextPrimaryAttack", GetGameTime());
}

public OnPluginStart() {
    HookEvent("weapon_fire", Event_WeaponFire);
}

public Event_WeaponFire(Handle:event, const String:name[], bool:dontBroadcast) {
    if (((++g_index2)%3) != 0) {
        return;
    }
    new client = GetClientOfUserId(GetEventInt(event, "userid"));
    if (!client) {
        return;
    }
    SwitchWeapon(client);
}
Weapons changes, but then there is a normal delay like without plugin;
__________________
altex is offline
altex
Veteran Member
Join Date: May 2009
Location: Russia
Old 10-06-2012 , 05:37   Re: How to switch to weapon without delays after GivePlayerItem command?
Reply With Quote #5

I found sourcemod plugin [CSS] Weapon Mod
I can change many properties including quickswitch, fire rates, norecoil.
It can change it on the fly, for exampe if i want to quickly switch to AK47, i can use "weaponmod ak47 quickswitch 1" command.
It works for all the weapons, and when i added csgo weapons to it then it keeps working on csgo too.
So i checked the source code and i found quick switch implementation there
Code:
	/* if quickswitch was used change from the deploy animation back to the idle one on the players viewmodel.
	also remove the attack delay from switching weapons. */
	if (ProcessArray[client][ProcessQuickSwitch] == 1)
	{
		ProcessArray[client][ProcessQuickSwitch] = 0;
		
		/* "m_flNextAttack" is whent the player can next attack, set it to next game frame
		instead of the deploy animation length */
		SetEntPropFloat(client, Prop_Send, "m_flNextAttack", GetGameTime());
		
		new ViewModel = GetEntPropEnt(client, Prop_Send, "m_hViewModel");
		
		// make sure the correct animation is used if the silencer is on.
		if (StrEqual("usp", WeaponName, false))
		{
			if (!GetEntProp(WeaponIndex, Prop_Send, "m_bSilencerOn"))
			{
				SetEntProp(ViewModel, Prop_Send, "m_nSequence", 8);
				return;
			}
		}
		else if (StrEqual("m4a1", WeaponName, false))
		{
			if (!GetEntProp(WeaponIndex, Prop_Send, "m_bSilencerOn"))
			{
				SetEntProp(ViewModel, Prop_Send, "m_nSequence", 7);
				return;
			}
		}
		
		SetEntProp(ViewModel, Prop_Send, "m_nSequence", 0);
	}
- it even fixes the model animation
- correctly work with silencers and burstfire modes
- makes quick switch when use switches weapons by himmself without GivePlayerItem command

nice plugin

so i updated test plugin and now it works
Code:
#pragma semicolon 1

#include <sdktools>
#include <sdkhooks>

public Plugin:myinfo = {
    name        = "test",
    author      = "test",
    description = "test",
    version     = "test",
    url         = "test"
};

new g_index     = 0;
new g_index2    = 0;
new String:g_weapons[][] = {
    "weapon_glock",
    "weapon_p250",
    "weapon_fiveseven",
    "weapon_deagle",
    "weapon_elite",
    "weapon_hkp2000",
    "weapon_tec9",

    "weapon_nova",
    "weapon_xm1014",
    "weapon_sawedoff",

    "weapon_m249",
    "weapon_negev",
    "weapon_mag7",

    "weapon_mp7",
    "weapon_ump45",
    "weapon_p90",
    "weapon_bizon",
    "weapon_mp9",
    "weapon_mac10",

    "weapon_famas",
    "weapon_m4a1",
    "weapon_aug",
    "weapon_galilar",
    "weapon_ak47",
    "weapon_sg556",

    "weapon_ssg08",
    "weapon_awp",
    "weapon_scar20",
    "weapon_g3sg1",

    "weapon_taser",
    "weapon_molotov",
    "weapon_hegrenade"

};

SwitchWeapon(client) {
    if (g_index+1>= sizeof(g_weapons)) {
        g_index = 0;
    } else {
        g_index++;
    }

    for (new i = 0, ent; i < 2; i++) {
        ent = GetPlayerWeaponSlot(client, i);
        if (ent > 0) {
            RemovePlayerItem(client, ent);
            RemoveEdict(ent);
        }
    }

    new weapon = GivePlayerItem(client, g_weapons[g_index]);
    FakeClientCommand(client, "use %s", g_weapons[g_index]);
    InstantSwitch(client, weapon);
}

public OnPluginStart() {
    HookEvent("player_death", Event_PlayerDeath);
}

public Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast) {
    new client = GetClientOfUserId(GetEventInt(event, "attacker"));
    if (!client) {
        return;
    }
    SwitchWeaponDelayed(client);
}

SwitchWeaponDelayed(client) {
    CreateTimer(0.1, Timer_SwitchWeapon, client);
}

public Action:Timer_SwitchWeapon(Handle:timer, any:client) {
    if (client&&IsClientInGame(client)&&IsPlayerAlive(client)) {
        SwitchWeapon(client);
    }
}

public OnMapStart() {
    for (new client = 1; client <= MaxClients; client++) { 
        if (IsClientInGame(client)) {
            SDKHook(client, SDKHook_WeaponSwitch, OnWeaponSwitch);
        } 
    }
}

public OnClientPutInServer(client) {
    SDKHook(client, SDKHook_WeaponSwitch, OnWeaponSwitch);
}

public Action:OnWeaponSwitch(client, weapon) {
    new Handle:data;
    data = CreateDataPack();
    WritePackCell(data, client);
    WritePackCell(data, weapon);

    CreateTimer(0.1, Timer_InstantSwitch, data);

    return Plugin_Continue;
}

public Action:Timer_InstantSwitch(Handle:timer, any:data) {
    ResetPack(data);
    new client = ReadPackCell(data);
    new weapon = ReadPackCell(data);
    CloseHandle(data);

    if (client&&IsClientInGame(client)&&IsPlayerAlive(client)) {
        InstantSwitch(client, weapon, 1);
    }
}

InstantSwitch(client, weapon, timer = 0) {
    new Float:GameTime = GetGameTime();

    if (!timer) {
        SetEntPropEnt(client, Prop_Send, "m_hActiveWeapon", weapon);
        SetEntPropFloat(weapon, Prop_Send, "m_flNextPrimaryAttack", GameTime);
    }

    SetEntPropFloat(client, Prop_Send, "m_flNextAttack", GameTime);
    new ViewModel = GetEntPropEnt(client, Prop_Send, "m_hViewModel");
    SetEntProp(ViewModel, Prop_Send, "m_nSequence", 0);
}
__________________

Last edited by altex; 10-06-2012 at 05:40.
altex is offline
altex
Veteran Member
Join Date: May 2009
Location: Russia
Old 05-29-2017 , 04:33   Re: How to switch to weapon without delays after GivePlayerItem command?
Reply With Quote #6

Quote:
Originally Posted by freddddd View Post
There is one issue however, when a player kills another player they will receive another random weapon.
That's not an issue, it's a feature - that how it should work.
__________________
altex 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 11:46.


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