Raised This Month: $ Target: $400
 0% 

Get user weapons on death


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 07-28-2014 , 18:54   Get user weapons on death
Reply With Quote #1

Hey, guys! I'm currently trying to save somehow player's weapons when he dies. The purpose is to respawn him with the same weapons as he died (if it's even possible). So, what I tried:
- Hooking CurWeapon and looping trough get_user_weapons.
- Geting user weapons on death (probably didn't work because he is already dead...)
The problem is that I don't know how to save those weapon indexes and later give them on spawn.
Any help is appereciated.
P.S.: Took a look here, some of the information may be useful: https://forums.alliedmods.net/showthread.php?t=182970
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 07-29-2014 , 00:00   Re: Get user weapons on death
Reply With Quote #2

You'd do something like:

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <fun>

new iWeapons[33][32]

public 
plugin_init() 
{
    
RegisterHam(Ham_Killed"player""eventPlayerKilled"false)
    
RegisterHam(Ham_Spawn"player""eventPlayerSpawned"true)
}

public 
eventPlayerKilled(id)
{
    new 
iNum
    get_user_weapons
(idiWeapons[id], iNum)
}

public 
eventPlayerSpawned(id)
{
    if(!
is_user_alive(id))
    {
        return 
HAM_IGNORED
    
}
    
    new 
szWeaponName[45]
    
    for(new 
032i++)
    {
        if(
iWeapons[id][i])
        {
            
get_weaponname(iWeapons[id][i], szWeaponNamecharsmax(szWeaponName))
            
            
give_item(idszWeaponName)
        }
    }
    
    
arrayset(iWeapons[id], 032)
    
    return 
HAM_IGNORED

mottzi is offline
Send a message via MSN to mottzi
Old 07-29-2014, 05:05
Flick3rR
This message has been deleted by Flick3rR.
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 07-30-2014 , 15:13   Re: Get user weapons on death
Reply With Quote #3

Well, that works, but only sometimes...
I mean, it seems like it's not the best way to do the job, since sometimes it doesn't give anything, sometimes it gives only primaries, etc.
Here is my code for now, with the mentioned bugs:
PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <hamsandwich>
#include <fakemeta>
#include <cstrike>
#include <fun>

const PH PLUGIN_HANDLED
const PC PLUGIN_CONTINUE

const TimeToRespawnAfterChoose 5

new player_lives[33]
new 
player_weapons[33][32]
new 
player_bpammo[33][32]
new 
g_flDeadOrigin[33][3]
new 
g_iMaxPlayers

public plugin_init() {
    
RegisterHam(Ham_Killed"player""onPlayerKilled")
    
    
g_iMaxPlayers get_maxplayers()
}

public 
onPlayerKilled(victimkillershouldgib)
{
    
    
pev(victimpev_origing_flDeadOrigin[victim])
    
set_task(1.0"taskAskForRespawn"victim)
    
    new 
num
    get_user_weapons
(killerplayer_weapons[killer], num)
        
    for(new 
inumi++)
        
cs_get_user_bpammo(killerplayer_bpammo[killer][player_weapons[killer][i]])    
}

public 
taskAskForRespawn(id)
{
    if(
is_user_alive(id))
        return 
PH
    
    
new szTitle[64]
    
formatex(szTitlecharsmax(szTitle), "You have\r %i\y extra lives^nDo you want to use one?"player_lives[id])
    
    new 
menu menu_create(szTitle"handleRespawnMenu")
    
    
menu_additem(menu"Yes"""0)
    
menu_additem(menu"No"""0)
    
    
menu_display(idmenu0)
    
    return 
PC
}

public 
handleRespawnMenu(idmenuitem)
{
    if(
item == MENU_EXIT)
    {
        
menu_destroy(menu)
        return 
PH
    
}
    
    if(
is_user_alive(id))
    {
        
client_print(idprint_chat"You are already alive!")
        return 
PH
    
}
    
    switch(
item)
    {
        case 
0:
        {
            
client_print(idprint_chat"You will be respawned in %d seconds!"TimeToRespawnAfterChoose)
            
set_task(float(TimeToRespawnAfterChoose), "respawnPlayer"id)
        }
        default: return 
PH
    
}    
    
    return 
PH
}

public 
respawnPlayer(id)
{
    if(
is_user_alive(id))
        return 
PH
    
    ExecuteHamB
(Ham_CS_RoundRespawnid)
    
    
set_pev(idpev_origing_flDeadOrigin[id])
    
    new 
szWeaponName[32]
    for(new 
1<= g_iMaxPlayersi++)
    {
        if(
is_user_connected(id) && player_weapons[id][i])
        {
            
get_weaponname(player_weapons[id][i], szWeaponNamecharsmax(szWeaponName))
            
            
strip_user_weapons(id)
            
give_item(id"weapon_knife")
            
            
give_item(idszWeaponName)
            
cs_set_user_bpammo(idplayer_weapons[id][i], player_bpammo[id][i])
        }
    }
    
    return 
PC

I guess this is not the right way to set the BPAmmo, too, but it's the same with or without it. I'm currently waiting for better and improved suggestions.
Thanks!
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
NikKOo31
Senior Member
Join Date: May 2013
Location: Home
Old 07-30-2014 , 15:33   Re: Get user weapons on death
Reply With Quote #4

Just edited a few things

PHP Code:
/* Plugin generated by AMXX-Studio */ 

#include <amxmodx> 
#include <hamsandwich> 
#include <fakemeta> 
#include <cstrike> 
#include <fun> 

#define task_respawn    49839483

const PH PLUGIN_HANDLED 
const PC PLUGIN_CONTINUE 

const TimeToRespawnAfterChoose 

new player_lives[33
new 
player_weapons[33][32
new 
player_bpammo[33][32
new 
g_flDeadOrigin[33][3

public 
plugin_init() { 
    
//don't forget to register plugin
    
RegisterHam(Ham_Killed"player""onPlayerKilled"
     


public 
onPlayerKilled(victim

     
    
pev(victimpev_origing_flDeadOrigin[victim]) 
    
set_task(1.0"taskAskForRespawn"victim task_respawn
     
    new 
num 
    get_user_weapons
(victimplayer_weapons[victim], num
    
player_weapons[victim][num] = 0
    
for(new inumi++) 
        
cs_get_user_bpammo(victimplayer_bpammo[victim][player_weapons[victim][i]])     


public 
taskAskForRespawn(id

    
id -= task_respawn
    
if(is_user_alive(id)) 
        return 
PH 
     
    
new szTitle[64
    
formatex(szTitlecharsmax(szTitle), "You have\r %i\y extra lives^nDo you want to use one?"player_lives[id]) 
     
    new 
menu menu_create(szTitle"handleRespawnMenu"
     
    
menu_additem(menu"Yes"""0
    
menu_additem(menu"No"""0
     
    
menu_display(idmenu0
     
    return 
PH 


public 
handleRespawnMenu(idmenuitem

    if(
item == MENU_EXIT
    { 
        
menu_destroy(menu
        return 
PH 
    

     
    if(
is_user_alive(id)) 
    { 
        
client_print(idprint_chat"You are already alive!"
        return 
PH 
    

     
    switch(
item
    { 
        case 
0
        { 
            
client_print(idprint_chat"You will be respawned in %d seconds!"TimeToRespawnAfterChoose
            
set_task(float(TimeToRespawnAfterChoose), "respawnPlayer"id task_respawn
        } 
        default: return 
PH 
    
}     
    
    
menu_destroy(menu
    return 
PH 


public 
respawnPlayer(id

    
id -= task_respawn
    
if(is_user_alive(id)) 
        return 
PH 
     
    ExecuteHamB
(Ham_CS_RoundRespawnid
     
    
set_pev(idpev_origing_flDeadOrigin[id]) 

    
strip_user_weapons(id

    new 
szWeaponName[32] , i
    
while(player_weapons[id][i]) 
    { 
        
get_weaponname(player_weapons[id][i], szWeaponNamecharsmax(szWeaponName)) 
        
give_item(idszWeaponName
        
cs_set_user_bpammo(idplayer_weapons[id][i], player_bpammo[id][i]) 
        
player_weapons[id][i] = 0
        i
++
    } 
     
    return 
PC 

__________________
Hey ^_^
NikKOo31 is offline
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 07-30-2014 , 16:13   Re: Get user weapons on death
Reply With Quote #5

The result is the same... Sometimes gives nothing, sometimes gives only primary, even once I was freezed?! No errors in the log for now. Well, I gues I will search for an alternative.
The question is currently staying.
Thanks!
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 07-30-2014 , 16:53   Re: Get user weapons on death
Reply With Quote #6

Did you debug the array holding the weapon ids in the kill-event and in the respawn function?

Your for loop is wrong ( for(new i = 1; i <= g_iMaxPlayers; i++) ), have another look at my example. Stripping the players weapon is enough, you dont need to give him a knife, as tat would give him the knife twice (because it's already included in the weapons-array), then, you are not destroying the menus in the handler sometimes.

Edit: Btw, you are saving the killers weapons and not the victims...
Edit2: Both of you guys do the bpammo thing wrong; take this as a base and add your origin stuff:
PHP Code:
#include <amxmodx> 
#include <amxmisc> 
#include <hamsandwich> 
#include <fun> 
#include <cstrike> 

new iWeapons[33][32
new 
iWeaponBackPack[33][32]
new 
iDeadOrigin[33][3]

public 
plugin_init()  

    
RegisterHam(Ham_Killed"player""eventPlayerKilled"false


public 
eventPlayerKilled(id

    new 
iNum 
    get_user_weapons
(idiWeapons[id], iNum
    
    for(new 
0iNumi++)
    {
        if(
iWeapons[id][i]) 
        {
            if(
iWeapons[id][i] != CSW_KNIFE)
            {
                
iWeaponBackPack[id][i] = cs_get_user_bpammo(idiWeapons[id][i])  
            }
        }
        else
        {
            break
        }
    }
    
    
get_user_origin(idiDeadOrigin[id])
    
    new 
menu menu_create("Respawn ?""menuRespawnHandler")
    
    
menu_additem(menu"Yes")
    
menu_additem(menu"No")
    
    
menu_display(idmenu)


public 
menuRespawnHandler(idmenuitem

    if(!
is_user_connected(id) || is_user_alive(id))
    {
        
menu_destroy(menu)
        return 
PLUGIN_HANDLED    
    
}
    
    if(
item == 0)
    {
        
ExecuteHamB(Ham_CS_RoundRespawnid
        
set_user_origin(idiDeadOrigin[id])
        
        new 
szWeaponName[45
        
        for(new 
032i++) 
        { 
            if(
iWeapons[id][i]) 
            { 
                if(
iWeapons[id][i] == CSW_KNIFE)
                {
                    
give_item(id"weapon_knife"
                }
                else
                {
                    
get_weaponname(iWeapons[id][i], szWeaponNamecharsmax(szWeaponName)) 
                    
                    
give_item(idszWeaponName
                    
cs_set_user_bpammo(idiWeapons[id][i], iWeaponBackPack[id][i]) 
                }
            } 
            else
            {
                break
            }
        } 
    }
    
    
arrayset(iWeapons[id], 032
    
arrayset(iWeaponBackPack[id], 032
    
    
menu_destroy(menu)
    return 
PLUGIN_HANDLED

Note: You could avoid useless loop cycles using break when iWeapons[id][i] == 0...

Edit3: Added the origin stuff and testet, should work o.k.

Last edited by mottzi; 07-30-2014 at 17:43.
mottzi is offline
Send a message via MSN to mottzi
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 07-30-2014 , 18:33   Re: Get user weapons on death
Reply With Quote #7

Lol, mottzi, thanks for the note. Really dumb - getting killer's weapons. Now it's all okay, thank you all guys!
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 07-30-2014 , 18:43   Re: Get user weapons on death
Reply With Quote #8

hook curWeapon event, and save the weapons into global var.

after, use csx to hook the death and check the global var...

__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 07-30-2014 , 19:01   Re: Get user weapons on death
Reply With Quote #9

I think it's not needed to hook that event, since I can get the weapons once on user's death and store them into the variable. The great example by mottzi is working (made little changes for the code). I think the case is closed! Thanks to all!
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 07-30-2014 , 20:28   Re: Get user weapons on death
Reply With Quote #10

Quote:
Originally Posted by ^SmileY View Post
hook curWeapon event, and save the weapons into global var.

after, use csx to hook the death and check the global var...

using CurWeapon would probably be the poorest way to go...
mottzi is offline
Send a message via MSN to mottzi
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 10:02.


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