Forum

> > CS2D > Scripts > Why not work? (dofile / table usage)
Forums overviewCS2D overview Scripts overviewLog in to reply

English Why not work? (dofile / table usage)

7 replies
To the start Previous 1 Next To the start

old Re: Why not work? (dofile / table usage)

Rainoth
Moderator Off Offline

Quote
1
2
3
4
dir = "sys/lua/"
n_s = "script.lua","sound.lua"
dofile(""..dir..""..n_s.."")
dofile(""..dir..""..n_s.."")
has to be like user tontonEd wrote or
1
2
3
4
dir = "sys/lua/"
n_s = {"script.lua","sound.lua"}
dofile(dir..n_s[1])
dofile(dir..n_s[2])

old Re: Why not work? (dofile / table usage)

DC
Admin Off Offline

Quote
What user tontonEd and user Rainoth wrote works but it's better to design scripts in a more flexible way:
1
2
3
4
5
dir = "sys/lua/"
n_s = {"script.lua","sound.lua"}
for index = 1, #n_s do
	dofile(dir..n_s[index])
end

Advantage:
You can put as many files to your n_s array as you want and they will all be loaded without touching anything else in the code. (untested)

Also:
"Why not work?" is a bad title. Nobody will ever find this thread when searching. Please try to use less generic titles in future. I added "(dofile / table usage)" to at least narrow down the topic.

old Re: Why not work? (dofile / table usage)

Talented Doge
User Off Offline

Quote
I prefer the style DC has mentioned when there are many files need to be loaded.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dir = "sys/lua/shits/"

loadfiles =
{
"color",
"settings",
"cmds",
"radio",
"hooks",
"func",
"saycmds"
}

for i = 1, #loadfiles do
	if io.open(dir..loadfiles[i]..".lua")then
		dofile(dir..loadfiles[i]..".lua")
	else
		print("File "..loadfiles[i].." doesn't exist.")
	end
	io.close()
end

old Re: Why not work? (dofile / table usage)

Dousea
User Off Offline

Quote
To sum up user DC's and user Talented Doge's code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
local directory = "sys/lua/"
local files = {"script", "sound"}

for index, filename in ipairs(files) do
	local path = directory .. filename .. ".lua"
	local file, errorMessage = io.open(path)
	
	if (file) then
		dofile(path)
		file:close()
	else
		error(errorMessage)
	end
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview