English How to disable barricade upgrade using script?

6 replies
Goto Page
To the start Previous 1 Next To the start
Up
SkullFace
User
Offline Off
I've been fiddling around with objectupgrade hook to stop any attempt to upgrade a barricade building.

This was my attempt.
Code:
1
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


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.
IMG:https://www.cs2d.com/img/ref_dynamicobjects.png
IMG:https://i.imgur.com/kZBVkjM.gif
11.11.23 11:58:52 am
Up
Hajt
User
Offline Off
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
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")
11.11.23 07:51:14 pm
Up
Mami Tomoe
User
Offline Off
user Hajt has written:
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
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")


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 cs2d lua hook startround hook to follow up every round start to remove all entries from the table.
Additionally, you would need a hook for when the barricades are destroyed.
It's hard being the best girl in the whole entire world
12.11.23 07:59:24 am
Up
Cure Pikachu
User
Offline Off
And may I ask what is the issue of just simply doing this considering the cs2d lua hook objectupgrade hook has the object ID as parameter you can leverage using cs2d lua cmd object (except for performance reasons due to the function calls) ?
Code:
1
2
3
4
5
6
addhook("objectupgrade","stopupgrade")
function stopupgrade(oid)
     if object(oid,"type") == 1 then
          return 1
     end
end

user 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 user Hajt provided plus adding the things user Mami Tomoe mentioned:
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
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

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
IMG:https://i.imgur.com/DeSeUxC.png
IMG:https://i.imgur.com/xpsyQRX.png
12.11.23 10:01:37 am
Up
DC
Admin
Offline Off
Caching isn't needed here. It's just a single Lua API call (cs2d lua cmd object) that you would save - in a hook which isn't even called each frame. I would go for the simple solution without caching.

Additional Reasoning >
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
12.11.23 12:40:38 pm
Up
SkullFace
User
Offline Off
I've must have missed this by adding
else

Thanks to everyone!
IMG:https://i.imgur.com/kZBVkjM.gif
12.11.23 01:56:22 pm
Up
Cure Pikachu
User
Offline Off
@user SkullFace: More like you forgot to parse the object ID parameter through the cs2d lua cmd 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.
IMG:https://i.imgur.com/DeSeUxC.png
IMG:https://i.imgur.com/xpsyQRX.png
To the start Previous 1 Next To the start