How to disable barricade upgrade using script?
6 replies



11.11.23 11:45:24 am
I've been fiddling around with objectupgrade hook to stop any attempt to upgrade a barricade building.
This was my attempt.
I'm scratching my head in confusion. I tried replacing return 1 and 0, it does work but for all. My goal was to find barricade ID and use it that way.
This was my attempt.
Code:
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
--NO UPGRADE
addhook("objectupgrade","no_upgrade")
function no_upgrade(id,player,progress,total)
if id == 1 then
return 1
else
return 0
end
end
addhook("objectupgrade","no_upgrade")
function no_upgrade(id,player,progress,total)
if id == 1 then
return 1
else
return 0
end
end
I'm scratching my head in confusion. I tried replacing return 1 and 0, it does work but for all. My goal was to find barricade ID and use it that way.


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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
local barricades = {}
function in_array(arr, value)
for _, v in ipairs(arr) do
if v == value then
return true
end
end
return false
end
function build_hook(id, type, x, y, mode, objectid)
if type == 1 then
table.insert(barricades, objectid)
end
end
function objectupgrade_hook(id, player, progress, total)
if in_array(barricades, id) then
return 1
end
end
addhook("build", "build_hook")
addhook("objectupgrade", "objectupgrade_hook")
function in_array(arr, value)
for _, v in ipairs(arr) do
if v == value then
return true
end
end
return false
end
function build_hook(id, type, x, y, mode, objectid)
if type == 1 then
table.insert(barricades, objectid)
end
end
function objectupgrade_hook(id, player, progress, total)
if in_array(barricades, id) then
return 1
end
end
addhook("build", "build_hook")
addhook("objectupgrade", "objectupgrade_hook")

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
local barricades = {}
function in_array(arr, value)
for _, v in ipairs(arr) do
if v == value then
return true
end
end
return false
end
function build_hook(id, type, x, y, mode, objectid)
if type == 1 then
table.insert(barricades, objectid)
end
end
function objectupgrade_hook(id, player, progress, total)
if in_array(barricades, id) then
return 1
end
end
addhook("build", "build_hook")
addhook("objectupgrade", "objectupgrade_hook")
function in_array(arr, value)
for _, v in ipairs(arr) do
if v == value then
return true
end
end
return false
end
function build_hook(id, type, x, y, mode, objectid)
if type == 1 then
table.insert(barricades, objectid)
end
end
function objectupgrade_hook(id, player, progress, total)
if in_array(barricades, id) then
return 1
end
end
addhook("build", "build_hook")
addhook("objectupgrade", "objectupgrade_hook")
Although smart, this would fail if the barricade was spawned by the map (with same team property as the player).
Furthermore, this adds the complication of needing another hook such as the

Additionally, you would need a hook for when the barricades are destroyed.
It's hard being the best girl in the whole entire world
And may I ask what is the issue of just simply doing this considering the
objectupgrade hook has the object ID as parameter you can leverage using
object (except for performance reasons due to the function calls) ?
SkullFace 's code is literally prevent the upgrading of the object that has the internal object ID of 1.
If we want to factor in caching, best I could come up with for now. It's basically what
Hajt provided plus adding the things
Mami Tomoe mentioned:
Could argue this is more of a performance hit due to the 3 iterations being done especially during the upgrade attempt.


Code:
1
2
3
4
5
6
2
3
4
5
6
addhook("objectupgrade","stopupgrade")
function stopupgrade(oid)
if object(oid,"type") == 1 then
return 1
end
end
function stopupgrade(oid)
if object(oid,"type") == 1 then
return 1
end
end

If we want to factor in caching, best I could come up with for now. It's basically what


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
27
28
29
30
31
32
33
34
35
36
37
38
39
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
27
28
29
30
31
32
33
34
35
36
37
38
39
barricades = {}
addhook("startround","initbars")
function initbars()
-- Reset table
barricades = {}
-- Then repopulate with ones spawned by the map
for _, o in pairs(object(0,"table")) do
if object(o,"type") == 1 then
table.insert(barricades,o)
end
end
end
addhook("build,"addtobars")
function addtobars(pid,btype,x,y,mode,oid)
if btype == 1 then
table.insert(barricades,oid)
end
end
addhook("objectkill","removefrombars")
function removefrombars(oid)
for a, b in ipairs(barricades) do
if b == oid then
table.remove(barricades,a)
break
end
end
end
addhook("objectupgrade","stopupgrade")
function stopupgrade(oid)
for _, o in ipairs(barricades) do
if o == oid then
return 1
end
end
end
addhook("startround","initbars")
function initbars()
-- Reset table
barricades = {}
-- Then repopulate with ones spawned by the map
for _, o in pairs(object(0,"table")) do
if object(o,"type") == 1 then
table.insert(barricades,o)
end
end
end
addhook("build,"addtobars")
function addtobars(pid,btype,x,y,mode,oid)
if btype == 1 then
table.insert(barricades,oid)
end
end
addhook("objectkill","removefrombars")
function removefrombars(oid)
for a, b in ipairs(barricades) do
if b == oid then
table.remove(barricades,a)
break
end
end
end
addhook("objectupgrade","stopupgrade")
function stopupgrade(oid)
for _, o in ipairs(barricades) do
if o == oid then
return 1
end
end
end
Could argue this is more of a performance hit due to the 3 iterations being done especially during the upgrade attempt.
edited 2×, last 12.11.23 01:48:31 pm


Caching isn't needed here. It's just a single Lua API call (
object) that you would save - in a hook which isn't even called each frame. I would go for the simple solution without caching.

@
SkullFace: More like you forgot to parse the object ID parameter through the
object function call so all you are really doing is just return 1 (stop upgrade) if the ID of the building you are attempting to upgrade has the value of 1 as stated. Also, most CS2D hook functions return 0 at the end by default.







