Forum

> > CS2D > Scripts > Load a variable from .txt file (LUA script)
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Load a variable from .txt file (LUA script)

21 Antworten
Seite
Zum Anfang Vorherige 1 2 Nächste Zum Anfang

alt Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Zitieren
Hello us nrealSoftWare! Today im making one simple script to load 4 variables from file. so i don't know why my script is not working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("join","_load_data")
function _load_data()
owner = {0}
admin = {0}
vip = {0}
dude = {0}

local file = io.open("sys/lua/rp/1.txt")
local string1, string2, string3, string4 = file:read("","*n","*n","*n")
	owner = string1
	admin = string2
	vip = string3
	dude = string4
end

EDIT: there is code where i using variables "owner","admin","vip","dude":
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
pl = {}
addhook("join","_join")
function _join(id)
pl[id] = {}
pl[id].usgn = player(id,"usgn")
	for _, usgn in ipairs(owner) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the OWNER!")
		end
	end
	for _, usgn in ipairs(admin) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the ADMIN!")
		end
	end
	for _, usgn in ipairs(vip) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the VIP!")
		end
	end
	for _, usgn in ipairs(dude) do
		if player(id,"usgn")==usgn then
			msg2(id,"©000255000You are the DUDE!")
		end
	end
end
2× editiert, zuletzt 13.02.15 19:25:53

alt Re: Load a variable from .txt file (LUA script)

Rainoth
Moderator Off Offline

Zitieren
1
file:read("","*n","*n","*n")
Why is the first one "" not "*n" ?

You should use
table.insert to add values to your tables. what you're doing is basically

a = {} (a table)
a = 11111 (a number)

No way would that work. Especially if you're looping it to gain all elements.

alt Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Zitieren
@user Rainoth: i do not understand you, i should use that: ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
addhook("join","_load_data")
function _load_data()
owner = {}
admin = {}
vip = {}
dude = {}

local file = io.open("sys/lua/rp/1.txt")
local string1, string2, string3, string4 = file:read("*n","*n","*n","*n")
     owner = {string1}
     admin = {string2}
     vip = {string3}
     dude = {string4}
end
Console has written:
table expected, got nil

alt Re: Load a variable from .txt file (LUA script)

_Yank
User Off Offline

Zitieren
Console was written, ROFL.

Anyways, you probably want to use string.match function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
addhook("join","_load_data")
function _load_data()
	owner = {}
	admin = {}
	vip = {}
	dude = {}

	local file = io.open("sys/lua/rp/1.txt", "r")
	local string1, string2, string3, string4 = string.match(file:read("*all"), "(.*), (.*), (.*), (.*)")

	table.insert(owner, string1)
	table.insert(admin, string2)
	table.insert(vip, string3)
	table.insert(dude, string4)
end

alt Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Zitieren
1
2
3
4
5
6
7
8
local file=io.open("1.txt","r")
local line1=file:read("*n")
local line2=file:read("*n")
file:read("*l")	-- for unknown reason you need to read till newline then discard it.
local line3={string.match(file:read("*l"),"(%d*),(%d*)")}
local line4=file:read("*n")
line3[1]=tonumber(line3[1])
line3[2]=tonumber(line3[2])
That's just an example how to read them.

Now line1,line2,line3,line4 contains these
1
2
3
4
0
0
{88222,117228}
88305

alt Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Zitieren
user MikuAuahDark hat geschrieben
1
2
3
4
5
6
7
8
local file=io.open("1.txt","r")
local line1=file:read("*n")
local line2=file:read("*n")
file:read("*l")	-- for unknown reason you need to read till newline then discard it.
local line3={string.match(file:read("*l"),"(%d*),(%d*)")}
local line4=file:read("*n")
line3[1]=tonumber(line3[1])
line3[2]=tonumber(line3[2])
That's just an example how to read them.

but how i should use that?
1× editiert, zuletzt 09.02.15 13:10:17

alt Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Zitieren
line 1 equal to value at line 1, you can change the variable.
That's just an example, use it if you think that's suitable for you because we don't know what those numbers mean at your file. Maybe like money or something like that.

alt Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Zitieren
Ok, after i see your code at first post, i assume that you want something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
owner=0
admin=0
vip={}
dude=0

addhook("startround","_load_data")
function _load_data()
	local f=io.open("sys/lua/rp/1.txt")
	owner,admin=f:read("*n","*n")
	f:read("*l")
	local v=f:read("*l")
	for w in v:gmatch("%d+") do table.insert(vip,tonumber(w)) end
	dude=f:read("*n")
end
_load_data()

alt Re: Load a variable from .txt file (LUA script)

VaiN
User Off Offline

Zitieren
why not do this the easy way? Save your data as Lua and let Lua do the work for you.

Example Data in 1.txt:
1
return {0,0,{88222,117228},88305}

Load the data into a table:
1
2
3
4
local func,err = loadfile("1.txt")
if func then
	mydata = func()
end
now your data is all in a table and easy to use however you want to.

alt Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Zitieren
user muxarus hat geschrieben
Zitat
Console has written:
bad argument #1 to 'ipairs' (table expected, got number)

Strange, i never use ipairs. That should be problen in the another script.

EDIT: Oh, i understand now. You may want something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local function list_usgn_by_comma(_)
	local __={}
	for ___ in _:gmatch("%d+") do
		table.insert(__,tonumber(___))
	end
	return __
end

addhook("startround","load_data")
function load_data()
	local f=io.open("sys/lua/rp/1.txt","r")
	owner=list_usgn_by_comma(f:read("*l"))
	admin=list_usgn_by_comma(f:read("*l"))
	vip=list_usgn_by_comma(f:read("*l"))
	dude=list_usgn_by_comma(f:read("*l"))
	f:close()
end
Code above shouldn't throw any ipairs error in your script.

alt Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Zitieren
user VaiN hat geschrieben
why not do this the easy way? Save your data as Lua and let Lua do the work for you.

oh, of course i can do this

user MikuAuahDark hat geschrieben
Code above shouldn't throw any ipairs error in your script.

still have problem.
Zitat
Console has written:
bad argument #1 to 'ipairs' (table expected, got number)

alt Re: Load a variable from .txt file (LUA script)

MikuAuahDark
User Off Offline

Zitieren
what line? it's bit hard to debug them if you don't give us the line number. Maybe the variable is overwritten somewhere in your another script. Check it again, or add this after the declaration of load_data
load_data()

alt Re: Load a variable from .txt file (LUA script)

muxarus
User Off Offline

Zitieren
@user MikuAuahDark:
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
40
41
42
43
44
local function list_usgn_by_comma(_)
     local __={}
     for ___ in _:gmatch("%d+") do
          table.insert(__,tonumber(___))
     end
     return __
end

addhook("startround","load_data")
function load_data()
     local f=io.open("sys/lua/rp/1.txt","r")
     owner=list_usgn_by_comma(f:read("*l"))
     admin=list_usgn_by_comma(f:read("*l"))
     vip=list_usgn_by_comma(f:read("*l"))
     dude=list_usgn_by_comma(f:read("*l"))
     f:close()
end

pl = {}
addhook("join","_join")
function _join(id)
pl[id] = {}
pl[id].usgn = player(id,"usgn")
     for _, usgn in ipairs(owner) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the OWNER!")
          end
     end
     for _, usgn in ipairs(admin) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the ADMIN!")
          end
     end
     for _, usgn in ipairs(vip) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the VIP!")
          end
     end
     for _, usgn in ipairs(dude) do --There (bad argument #1 to 'ipairs' (table expected got nil))
          if player(id,"usgn")==usgn then
               msg2(id,"©000255000You are the DUDE!")
          end
     end
end
errors only in that script. if i add inside that script default numbers for variables (owner, admin, vip, dude) will be no errors but i want to load variables from file
2× editiert, zuletzt 15.02.15 10:42:39
Zum Anfang Vorherige 1 2 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht