Forum

> > CS2D > Scripts > How to disable barricade upgrade using script?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to disable barricade upgrade using script?

6 replies
To the start Previous 1 Next To the start

old How to disable barricade upgrade using script?

Casper-
User Off Offline

Quote
I've been fiddling around with objectupgrade hook to stop any attempt to upgrade a barricade building.

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

old Re: How to disable barricade upgrade using script?

Hajt
User Off Offline

Quote
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")

old Re: How to disable barricade upgrade using script?

Mami Tomoe
User Off Offline

Quote
user Hajt has written
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.

old Re: How to disable barricade upgrade using script?

Cure Pikachu
User Off Offline

Quote
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) ?
1
2
3
4
5
6
addhook("objectupgrade","stopupgrade")
function stopupgrade(oid)
	if object(oid,"type") == 1 then
		return 1
	end
end
user Casper- '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:
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 3×, last 08.06.24 07:19:01 pm

old Re: How to disable barricade upgrade using script?

DC
Admin Off Offline

Quote
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 >

old Re: How to disable barricade upgrade using script?

Cure Pikachu
User Off Offline

Quote
@user Casper-: 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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview