Images with drop/collect hook
8 replies



21.01.23 12:59:32 am
When I spam the drop button it generates a lot of images (due to lag) which can't be released with the collect hook.
So is there any way to prevent it?
Sample:
So is there any way to prevent it?
Sample:
Code:
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
addhook("drop","_drop")
function _drop(id,iid,type,ammoin,ammo,mode,xpos,ypos)
local z = math.random(0,359)
if type == 1 then
Player[id].image = image('gfx/cs2dbr/skinsystem/usp/drop/'..usp[id]..'.png',0,0,0)
imagepos(Player[id].image,xpos*32+16,ypos*32+16,z)
weapon_i[iid] = Player[id].image
end
end
function _drop(id,iid,type,ammoin,ammo,mode,xpos,ypos)
local z = math.random(0,359)
if type == 1 then
Player[id].image = image('gfx/cs2dbr/skinsystem/usp/drop/'..usp[id]..'.png',0,0,0)
imagepos(Player[id].image,xpos*32+16,ypos*32+16,z)
weapon_i[iid] = Player[id].image
end
end
Code:
1
2
3
4
5
6
7
2
3
4
5
6
7
addhook("collect","_collect")
function _collect(id,iid,type,ain,a,mode)
if type == 1 then
freeimage(weapon_i[iid])
weapon_i[iid] = nil
end
end
function _collect(id,iid,type,ain,a,mode)
if type == 1 then
freeimage(weapon_i[iid])
weapon_i[iid] = nil
end
end
Admin/mod comment:
This is about scripting so I moved it to the script section /DC ChatGPT replied:
Quote:
One way to prevent the lag caused by spamming the drop button is to limit the number of images that are generated. You can do this by adding a counter variable that keeps track of the number of images currently in use, and only allowing the drop function to execute if the counter is below a certain threshold. Additionally, you can also add a delay between each drop, so the player can't spam the button too quickly.
Here's an example of how you could implement this in your code:
This code limits the number of images that can be created, and also adds a delay between each drop, so that the player can't spam the button too quickly.
Here's an example of how you could implement this in your code:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
local image_counter = 0
local max_images = 20
local drop_delay = 0.5
addhook("drop", "_drop")
function _drop(id, iid, type, ammoin, ammo, mode, xpos, ypos)
if os.clock() - Player[id].last_drop > drop_delay and image_counter < max_images then
local z = math.random(0, 359)
if type == 1 then
Player[id].image = image('gfx/cs2dbr/skinsystem/usp/drop/'..usp[id]..'.png', 0, 0, 0)
imagepos(Player[id].image, xpos*32+16, ypos*32+16, z)
weapon_i[iid] = Player[id].image
image_counter = image_counter + 1
Player[id].last_drop = os.clock()
end
end
end
addhook("collect", "_collect")
function _collect(id, iid, type, ain, a, mode)
if type == 1 then
freeimage(weapon_i[iid])
weapon_i[iid] = nil
image_counter = image_counter - 1
end
end
local max_images = 20
local drop_delay = 0.5
addhook("drop", "_drop")
function _drop(id, iid, type, ammoin, ammo, mode, xpos, ypos)
if os.clock() - Player[id].last_drop > drop_delay and image_counter < max_images then
local z = math.random(0, 359)
if type == 1 then
Player[id].image = image('gfx/cs2dbr/skinsystem/usp/drop/'..usp[id]..'.png', 0, 0, 0)
imagepos(Player[id].image, xpos*32+16, ypos*32+16, z)
weapon_i[iid] = Player[id].image
image_counter = image_counter + 1
Player[id].last_drop = os.clock()
end
end
end
addhook("collect", "_collect")
function _collect(id, iid, type, ain, a, mode)
if type == 1 then
freeimage(weapon_i[iid])
weapon_i[iid] = nil
image_counter = image_counter - 1
end
end
This code limits the number of images that can be created, and also adds a delay between each drop, so that the player can't spam the button too quickly.
Share time limited free games here

LUA ERROR: sys/lua/autorun/main.lua:507: attempt to perform arithmetic on field 'last_drop' (a nil value)
-> sys/lua/autorun/main.lua:507: in function <sys/lua/autorun/main.lua:506>
-> in Lua hook 'drop', params: 1, 1, 1, 12, 100, 0, 33, 42
The line: if os.clock() - Player[id].last_drop > drop_delay and image_counter < max_images then
@edit: fixed adding Player[id].last_drop = 0 in the join hook
os.clock() doesn't seems to work in linux os, just windows
well, anyway even testing in windows it doesn't helped. because limiting time in drop will also affect in buy hook. so if u buy a weapon while you cant drop, it will equip and not strip/drop.
-> sys/lua/autorun/main.lua:507: in function <sys/lua/autorun/main.lua:506>
-> in Lua hook 'drop', params: 1, 1, 1, 12, 100, 0, 33, 42
The line: if os.clock() - Player[id].last_drop > drop_delay and image_counter < max_images then
@edit: fixed adding Player[id].last_drop = 0 in the join hook
os.clock() doesn't seems to work in linux os, just windows
well, anyway even testing in windows it doesn't helped. because limiting time in drop will also affect in buy hook. so if u buy a weapon while you cant drop, it will equip and not strip/drop.
edited 6×, last 21.01.23 02:37:20 am
os.clock works differently between Windows and Linux. See
[LuaJIT/LuaFFI] Fix for os.clock (6)




LUA ERROR: sys/lua/autorun/main.lua:3: attempt to index global 'jit' (a nil value)
Basically what I need is (example): when you drop USP it creates a dropped USP image, and when you collect USP, the dropped image disappear. Note that usp[id] is the current skin ID of the player.
Basically what I need is (example): when you drop USP it creates a dropped USP image, and when you collect USP, the dropped image disappear. Note that usp[id] is the current skin ID of the player.
edited 1×, last 21.01.23 07:04:22 am
In that case I think your code just work(?), except the redundant
Player[id].image
which can be done as local variable instead. 

ChatGPT has written:
You're correct that os.clock() is not a reliable function to use for measuring time in Linux, as it may not have the same behavior as it does in Windows. One alternative you could use for measuring time in Lua is the socket.gettime() function, which returns the current time in seconds since the epoch (January 1, 1970).
Another solution would be to use a different approach for limiting the rate at which the drop button can be pressed. Instead of limiting the rate of drops based on time, you could use a counter to track the number of drops that have been made and only allow additional drops if the counter is less than a certain threshold.
For example, you could add a new field to the player table, drop_count, and increment it each time the player drops an item. Then, in the drop hook, you could check if drop_count is less than a certain threshold and only execute the drop code if it is.
Copy code
addhook("join","_join")
function _join(id)
Player[id].drop_count = 0
end
addhook("drop","_drop")
function _drop(id,iid,type,ammoin,ammo,mode,xpos,ypos)
if Player[id].drop_count < max_drops then
-- code to drop item
Player[id].drop_count = Player[id].drop_count + 1
else
-- code to prevent drop
end
end
This way, if a player buy a weapon while they can't drop, it will equip and drop the previous weapon.
Another solution would be to use a different approach for limiting the rate at which the drop button can be pressed. Instead of limiting the rate of drops based on time, you could use a counter to track the number of drops that have been made and only allow additional drops if the counter is less than a certain threshold.
For example, you could add a new field to the player table, drop_count, and increment it each time the player drops an item. Then, in the drop hook, you could check if drop_count is less than a certain threshold and only execute the drop code if it is.
Copy code
addhook("join","_join")
function _join(id)
Player[id].drop_count = 0
end
addhook("drop","_drop")
function _drop(id,iid,type,ammoin,ammo,mode,xpos,ypos)
if Player[id].drop_count < max_drops then
-- code to drop item
Player[id].drop_count = Player[id].drop_count + 1
else
-- code to prevent drop
end
end
This way, if a player buy a weapon while they can't drop, it will equip and drop the previous weapon.
Do those quotes conflict with the US.de rules?

Share time limited free games here


In that case I think your code just work(?), except the redundant
Player[id].image
which can be done as local variable instead.Alright, I'll change to that. And about the weapon_i[iid], is it ok to use this same variable for all other weapons or may I use different for each weapon like usp_i[iid], glock_i[iid], etc.?
I think it's ok to keep it since iid are always unique, right?
@

I made a cooldown for dropping and an exception for buying, it seems to works as intended now, ty.
edited 7×, last 21.01.23 06:52:05 pm
iid will be unique for each weapon, so an USP and Glock will never share same iid. So yes it's perfectly okay to use single table
weapon_i[iid]
. 




