Forum

> > CS2D > Scripts > loadstring error
Forums overviewCS2D overview Scripts overviewLog in to reply

English loadstring error

22 replies
Page
To the start Previous 1 2 Next To the start

old loadstring error

Gajos
BANNED Off Offline

Quote
I don't know how to add more arguments to loadstring! (line: 14)
I have an errors.
Lua compiler: here
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
_load = {}

function addhook(name,func)
	if name == 'load' then
		table.insert(_load,func)
	end
end

function inithook(name,...)
	t = {...}
	if name == 'load' then
		if _load[1] then
			for _, i in pairs(_load) do
				local f = loadstring(i..'('..table.concat(t,',')..')')
				f()
			end
		end
	end
end

addhook('load','onload')
function onload(id,l)
	print(id..': '..l)
end

function start()
	local id = 1
	inithook('load',id,'lol')
end

start()

old Re: loadstring error

DannyDeth
User Off Offline

Quote
What are you trying to accomplish with this script? I had similiar troubles with loadstring before, but never figured out what was wrong. If I knew what you are trying to do, I might be able to find an alternative method for solving your problem.

EDIT: If you print the table.concat of t, you will see "1,lol". Loadstring will try to pass the variable lol, not the string 'lol' to the function. This is where you are getting the nil value stuff.

I rewrote the code to convert every argument to a string value before passing it to the function, if you use this code you must tonumber any variables that need to have arithmetic performed on them.
Code >
edited 1×, last 27.12.13 12:50:13 am

old Re: loadstring error

DannyDeth
User Off Offline

Quote
I'm not thick, dude, I was asking what the script is supposed to do, not how it works. I can explain in detail how an IRC server works, but if I did not tell you it was an IRC server you wouldn't know what the hell I was talking about, would you?

old Re: loadstring error

Flacko
User Off Offline

Quote
Why don't you tell us what the error is.
Also, why don't you just do sth like this
1
_G[i](...)
using loadstring is bad

old Re: loadstring error

Avo
User Off Offline

Quote
Don´t ask for help then. I can´t see any sensible reason why we don´t have to know this. Why aye man?

old Re: loadstring error

Avo
User Off Offline

Quote
>my
>us

pls

---

Don't waste our time. If you want to add a possibility to run lua code in-game(sounds like a server hack), you should to get rid of this on your own. I would help you if you gave here information about your aim.

old Re: loadstring error

Gajos
BANNED Off Offline

Quote
lol
I'm want use this for eg. Visual Lua like:
lib.addhook('buttonhover','wtf')

old Re: loadstring error

DannyDeth
User Off Offline

Quote
Okay, time for a little lesson in the architecture of Lua then.

In Lua, *everything* is a table. The global table contains all variables & functions declared in a non-local scope. So these code examples are identical to Lua:
1
myFunction(1,2,3)
1
_G["myFunction"](1,2,3)

So, naturally, if you know the name of the function that needs to be executed ( which is of course stored in the table _load in your code ), you can execute the function using the global table. The arguments can be expanded from a table using unpack(), so your inithook code would be something like this:

1
2
3
4
5
6
7
8
9
10
function inithook(name,...)
     t = {...}
     if name == 'load' -- btw, why is this here?
          if _load[1] then -- and this?
               for _, i in pairs(_load) do
                    _G[i](unpack(t))
               end
          end
     end
end

NOTE: I didn't test this.

old Re: loadstring error

Gajos
BANNED Off Offline

Quote
I don't know how I can make this like you for eg. 2 hooks.
Create hook load and save for functions onload() and onsave()

old Re: loadstring error

Gajos
BANNED Off Offline

Quote
Thanks all. Pls help:

error line: 15
input:15: attempt to index field '?' (a nil value)
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
hookG = {}

function addhook(name,func)
	local t = {
		name = name;
		func = func;
	}
	table.insert(hookG,t)
end

function inithook(name,...)
	local t = {...}
	for _, i in pairs(hookG) do
		if i.name == name then
			_G[_].func(unpack(t))
		end
	end
end

-- test
addhook('load','onload')
function onload(id,l)
	print(id..': '..l)
end

function start()
	local id = 1
	inithook('load',id,'lol')
end

start()
edited 3×, last 28.12.13 07:27:56 am

old Re: loadstring error

Flacko
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
local hookt = {} --hook table

function addhook(hookname, funcname)
	hookt[hookname] = hookt[hookname] or {}
	table.insert(hookt[hookname], funcname)
end

function callhook(hookname, ...)
	if hookt[hookname] then
		for k, funcname in pairs(hookt[hookname]) do
			_G[funcname](...)
		end
	end
end

--test
addhook('load', 'onload')
function onload(...)
	for k, v in pairs({...}) do
		print(v)
	end
end

callhook('load', 'hello', 'world')
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview