Forum

> > CS2D > Scripts > Addhooking function in anonymious numeric table
Forums overviewCS2D overview Scripts overviewLog in to reply

English Addhooking function in anonymious numeric table

6 replies
To the start Previous 1 Next To the start

old Addhooking function in anonymious numeric table

Bowlinghead
User Off Offline

Quote
Hello,
I have some weird numeric table structures and now want to addhook functions inside my tables. (cs2d lua cmd addhook)

1. How do I make this code work?
1
addhook("attack", "myTbl[1].a.func1")
Not working duo to []-accessors in addhook cmd (see []-accessor warning in doc)




2. How do I 'addhook' a function I dont know the roots of yet? (See below)

I have n files and they all return the same table structure:
file1.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
return {
	desc="Some file n=1 description",

	["a"] = {
		func1 = function(id)
			print("Foo: "..id)
		end,
		addMe = function()
			-- ???
			addhook("attack","self:func1") -- self not declared
			-- and there is no tostring(self) 
		end,
		
	}
	["b"] = {
		func1 = function(id)
			print("Bar: "..id)
		end,
	}
}

And a fileSuper.lua that reads my file1.lua:
fileSuper.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
myFilePathStrings = {
	"sys/lua/file1.lua",
	--"file2.lua",
	-- ...
}
local currFile
local myTbl = {}

-- Load Table
for _,file in pairs(myFilePathStrings)
	currFile = loadfile(file)
	myTbl[#myTbl+1] = currFile()
end


-- Execute function (works fine)
myTbl[1]["a"].func1(5) --> Foo: 5
myTbl[1]["b"].func1(5) --> Bar: 5

-- Addhook?
myTbl[1]["a"].addMe()
addhook("attack", "myTbl[1].a.func1") -- []-accessors

Are there any easy solutions to that?

old Re: Addhooking function in anonymious numeric table

Bowlinghead
User Off Offline

Quote
Thanks. I looked through that
I think you meant main.lua in your presented file upload.
I think the magic lies between loadstring() and smart lua tableization. It looks perfectly suitable for my
Hooker.lua >


Actually rearranging the table keys to strings seems way to be the way to go and counting the amounts manually (#myTbl doesnt work here)
fileSuper.lua
1
2
3
4
5
6
7
8
9
10
11
... (thread topic code)
-- Load Table
local counter = 1
for _,file in pairs(myFilePathStrings)
     currFile = loadfile(file)
     myTbl[a..tostring(counter)] = currFile() -- make it "string"-key
	counter = counter+1
end
...

addhook("attack","myTbl.a1.a.func1")
Following the instructions found in cs2d lua cmd addhook .
edited 1×, last 05.10.23 09:53:12 pm

old Re: Addhooking function in anonymious numeric table

Mami Tomoe
User Off Offline

Quote
My original idea:
A proxy hook is the solution to both the problems.

Just make your proxy hook module override the cs2d lua cmd addhook function (or create a new one with a different name) and let it receive the function for the hook as a function and not as a string.

Like so:
1
2
3
4
5
6
7
8
function myHook(...)
	-- code
end

mt.add_hook('startround', myHook) -- Note how we send a function and not a string.

-- Original method:
addhook('startround', 'myHook')

Then you just save the function in a table and use a main proxy hook to call all of the proxied hooks within the table.
Fairly simple if you know Lua well enough.

Easier approach:
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
local tbl = {
	{
		func = function(mode)
			print('1')
		end
	},
	{
		func = function(mode)
			print('2')
		end
	},
	{
		func = function(mode)
			print('3')
		end
	}
}

function startround_hook(mode)
	for i = 1, #tbl do
		local e = tbl[i]

		if e.func and type(e.func) == 'function' then
			e.func(mode)
		end
	end
end

addhook('second', 'startround_hook')
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview