1.Setting
pev_nextthink on
Spawn() is not reliable. Due to this, your plugin is working only if a player dropped a weapon by
drop command, but fails when he is killed and game drop the weapon.
Why? Usually, when a
weaponbox is created game sets the think to
CWeaponBox::Kill and
nextthink to
gametime() + 120 in HL and to
gametime() + 300 in CS. This happens AFTER spawn is called, so it overrides what you set in
fw_WeaponBoxSpawn.
But, when you manually drop the weapon the piece of code I'm talking about is missing(probably wanted?), so you are lucky and it somehow works.
To understand better, look at HL code:
1.
https://github.com/ValveSoftware/hal...ayer.cpp#L4566 this happens when you drop manually the weapon. See, nextthink is not changed.
2.
https://github.com/ValveSoftware/hal...layer.cpp#L780 this is executed when you die and game drop the active item. Here nextthink is changed.
What you should do to make it work in both cases is to hook
Touch instead of
Spawn()
PHP Code:
register_touch("worldspawn", "weaponbox", "CWeaponBox_Touch")
In
CWeaponBox_Touch alter
pev_nextthink.
If you still don't get it, here is the order of calls.
When you drop a weapon:
1."drop" command is send.
2.
DropPlayerItem() is called.
3.
weaponbox entity is created.
4.
Spawn is called.
5.
SetModel() set the entity model to
models/w_weaponbox.mdl
6.
Touch() is called -> here you should set the new weaponbox life.
When game drop a weapon:
1.
PackDeadPlayerItems is called.
2.
weaponbox entity is created.
3.
Spawn is called.
4.
SetModel() set the entity model to
models/w_weaponbox.mdl
5.
Think is set to
CWeaponBox::Kill, so it will later remove the entity.
6.
pev_nextthink is set to gametime() + 120
7.
Touch() is called -> here you should set the new weaponbox life.
If you do it like that then it will work also in
CS and probably other mods.
Short story: Set nextthink in
Touch, not
Spawn so game won't override your value.
2.In
fw_WeaponBoxThink use
charsmax() to retrieve a string size.
3.In
fw_FadeOut also check if entity is valid. Some other plugins may remove it while the task is running.
You could retrieve
pev_renderamt inside
fw_FadeOut, instead of passing it in
set_task(so you will pass only entity index) and you won't need to convert the value to integer anymore. Anyway, your way is ok too I guess.
4.Don't remove
weaponbox entities like you do. That way you only remove the box and not the weapon that is inside it.
The correct way to remove it is to call think on the entity. Since your superceded Think(), you could set a custom value in a unused field, like
pev_iuser3. In
fw_WeaponBoxThink, check pev_iuser3. If it holds your custom value, then do nothing. Else, set the properties(as you already do) and supercede.
Something like:
Spoiler
PHP Code:
public fw_FadeOut(data[2]){
if(amt>0.0){
}
else
{
set_pev(ent, pev_iuser3, 123456)
call_think(ent)
}
}
PHP Code:
public fw_WeaponBoxThink(ent)
{
if(!pev_valid(ent))
return HAM_IGNORED
if(pev(ent, pev_iuser3) != 123456)
{
//your current code
}
}
__________________